华风 发表于 2017-3-2 10:00:45

Solr自学笔记 1 —— Solr部署,添加,更新,删除

  1.solr介绍:自查wiki



2.环境部署
   1)下载地址:http://lucene.apache.org/solr/mirrors-solr-latest-redir.html
   2)需要的环境:         
            1.Java 1.7 or greater. Some places you can get it are from Oracle, Open JDK, or IBM.
Running java -version at the command line should indicate a version number starting with 1.7.
Gnu's GCJ is not supported and does not work with Solr.
         2.A Solr release.
3)官方文档
   docs目录下,如下图所示:


3.教程详情
   1)启动solr服务
          这里使用的是官方自带的例子:start.jar ,将下载的压缩包解压到E:盘中,通过以下命令行

[*]E:\solr-4.8.0\example>java -jar start.jar

   这里执行的是含有Solr WAR的jetty服务器和相应的配置文件

Solr can run in any Java Servlet Container of your choice, but to simplify this tutorial, the example index includes a small installation of Jetty.

To launch Jetty with the Solr WAR, and the example configs, just run the start.jar ...
      此时,可以通过浏览器,输入http://localhost:8983/solr/ 就可以查看Solr的管理界面
   2)此时Solr服务已经运行,但是它并不包含任何数据,你可以通过修改Solr索引,使用POST命令的方式向Solr中增加,更新和删除信息。
   3)添加数据
   这里通过以下命令行,要保证上面的solr服务一定要运行   
    添加数据的的数据格式,包含:
          xml: application/xml,text/xml;
          json: application/xml,text/json;
          csv : application/csv,text/csv;
          javabin: application/javabin;
    向Solr中添加数据的方式,包含:   

[*]

[*]Import records from a database using the Data Import Handler (DIH). 数据库
[*]Load a CSV file (comma separated values), including those exported by Excel or MySQL. CSV文件
[*]POST JSON documentsJSON文档
[*]Index binary documents such as Word and PDF with Solr Cell (ExtractingRequestHandler).Solr格式的pdf和word字节文档
[*]Use SolrJ for Java or other Solr clients to programatically create documents to send to Solr.使用SolrJ或者Solr客户端编码的方式创建文档



[*]E:\solr-4.8.0\example\exampledoc> java -jar post.jar solr.xml monitor.xml

   执行成功结果:
      SimplePostTool version 1.5
Posting files to base url http://localhost:8983/solr/update using content-type a
pplication/xml..
POSTing file solr.xml
POSTing file monitor.xml
2 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/update..
Time spent: 0:00:00.840


[*]solr.xml中内容:

   <add>
<doc>
<field name="id">SOLR1000</field>
<field name="name">Solr, the Enterprise Search Server</field>
<field name="manu">Apache Software Foundation</field>
<field name="cat">software</field>
<field name="cat">search</field>
<field name="features">Advanced Full-Text Search Capabilities using Lucene</field>
<field name="features">Optimized for High Volume Web Traffic</field>
<field name="features">Standards Based Open Interfaces - XML and HTTP</field>
<field name="features">Comprehensive HTML Administration Interfaces</field>
<field name="features">Scalability - Efficient Replication to other Solr Search             Servers
</field>
<field name="features">Flexible and Adaptable with XML configuration and Schema</field>
<field name="features">Good unicode support: h&#xE9;llo (hello with an accent over the            e)
</field>
<field name="price">0</field>
<field name="popularity">10</field>
<field name="inStock">true</field>
<field name="incubationdate_dt">2006-01-17T00:00:00.000Z</field>
</doc>
   </add>
      此时通过一下的URL可以查询到增加的结果: http://localhost:8983/solr/collection1/select?q=solr&wt=xml
      浏览器结果如下:

   {
    "responseHeader": {
      "status": 0,
      "QTime": 1,
      "params": {
            "q": "solr",
            "_": "1474873714872",
            "wt": "json"
      }
    },
    "response": {
      "numFound": 1,
      "start": 0,
      "docs": [
            {
                "id": "SOLR1000",
                "name": "Solr, the Enterprise Search Server",
                "manu": "Apache Software Foundation",
                "cat": [
                  "software",
                  "search"
                ],
                "features": [
                  "Advanced Full-Text Search Capabilities using Lucene",
                  "Optimized for High Volume Web Traffic",
                  "Standards Based Open Interfaces - XML and HTTP",
                  "Comprehensive HTML Administration Interfaces",
                  "Scalability - Efficient Replication to other Solr Search Servers",
                  "Flexible and Adaptable with XML configuration and Schema",
                  "Good unicode support: héllo (hello with an accent over the e)"
                ],
                "price": 0,
                "price_c": "0,USD",
                "popularity": 10,
                "inStock": true,
                "incubationdate_dt": "2006-01-17T00:00:00Z",
                "_version_": 1546511824707387400
            }
      ]
    }
}
   
          4)更新数据:
       更新数据的规则:Whenever you POST commands to Solr to add a document with the same value for the uniqueKey as an existing document, it automatically replaces it for you(根据文档唯一值是否存在,替换文档的内容)
   查看Solr中的文档数URL:
      http://localhost:8983/solr/#/collection1/plugins/core?entry=searcher
   numDocs :某个索引上的可以被搜索到的文档数
   maxDocs :表示某个索引上所有的文档数目,逻辑上可能已被删除,但实现上,并没有在该索引上移除
          5)删除数据:
   可以使用POSTing一个删除命令到相应的URL,明确文档unique key 或者一个查询来匹配多文档。
   执行下面的命令,来删除一个具体的文档:


java -Ddata=args -Dcommit=false -jar post.jar "<delete><id>SP2514</id></delete>"
页: [1]
查看完整版本: Solr自学笔记 1 —— Solr部署,添加,更新,删除