Solr 6.7学习笔记(06)-- spell check

        拼写检查也是搜索引擎必备的功能。Solr中提供了SpellCheckComponent 来实现此功能。我看过《Solr In Action》,是基于Solr4.X版本的,那时Suggestion 和 SpellCheck 用的是同一个组件。我个人感觉这两个其实也可以归为同一类概念。当你在搜索框中输入拼写错误的单词时,找不到符合此错误单词的suggest 项,应该加入较正后的单词。试了一下度娘和谷哥,输入拼写错误的单词时,它会在suggestion里直接提供正确拼写的单词。Solr 6.X里增加了Suggest 的组件(不确定Solr5.X里有没有这个组件),个人感觉Suggest 偏向于搜索的内容是正确的情况下给出的建议,SpellCheck是搜索的内容是不正确的情况下给出的建议。

        SpellCheck的一些参数:

参数 说明
spellcheck 为true时,表示开启拼写检查
spellcheck.q 通常是搜索框输入的内容
spellcheck.build 为true时,将会创建用于spellcheck所需要的字典。通常不要在每次请求都指定此值为true
spellcheck.collate 为true时,将会根据最佳的建议重新构建一个查询语句。注意:仅返回重新构建的查询语句,并没有真正用它去查询
spellcheck.maxCollations 默认值是1. 指定返回最大较正后的查询语句数
spellcheck.maxCollationTries 默认值是0. 较低的值具有较好的性能,但是有可能没有较正结果。较高的值不容易出现找不到较正结果的情况
spellcheck.maxCollationEvaluations 默认值是10000。指定最大的较正组合。当用户输入的错误单词较多时,可能有很多种较正后的组合。
spellcheck.collateExtendedResults 默认值是false。为true时,返回扩展的详细的较正结果
spellcheck.collateMaxCollectDocs 测试可能的较正结果时,选取的最大document数量。默认值是0. 表示所有的document都需要检查。
spellcheck.collateParam.* 指定 param=value 对,(不是很明白有什么用)
spellcheck.count 返回的正确的拼写结果的条数
spellcheck.dictionary 指定拼写检查的字典
spellcheck.extendedResults 在拼定检查结果中附加一些额外的信息
spellcheck.onlyMorePopular 为true时,将会返回命中结果比当前查询语句命中结果更多的语句。
spellcheck.maxResultsForSuggest

e.g. 如果设置为5,当用户的输入的查询只返回5条或更少的记录,spellchecker将会报告"correctlySpelled=false",并提供建议项。设置一个大于0的值,

有助于提供 "你要搜的是不是:XXX" 这样的功能。

spellcheck.alternativeTermCount 为每个分词返回的存在于索引或字典中的建议的分词的数目
spellcheck.reload 重新加载spellchecker
spellcheck.accuracy 精确度。用于判断某个结果是否算作一个较正项
spellcheck.<DICT_NAME>.key e.g. spellcheck.myDict.myKey=myValue 貌似是用这里的key,value去替代字典中的值。

举个例子:

<searchComponent name="spellcheck" class="solr.SpellCheckComponent">

    <str name="queryAnalyzerFieldType">text_general</str>
    <lst name="spellchecker">
      <str name="name">default</str>
      <str name="field">text</str>
      <str name="classname">solr.DirectSolrSpellChecker</str>
      <!-- the spellcheck distance measure used, the default is the internal levenshtein -->
      <str name="distanceMeasure">internal</str>
      <!-- minimum accuracy needed to be considered a valid spellcheck suggestion -->
      <float name="accuracy">0.5</float>
      <!-- the maximum #edits we consider when enumerating terms: can be 1 or 2 -->
      <int name="maxEdits">2</int>
      <!-- the minimum shared prefix when enumerating terms -->
      <int name="minPrefix">1</int>
      <!-- maximum number of inspections per result. -->
      <int name="maxInspections">5</int>
      <!-- minimum length of a query term to be considered for correction -->
      <int name="minQueryLength">4</int>
      <!-- maximum threshold of documents a query term can appear to be considered for correction -->
      <float name="maxQueryFrequency">0.01</float>
      <!-- uncomment this to require suggestions to occur in 1% of the documents
        <float name="thresholdTokenFrequency">.01</float>
      -->
    </lst>
    
    <!-- a spellchecker that can break or combine words.  See "/spell" handler below for usage -->
    <lst name="spellchecker">
      <str name="name">wordbreak</str>
      <str name="classname">solr.WordBreakSolrSpellChecker</str>      
      <str name="field">name</str>
      <str name="combineWords">true</str>
      <str name="breakWords">true</str>
      <int name="maxChanges">10</int>
    </lst>
   
  </searchComponent>
  
  <!-- spellcheck component 的使用示例.  

       NOTE: 这纯粹是一个例子.  此处把 SpellCheckComponent 嵌入到 request handler 中是为了
       不需要多加一次spellcheck的请求
       
       See http://wiki.apache.org/solr/SpellCheckComponent for details
       on the request parameters.
    -->
  <requestHandler name="/spell" class="solr.SearchHandler" startup="lazy">
    <lst name="defaults">
      <!-- Solr will use suggestions from both the 'default' spellchecker
           and from the 'wordbreak' spellchecker and combine them.
           collations (re-written queries) can include a combination of
           corrections from both spellcheckers -->
      <str name="spellcheck.dictionary">default</str>
      <str name="spellcheck.dictionary">wordbreak</str>
      <str name="spellcheck">on</str>
      <str name="spellcheck.extendedResults">true</str>       
      <str name="spellcheck.count">10</str>
      <str name="spellcheck.alternativeTermCount">5</str>
      <str name="spellcheck.maxResultsForSuggest">5</str>       
      <str name="spellcheck.collate">true</str>
      <str name="spellcheck.collateExtendedResults">true</str>  
      <str name="spellcheck.maxCollationTries">10</str>
      <str name="spellcheck.maxCollations">5</str>         
    </lst>
    <arr name="last-components">
      <str>spellcheck</str>
    </arr>
  </requestHandler>

注意:指定多个spellchecker 时,每个spellchecker中的 field 的 fieldType必须和 <queryAnalyzerFieldType>中指定的一致。

以下是几种可选的classname:

1. IndexBasedSpellChecker

    将创建索引(以Solr索引为基础),用于拼写检查

2. DirectSolrSpellChecker

    使用Solr索引中的分词来进行拼写检查,不会像IndexBasedSpellChecker那样创建另外的索引

3. FileBasedSpellChecker

    使用外部的文件做为拼写检查的字典。

4. WordBreakSolrSpellChecker

    使用分词组合或拆散分词作为拼写检查。

原文地址:https://www.cnblogs.com/langfanyun/p/7479130.html