不错的 solr 使用安装介绍

前些日子做了个 apache solr 应用的入门介绍,也在博客记录下,方便新手看看。以搜索论坛帖子为示例。

1、先下载 Apache Solr 1.3 http://apache.etoak.com/lucene/solr/1.3.0/apache-solr-1.3.0.zip,解压到如 E:\apache-solr-1.3.0。

2、下载 Apache Tomcat 6.0.18 http://labs.xiaonei.com/apache-mirror/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.zip,解压到如 E:\apache-tomcat-6.0.18。

3、solr 安装到 tomcat。修改 E:\apache-tomcat-6.0.18\conf\server.xml,加个 URIEncoding="UTF-8",把 8080 的那一块改为:

  1. <Connector port="8080" protocol="HTTP/1.1"  
  2.            connectionTimeout="20000"  
  3.            redirectPort="8443" URIEncoding="UTF-8"/>  

把下面的内容保存到 E:\apache-tomcat-6.0.18\conf\Catalina\localhost\solr.xml,没有这个目录自行创建。

  1. <Context docBase="E:/apache-solr-1.3.0/dist/apache-solr-1.3.0.war" reloadable="true" >  
  2.     <Environment name="solr/home" type="java.lang.String" value="E:/apache-solr-1.3.0/example/solr" override="true" />  
  3. </Context>  

solr 的更多方式请看:solr install

4、现在安装好,启动 tomcat,并打开 http://localhost:8080/solr/admin/ 看看界面。

5、为搜索论坛帖子应用设计索引结构:

字段说明
id帖子 id
user发表用户名或UserId
title标题
content内容
timestamp发表时间
text把标题和内容放到这里,可以用同时搜索这些内容。

