solr-spellcheck

来源:互联网 发布:java游戏小程序 编辑:程序博客网 时间:2024/06/05 05:09
原文链接:http://ronxin999.blog.163.com/blog/static/4221792020117304579589/


首先需要说明的一点是Solr 的组件类(Component)和handler 类的关系,组件是绑定在handler上的,即handler在调用他的两个方法prepare和process时,分别调用该handler上的组件,那么Component是怎么加到handler上去的呢,下面会讲到。

spellcheckComponent是干什么的还是在这里简单描述下,就是像baidu,google那样下面有提示:你是不是要找那个关键词 
这些用来提示的关键字是怎么来的呢,在Solr里就要归功于SpellCheckComponent了,正因为有了它才,我们自己的搜索引擎也能实现这样的功能。

讲完了概念下面讲用法

Solr 要让SpellCheckComponent工作起来并不麻烦,只是网上的资料都没有说清楚要注意的地方。
Solr 的sorlconfig.xml配置文件以帮我们配好了SpellCheckComponent组件。如下:

<searchComponent name="spellcheck" class="solr.SpellCheckComponent">
    <str name="queryAnalyzerFieldType">textSpell</str>
    <!-- Multiple "Spell Checkers" can be declared and used by this
         component
      -->
    <!-- a spellchecker built from a field of the main index, and
         written to disk
      -->
    <lst name="spellchecker">
      <str name="name">default</str>
      <str name="field">title</str>    //对应solr schema.xml文件的field 并会用他的query分词器。
      <str name="spellcheckIndexDir">spellchecker</str>
      <!-- uncomment this to require terms to occur in 1% of the documents in order to be included in the dictionary
          <float name="thresholdTokenFrequency">.01</float>
      -->
    </lst>
  </searchComponent>

queryAnalyzerFieldType属性是你要拼写检查的Field类型,这里可以不改,如果没有该类行,solr会取field属性值对应的FieldType的query 分词器。如果field也找到分词器,Solr会给创建一个按空格划份分词器(WhitespaceAnalyzer)。

注意 :<str name="field">title</str> 这里的field是只在这个字段上进行拼写检查。

到这里 SpellCheckComponent 组件配置好了,好像基本不要我们改什么,关键在下面
因为我们查询是都是Solr的SearchHandler处理的。SearchHandler的组件如果我们不改配置文件的话,Solr会给它5个默认的组件。但是不包过SpellCheckComponent 组件,所以如果你不修改SearchHandler的配置文件的话,我们上面的配置是不起作用的,需要做如下的修改,
<requestHandler name="search" class="solr.SearchHandler" default="true">
    <!-- default values for query parameters can be specified, these
         will be overridden by parameters in the request
      -->
     <lst name="defaults">
       <str name="echoParams">explicit</str>
       <int name="rows">10</int>
     </lst>
       <!--
       <lst name="appends">
         <str name="fq">inStock:true</str>
       </lst>
      -->
       <!--
       <lst name="invariants">
         <str name="facet.field">cat</str>
         <str name="facet.field">manu_exact</str>
         <str name="facet.query">price:[* TO 500]</str>
         <str name="facet.query">price:[500 TO *]</str>
       </lst>
      -->
    <!-- If the default list of SearchComponents is not desired, that
         list can either be overridden completely, or components can be
         prepended or appended to the default list.  (see below)
      -->
    <!-- 注意这里,如果你把这个注释去掉,SearchHandler就只有components标签下面的组件了,
       <arr name="components">
         <str>nameOfCustomComponent1</str>
         <str>nameOfCustomComponent2</str>
       </arr>
      -->
     //这里加上spellcheck组件,你的拼写检查就可以工作了。
      <arr name="first-components">
         <str>spellcheck</str>
      </arr>   
    <!--  //或者这里加上spellcheck组件。
     <arr name="last-components">
         <str>myLastComponentName</str>
       </arr>
     -->
    </requestHandler>

上面的配置是solrconfig里面copy的,把注释删了而已。
 
这里重点说明下handler拥有的 components,first-components,last-components这三个属性.Solr的handler都是同过这三个属性来取他所依赖的components的,

如果配置了components,则SOlr不会给默认的5个组件。而且你配置的first-components,last-components两个都是无效的。
如果配置了first-components,SOlr会给handler添加5个默认的组件时,同时会添加first-components配置的组件,而且这组件最先工作。
如果配置了last-components,同上,只是last-components 配置的组件是到数第2个,那最后一个是谁呢,因为Solr总是把5个默认其中一个DebugComponent放在最后。

到这里总算写完了。总结下,你要在SearchHandler里加上first-components或者last-components,才能使你的SpellCheckComponent在你查询时工作。
0 0