Solr 使用入门介绍,以搜索论坛帖子为示例

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原文出处, 即下面的声明.

原文出处:点击打开链接

原文还介绍了solr的部署,由于前面我已经有过一篇文章了,所以省略

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

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

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

<?xml version="1.0" encoding="UTF-8" ?> 
<schema name="example" version="1.1">  
  <types>  
    <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>  
    <fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>  
    <fieldType name="date" class="solr.DateField" sortMissingLast="true" omitNorms="true"/>  
    <fieldType name="text" class="solr.TextField" positionIncrementGap="100">  
      <analyzer>  
        <tokenizer class="solr.CJKTokenizerFactory"/>  
      </analyzer>  
    </fieldType>  
 </types>  
 
 <fields>  
  <field name="id" type="sint" indexed="true" stored="true" required="true" />  
  <field name="user" type="string" indexed="true" stored="true"/>  
  <field name="title" type="text" indexed="true" stored="true"/>  
  <field name="content" type="text" indexed="true" stored="true" />  
  <field name="timestamp" type="date" indexed="true" stored="true" default="NOW"/>  
  <field name="text" type="text" indexed="true" stored="false" multiValued="true"/>  
 </fields>  
 
 <!-- Field to use to determine and enforce document uniqueness.   
      Unless this field is marked with required="false", it will be a required field   
   -->  
 <uniqueKey>id</uniqueKey>  
 
 <!-- field for the QueryParser to use when an explicit fieldname is absent -->  
 <defaultSearchField>text</defaultSearchField>  
  
 <!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->  
 <solrQueryParser defaultOperator="AND"/>  
  
 <!-- copyField commands copy one field to another at the time a document   
       is added to the index.  It's used either to index the same field differently,   
       or to add multiple fields to the same field for easier/faster searching.  -->  
<!-- -->  
  <copyField source="title" dest="text"/>  
  <copyField source="content" dest="text"/>  
</schema>  


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

<?xml version="1.0" encoding="UTF-8" ?>  
<add>  
    <doc>  
        <field name="id">1</field>  
        <field name="user">chenlb</field>  
        <field name="title">solr 应用演讲</field>  
        <field name="content">这一小节是讲提交数据给服务器做索引,这里有一些数据,如:服务器,可以试查找它。</field>  
    </doc>  
</add> 


 

<?xml version="1.0" encoding="UTF-8" ?>  
<add>  
    <doc>  
        <field name="id">2</field>  
        <field name="user">bory.chan</field>  
        <field name="title">搜索引擎</field>  
        <field name="content">搜索服务器那边有很多数据。</field>  
        <field name="timestamp">2009-02-18T00:00:00Z</field>  
    </doc>  
    <doc>  
        <field name="id">3</field>  
        <field name="user">other</field>  
        <field name="title">这是什么</field>  
        <field name="content">你喜欢什么运动?篮球?</field>  
        <field name="timestamp">2009-02-18T12:33:05.123Z</field>  
    </doc>  
</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..

 

solr 1.4 运行这个例子是有这个问题。报错:

  1. org.apache.solr.common.SolrException: QueryElevationComponent requires the schema to have a uniqueKeyField implemented using StrField。  

把 solrconfig.xml 中删除两个结点:也就是elevator组件

<searchComponent name="elevator" class="solr.QueryElevationComponent" >
    <!-- pick a fieldType to analyze queries -->
   <str name="queryFieldType">string</str>
    <str name="config-file">elevate.xml</str>
  </searchComponent>

  <!-- A request handler for demonstrating the elevator component -->
  <requestHandler name="/elevate" class="solr.SearchHandler" startup="lazy">
    <lst name="defaults">
      <str name="echoParams">explicit</str>
      <str name="df">text</str>
    </lst>
    <arr name="last-components">
      <str>elevator</str>
    </arr>
  </requestHandler>


 

 



   

你可能感兴趣的:(Date,String,服务器,Solr,Class,encoding)