zoukankan      html  css  js  c++  java
  • Solr记录-solr内核与索引

    Solr核心(内核)

    Solr核心(Core)是Lucene索引的运行实例,包含使用它所需的所有Solr配置文件。我们需要创建一个Solr Core来执行索引和分析等操作。

    Solr应用程序可以包含一个或多个核心。 如果需要,Solr应用程序中的两个核心可以相互通信。

    创建一个核心

    安装和启动Solr后,可以连接到Solr的客户端(Web界面)。访问以下网址:

    如下面的截图(箭头指向)所示,最初Apache Solr中并没有核心。 现在我们来学习如何在Solr中创建一个核心。

    使用create命令

    创建核心的一种方法是使用create命令来创建无架构的核心,如下所示 -

    [Hadoop@localhost bin]$ ./Solr create -c solr_sample
    
    Shell

    这里,我们试图在Apache Solr中创建一个solr_sample核心。此命令将创建一个核心,显示以下消息。

    Copying configuration to new core instance directory:
    /home/Hadoop/Solr/server/Solr/Solr_sample 
    
    Creating new core 'Solr_sample' using command: 
    http://localhost:8983/Solr/admin/cores?action=CREATE&name=Solr_sample&instanceD 
    ir = Solr_sample {  
       "responseHeader":{    
          "status":0,    
          "QTime":850
       },  
       "core":"solr_sample" 
    }
    
    Shell

    可以在Solr中创建多个核心。在Solr Admin的左侧下方,有一个核心选择器(core selector),可以在其中选择新创建的核心,在弹出的表单中填入要创建的核心的相关信息,如以下屏幕截图所示。

    使用create_core命令

    或者,也可以使用create_core命令来创建核心。此命令具有以下选项 -

    –c core_name要创建的核心的名称
    -p port_name 要创建的核心的端口
    -d conf_dir 端口的配置目录

    下面来看看如何使用create_core命令。 在这里,我们将尝试创建一个名称为my_core的核心。

    [Hadoop@localhost bin]$ ./Solr create_core -c my_core
    
    Shell

    在执行时,上述命令创建一个核心,它会显示以下消息 -

    Copying configuration to new core instance directory: 
    /home/Hadoop/Solr/server/Solr/my_core  
    
    Creating new core 'my_core' using command: 
    http://localhost:8983/Solr/admin/cores?action=CREATE&name=my_core&instanceD
    ir = my_core {  
       "responseHeader":{  
          "status":0,  
          "QTime":750
       },  
       "core":"my_core" 
    }
    
    Shell

    删除核心

    可以使用Apache Solr的delete命令删除核心。现在假设在Solr中有一个名称为my_core的核心,如下面的截图所示。

    可以使用delete命令删除此核心,方法是将核心名称传递到此命令,如下所示 -

    [Hadoop@localhost bin]$ ./solr delete -c my_core
    
    Shell

    在执行上述命令时,将删除指定的内核,显示以下消息。

    Deleting core 'my_core' using command: 
    http://localhost:8983/Solr/admin/cores?action=UNLOAD&core = my_core&deleteIndex
       = true&deleteDataDir = true&deleteInstanceDir = true  {
    
       "responseHeader" :{    
          "status":0,    
          "QTime":170
       }
    }
    
    Shell

    可以打开Solr的Web界面以验证核心是否已成功删除。

    Solr索引数据

    一般来说,索引是系统地排列文档或(其他实体)。索引使用户能够在文档中快速地查找信息。

    • 索引集合,解析和存储文档。
    • 索引是为了在查找所需文档时提高搜索查询的速度和性能。

    在Apache Solr中的索引

    在Apache Solr中,我们可以索引(添加,删除,修改)各种文档格式,如xml,csv,pdf等。可以通过几种方式向Solr索引添加数据。
    在本章中,将讨论创建索引的几个方法 -

    • 使用Solr Web界面。
    • 使用任何客户端API(如Java,Python等)。
    • 使用提交工具。

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

    使用Post命令添加文档

    Solr在其bin/目录中有一个post命令。使用这个命令,可以在Apache Solr中索引各种格式的文件,例如JSON,XML,CSV。

    进入到Apache Solr的bin目录并执行post命令的-h选项,如以下代码块所示。

    yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ cd $SOLR_HOME 
    yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -h
    
    Shell

    在执行上述命令时,将得到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
    1,0.47'
    
    Shell

    示例`

    假设有一个名称为sample.csv的文件,其内容如下(这个文件也在`bin目录中)。
    上述数据集包含个人详细信息,如学生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命令在名称为Solr_sample的核心下,对此数据编制索引,如下所示:

    yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c solr_sample sample.csv
    
    Shell

    在执行上述命令时,给定文档在指定的核心下会生成索引,生成以下输出。

    yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c solr_sample sample.csv
    /usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.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.663
    
    Shell

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

    选择核心Solr_sample。 默认情况下,请求处理程序是/select,查询为“”。 不做任何修改,单击页面底部的ExecuteQuery按钮。

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

    注意 - 以相同的方式,可以索引其他文件格式,如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", 
       } 
    ]
    
    JSON

    第1步

    使用以下URL打开Solr Web界面 -

    第2步

    选择核心Solr_sample。 默认情况下,Request HandlerCommon WithinOverwriteBoost字段的值分别为/update1000true1.0,如下面的屏幕截图所示。

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

    使用Java Client API添加文档

    以下是Java程序向Apache Solr索引添加文档代码。将代码保存在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"); 
       } 
    }
    
    Java

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

    
    yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ javac AddingDocument.java
    yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ java AddingDocument
    
    Bash

    执行上述命令后,将得到以下输出。

    Documents added
  • 相关阅读:
    poj1182 食物链
    poj1611 The Suspects
    poj3436 Computer Factory
    (转) GPG入门教程
    cronolog安装配置
    RHCA-436-4 rhcs conga
    pxe引导gparted live万能分区
    linux下拔号pptp
    GNU parted简略用法
    hostapd故障解决(1.1,2.0,2.2)
  • 原文地址:https://www.cnblogs.com/xinfang520/p/7722301.html
Copyright © 2011-2022 走看看