Apache Solr - 索引数据

通常,索引是系统地排列文档或(其他实体).索引使用户能够在文档中查找信息.

  • 索引收集,解析和存储文档.

  • 索引是为了在查找所需文档时提高搜索查询的速度和性能.

Apache Solr中的索引

在Apache Solr中,我们可以索引(添加,删除,修改)各种文档格式,如xml,csv,pdf等.我们可以添加数据到Solr索引有几种方式.

在本章中,我们将讨论索引和减号;

  • 使用Solr Web界面.

  • 使用任何客户端API,如Java,Python等.

  • 使用发布工具.

在本章中,我们将讨论如何使用各种接口将数据添加到Apache Solr的索引(命令行, Web界面和Java客户端API)

使用Post命令添加文档

Solr有一个帖子命令在 bin/目录中.使用此命令,您可以在Apache Solr中索引各种格式的文件,如JSON,XML,CSV.

浏览Apache Solr的 bin 目录并执行post命令的 -h选项,如下面的代码块所示.

[Hadoop@localhost bin]$ cd $SOLR_HOME 
[Hadoop@localhost bin]$ ./post -h

执行上述命令后,您将获得一个列表 post命令的选项,如下所示.

Usage: post -c <collection> [OPTIONS] <files|directories|urls|-d [".."]> 
or post –help  
   collection name defaults to DEFAULT_SOLR_COLLECTION if not specified  
OPTIONS 
======= 
Solr options: 
   -url <base Solr update URL> (overrides collection, host, and port) 
   -host <host> (default: localhost) 
   -p or -port <port> (default: 8983) 
   -commit yes|no (default: yes)  

Web crawl options:  
   -recursive <depth> (default: 1) 
   -delay <seconds> (default: 10)  

Directory crawl options: 
   -delay <seconds> (default: 0)  

stdin/args options: 
   -type <content/type> (default: application/xml)  

Other options: 
   -filetypes <type>[,<type>,...] (default:   
   xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,
   rtf,htm,html,txt,log) 
   -params "<key> = <value>[&<key> = <value>...]" (values must be 
   URL-encoded; these pass through to Solr update request) 
   -out yes|no (default: no; yes outputs Solr response to console) 
   -format Solr (sends application/json content as Solr commands 
   to /update instead of /update/json/docs)  

Examples: 
* JSON file:./post -c wizbang events.json 
* XML files: ./post -c records article*.xml 
* CSV file: ./post -c signals LATEST-signals.csv 
* Directory of files: ./post -c myfiles ~/Documents 
* Web crawl: ./post -c gettingstarted http://lucene.apache.org/Solr -recursive 1 -delay 1 
* Standard input (stdin): echo '{commit: {}}' | ./post -c my_collection -
type application/json -out yes –d 
* Data as string: ./post -c signals -type text/csv -out yes -d $'id,value\n1,0.47'

示例

假设我们有一个名为 sample.csv的文件包含以下内容(在 bin 目录中).

Student IDFirst NameLasst NamePhoneCity
001RajivReddy9848022337Hyderabad
002SiddharthBhattacharya9848022338Kolkata
003RajeshKhanna9848022339Delhi
004PreethiAgarwal9848022330Pune
005TrupthiMohanty9848022336Bhubaneshwar
006ArchanaMishra9848022335Chennai

上述数据集包含个人详细信息,如学生ID,名字,姓氏,电话和城市.数据集的CSV文件如下所示.在这里,您必须注意,您需要提及架构,记录其第一行.

id,    first_name,   last_name,   phone_no,      location 
001,   Pruthvi,      Reddy,       9848022337,    Hyderabad 
002,   kasyap,       Sastry,      9848022338,    Vishakapatnam 
003,   Rajesh,       Khanna,      9848022339,    Delhi 
004,   Preethi,      Agarwal,     9848022330,    Pune 
005,   Trupthi,      Mohanty,     9848022336,    Bhubaneshwar 
006,   Archana,      Mishra,      9848022335,    Chennai

