该功能主要通过 solrconfig.xml 和 managed-schema 两个文件的修改完成
1.managed-schema 文件修改,做准备工作,这里我之前使用了IK分词器,可以改为自己正在使用的,第二个fieldType 是一个注意点!!尤其是使用中文的拼写检查的时候
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!-- 说明 这里spell 单独定义一个类型 是为了屏蔽搜索分词对中文拼写检测的影响,分词后查询结果过多不会给出建议词 -->
<fieldType name="spell_text_ik" class="solr.TextField">
<analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query">
<tokenizer class="solr.WhitespaceTokenizerFactory"/>
</analyzer>
</fieldType>
<field name="author" type="text_ik" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="description" type="text_ik" multiValued="false" indexed="true" required="false" stored="true"/>
<field name="title" type="text_ik" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="text" type="spell_text_ik" multiValued="true" indexed="true" stored="false"/>
<copyField source="author" dest="text"/>
<copyField source="description" dest="text"/>
<copyField source="title" dest="text"/>
2.修改 solrconfig.xml 配置文件
第一步 ,修改 searchComponent 组件建议查询的字段,我这里把 field 改为 text,text为多个字段的复制值,这里把多余的配置删除了,只贴了这一个,后续使用的时候也只是使用的这个,引入第三方词典可以根据自己需要开启 ,两个比较重要的字段单独说明一下name =default ,后续会使用,maxEdits 最大容错的字数,minQueryLength 最小的检测字符串长度,其它可以参考官方文档详细了解
<searchComponent name="spellcheck" class="solr.SpellCheckComponent">
<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>
</searchComponent>
第二步,修改spell requestHandler 请求处理器,当然也可以配置到 select里面,这里我只做了一个修改,屏蔽掉了<str name="spellcheck.dictionary">wordbreak</str> 因为分词的检测结果不是我期望的,可以自己开启和关闭做一下测试,注意:如过这里使用多个spellcheck,这些spellcheck的field使用的分词器必须一致,不然会报错 All checkers need to use the same Analyzer
<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>
如果开启了 wordbreak
3.通过命令建立词典
http://****:8983/solr/main_collection/spell?q=*%3A*&wt=json&indent=true&spellcheck.build=true
竣工,比较简单,我的需求是要第一个,可以根据自己的需求选择自己需要的spellcheck,当然也可以自定义
补充:历史遗留项目 使用的 solr 版本 5.3 ,DirectSolrSpellChecker 和 file 第三方字典好像不能同时使用?待确认
技术交流群:208779755 如有问题欢迎加群讨论