6、上面的索引结构告诉 solr,把下面的内容覆盖 E:\apache-solr-1.3.0\example\solr\conf\scheam.xml,(可以先备份这文件,方便以后看官方示例):

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <schema name="example" version="1.1">  
  4.   
  5.   <types>  
  6.     <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>  
  7.     <fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>  
  8.   
  9.     <!-- The format for this date field is of the form 1995-12-31T23:59:59Z, and   
  10.          is a more restricted form of the canonical representation of dateTime   
  11.          http://www.w3.org/TR/xmlschema-2/#dateTime   
  12.          The trailing "Z" designates UTC time and is mandatory.   
  13.          Optional fractional seconds are allowed: 1995-12-31T23:59:59.999Z   
  14.          All other components are mandatory.   
  15.   
  16.          Expressions can also be used to denote calculations that should be   
  17.          performed relative to "NOW" to determine the value, ie...   
  18.   
  19.                NOW/HOUR   
  20.                   ... Round to the start of the current hour   
  21.                NOW-1DAY   
  22.                   ... Exactly 1 day prior to now   
  23.                NOW/DAY+6MONTHS+3DAYS   
  24.                   ... 6 months and 3 days in the future from the start of   
  25.                       the current day   
  26.   
  27.          Consult the DateField javadocs for more information.   
  28.       -->  
  29.     <fieldType name="date" class="solr.DateField" sortMissingLast="true" omitNorms="true"/>  
  30.   
  31.     <fieldType name="text" class="solr.TextField" positionIncrementGap="100">  
  32.       <analyzer>  
  33.         <tokenizer class="solr.CJKTokenizerFactory"/>  
  34.       </analyzer>  
  35.     </fieldType>  
  36.   
  37.  </types>  
  38.   
  39.  <fields>  
  40.    <field name="id" type="sint" indexed="true" stored="true" required="true" />  
  41.    <field name="user" type="string" indexed="true" stored="true"/>  
  42.    <field name="title" type="text" indexed="true" stored="true"/>  
  43.    <field name="content" type="text" indexed="true" stored="true" />  
  44.    <field name="timestamp" type="date" indexed="true" stored="true" default="NOW"/>  
  45.   
  46.    <!-- catchall field, containing all other searchable text fields (implemented   
  47.         via copyField further on in this schema  -->  
  48.    <field name="text" type="text" indexed="true" stored="false" multiValued="true"/>  
  49.  </fields>  
  50.   
  51.  <!-- Field to use to determine and enforce document uniqueness.   
  52.       Unless this field is marked with required="false", it will be a required field   
  53.    -->  
  54.  <uniqueKey>id</uniqueKey>  
  55.   
  56.  <!-- field for the QueryParser to use when an explicit fieldname is absent -->  
  57.  <defaultSearchField>text</defaultSearchField>  
  58.   
  59.  <!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->  
  60.  <solrQueryParser defaultOperator="AND"/>  
  61.   
  62.   <!-- copyField commands copy one field to another at the time a document   
  63.         is added to the index.  It's used either to index the same field differently,   
  64.         or to add multiple fields to the same field for easier/faster searching.  -->  
  65. <!-- -->  
  66.    <copyField source="title" dest="text"/>  
  67.    <copyField source="content" dest="text"/>  
  68.   
  69. </schema>  

7、重启 tomcat,然后手动在 E:\apache-solr-1.3.0\example\exampledocs 创建两个 xml 数据文件。分别保存为 demo-doc1.xml 和 demo-doc2.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <add>  
  3.     <doc>  
  4.         <field name="id">1</field>  
  5.         <field name="user">chenlb</field>  
  6.         <field name="title">solr 应用演讲</field>  
  7.         <field name="content">这一小节是讲提交数据给服务器做索引,这里有一些数据,如:服务器,可以试查找它。</field>  
  8.     </doc>  
  9. </add>  
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <add>  
  3.     <doc>  
  4.         <field name="id">2</field>  
  5.         <field name="user">bory.chan</field>  
  6.         <field name="title">搜索引擎</field>  
  7.         <field name="content">搜索服务器那边有很多数据。</field>  
  8.         <field name="timestamp">2009-02-18T00:00:00Z</field>  
  9.     </doc>  
  10.     <doc>  
  11.         <field name="id">3</field>  
  12.         <field name="user">other</field>  
  13.         <field name="title">这是什么</field>  
  14.         <field name="content">你喜欢什么运动?篮球?</field>  
  15.         <field name="timestamp">2009-02-18T12:33:05.123Z</field>  
  16.     </doc>  
  17. </add>  

8、提交数据做索引,到 E:\apache-solr-1.3.0\example\exampledocs,运行:

E:\apache-solr-1.3.0\example\exampledocs>java -Durl=http://localhost:8080/solr/update -Dcommit=yes -jar post.jar demo-doc*.xml
SimplePostTool: version 1.2
SimplePostTool: WARNING: Make sure your XML documents are encoded in UTF-8, other encodings are not currently supported
SimplePostTool: POSTing files to http://localhost:8080/solr/update..
SimplePostTool: POSTing file demo-doc1.xml
SimplePostTool: POSTing file demo-doc2.xml
SimplePostTool: COMMITting Solr index changes..

9、查看搜索结果:

所有内容 http://localhost:8080/solr/select/?q=*%3A*&version=2.2&start=0&rows=10&indent=on

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <response>  
  3.   
  4. <lst name="responseHeader">  
  5.  <int name="status">0</int>  
  6.  <int name="QTime">0</int>  
  7.  <lst name="params">  
  8.   <str name="indent">on</str>  
  9.   <str name="start">0</str>  
  10.   <str name="q">*:*</str>  
  11.   <str name="rows">10</str>  
  12.   <str name="version">2.2</str>  
  13.  </lst>  
  14. </lst>  
  15. <result name="response" numFound="3" start="0">  
  16.  <doc>  
  17.   <str name="content">这一小节是讲提交数据给服务器做索引,这里有一些数据,如:服务器,可以试查找它。</str>  
  18.   <int name="id">1</int>  
  19.   <date name="timestamp">2009-05-27T04:07:54.89Z</date>  
  20.   <str name="title">solr 应用演讲</str>  
  21.   <str name="user">chenlb</str>  
  22.  </doc>  
  23.  <doc>  
  24.   <str name="content">搜索服务器那边有很多数据。</str>  
  25.   <int name="id">2</int>  
  26.   <date name="timestamp">2009-02-18T00:00:00Z</date>  
  27.   <str name="title">搜索引擎</str>  
  28.   <str name="user">bory.chan</str>  
  29.  </doc>  
  30.  <doc>  
  31.   <str name="content">你喜欢什么运动?篮球?</str>  
  32.   <int name="id">3</int>  
  33.   <date name="timestamp">2009-02-18T12:33:05.123Z</date>  
  34.   <str name="title">这是什么</str>  
  35.   <str name="user">other</str>  
  36.  </doc>  
  37. </result>  
  38. </response>  

bory.chan 用户的:http://localhost:8080/solr/select/?q=user%3Abory.chan&version=2.2&start=0&rows=10&indent=on

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <response>  
  3.   
  4. <lst name="responseHeader">  
  5.  <int name="status">0</int>  
  6.  <int name="QTime">0</int>  
  7.  <lst name="params">  
  8.   <str name="indent">on</str>  
  9.   <str name="start">0</str>  
  10.   <str name="q">user:bory.chan</str>  
  11.   <str name="rows">10</str>  
  12.   <str name="version">2.2</str>  
  13.  </lst>  
  14. </lst>  
  15. <result name="response" numFound="1" start="0">  
  16.  <doc>  
  17.   <str name="content">搜索服务器那边有很多数据。</str>  
  18.   <int name="id">2</int>  
  19.   <date name="timestamp">2009-02-18T00:00:00Z</date>  
  20.   <str name="title">搜索引擎</str>  
  21.   <str name="user">bory.chan</str>  
  22.  </doc>  
  23. </result>  
  24. </response>  

时间 http://localhost:8080/solr/select/?q=timestamp%3A%5B%222009-02-18T00%3A00%3A00Z%22+TO+%222009-02-19T00%3A00%3A00Z%22%5D&version=2.2&start=0&rows=10&indent=on

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <response>  
  3.   
  4. <lst name="responseHeader">  
  5.  <int name="status">0</int>  
  6.  <int name="QTime">16</int>  
  7.  <lst name="params">  
  8.   <str name="indent">on</str>  
  9.   <str name="start">0</str>  
  10.   <str name="q">timestamp:["2009-02-18T00:00:00Z" TO "2009-02-19T00:00:00Z"]</str>  
  11.   <str name="rows">10</str>  
  12.   <str name="version">2.2</str>  
  13.  </lst>  
  14. </lst>  
  15. <result name="response" numFound="2" start="0">  
  16.  <doc>  
  17.   <str name="content">搜索服务器那边有很多数据。</str>  
  18.   <int name="id">2</int>  
  19.   <date name="timestamp">2009-02-18T00:00:00Z</date>  
  20.   <str name="title">搜索引擎</str>  
  21.   <str name="user">bory.chan</str>  
  22.  </doc>  
  23.  <doc>  
  24.   <str name="content">你喜欢什么运动?篮球?</str>  
  25.   <int name="id">3</int>  
  26.   <date name="timestamp">2009-02-18T12:33:05.123Z</date>  
  27.   <str name="title">这是什么</str>  
  28.   <str name="user">other</str>  
  29.  </doc>  
  30. </result>  
  31. </response>  

常用的 solr 查询参数请看:solr 查询参数说明

简单的示例已经完成了,索引文件(默认)会在 CWD/solr/data/index 目录下,要改为 solr.home/data目录下,在 F:\apache-solr-1.3.0\example\solr\conf\solrconfig.xml 把 dataDir 注释掉,如:

  1. <!--  
  2. <dataDir>${solr.data.dir:./solr/data}</dataDir>  
  3. -->  
  <!--
  <dataDir>${solr.data.dir:./solr/data}</dataDir>
  -->

说明:上面没有使用中文分词,用官方的 CJK 分词,另外有 mmseg4j 中文分词的示例,请看:solr 中文分词 mmseg4j 使用例子

posted @ 2011-10-18 14:29  ITAres  阅读(709)  评论(0编辑  收藏  举报