您可以将此数据编入索引使用 post 命令命名 sample_Solr 的核心如下 :

[Hadoop@localhost bin]$ ./post -c Solr_sample sample.csv

在执行上述命令时,给定文档在指定的核心下编制索引,生成以下内容输出.

/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core
6.2.0.jar -Dauto = yes -Dc = Solr_sample -Ddata = files 
org.apache.Solr.util.SimplePostTool sample.csv 
SimplePostTool version 5.0.0 
Posting files to [base] url http://localhost:8983/Solr/Solr_sample/update... 
Entering auto mode. File endings considered are 
xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,
htm,html,txt,log 
POSTing file sample.csv (text/csv) to [base] 
1 files indexed. 
COMMITting Solr index changes to 
http://localhost:8983/Solr/Solr_sample/update... 
Time spent: 0:00:00.228

使用以下URL : 访问Solr Web UI的主页;

http://localhost:8983/

选择核心 Solr_sample .默认情况下,请求处理程序为/select ,查询为":".如果不进行任何修改,请单击页面底部的 ExecuteQuery 按钮.

Solr Sample

在执行查询时,您可以以JSON格式观察索引CSV文档的内容(默认),如以下屏幕截图所示.

CSV Document

注意 : 以同样的方式,您可以索引其他文件格式,如JSON,XML,CSV等.

使用Solr Web界面添加文档

您还可以使用Solr提供的Web界面索引文档.让我们看看如何索引以下JSON文档.

[ 
   { 
      "id" : "001", 
      "name" : "Ram", 
      "age" : 53, 
      "Designation" : "Manager", 
      "Location" : "Hyderabad", 
   }, 
   { 
      "id" : "002", 
      "name" : "Robert", 
      "age" : 43, 
      "Designation" : "SR.Programmer", 
      "Location" : "Chennai", 
   }, 
   { 
      "id" : "003", 
      "name" : "Rahim", 
      "age" : 25, 
      "Designation" : "JR.Programmer", 
      "Location" : "Delhi", 
   } 
]

第1步

使用以下网址打开Solr网络界面 :

http://localhost:8983/

第2步

选择核心 Solr_sample 的.默认情况下,Request Handler,Common Within,Overwrite和Boost字段的值分别为/update,1000,true和1.0,如以下屏幕截图所示.

Request Handler

现在,从JSON,CSV,XML等中选择所需的文档格式.键入要编制索引的文档在文本区域中,单击提交文档按钮,如以下屏幕截图所示.

提交文档

使用Java客户端API添加文档

以下是将文档添加到Apache Solr索引的Java程序.将此代码保存在名为 AddingDocument.java 的文件中.

import java.io.IOException;  

import org.apache.Solr.client.Solrj.SolrClient; 
import org.apache.Solr.client.Solrj.SolrServerException; 
import org.apache.Solr.client.Solrj.impl.HttpSolrClient; 
import org.apache.Solr.common.SolrInputDocument; 

public class AddingDocument { 
   public static void main(String args[]) throws Exception { 
      //Preparing the Solr client 
      String urlString = "http://localhost:8983/Solr/my_core"; 
      SolrClient Solr = new HttpSolrClient.Builder(urlString).build();   
      
      //Preparing the Solr document 
      SolrInputDocument doc = new SolrInputDocument(); 
   
      //Adding fields to the document 
      doc.addField("id", "003"); 
      doc.addField("name", "Rajaman"); 
      doc.addField("age","34"); 
      doc.addField("addr","vishakapatnam"); 
         
      //Adding the document to Solr 
      Solr.add(doc);         
         
      //Saving the changes 
      Solr.commit(); 
      System.out.println("Documents added"); 
   } 
}

通过在终端中执行以下命令来编译上述代码 :

[Hadoop@localhost bin]$ javac AddingDocument 
[Hadoop@localhost bin]$ java AddingDocument

在执行上述命令时,您将获得以下输出.

Documents added