在Solr的自动完成/自动补充-Suggester组件:
1.可直接启动example/start.jar启动jetty;访问
[url]http://localhost:8983/solr/spell?q=delll ultrashar&spellcheck=true&spellcheck.collate=true&spellcheck.build=true[/url]
2.配置solrconfig.xml,
①.引用sourceLocation词典。
<searchComponent class="solr.SpellCheckComponent" name="suggest">
<lst name="spellchecker">
<str name="name">suggest</str>
<str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
<str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
<!-- Alternatives to lookupImpl:
org.apache.solr.spelling.suggest.fst.FSTLookup [finite state automaton]
org.apache.solr.spelling.suggest.fst.WFSTLookupFactory [weighted finite state automaton]
org.apache.solr.spelling.suggest.jaspell.JaspellLookup [default, jaspell-based]
org.apache.solr.spelling.suggest.tst.TSTLookup [ternary trees]
-->
<str name="field">name</str> <!-- the indexed field to derive suggestions from -->
<float name="threshold">0.005</float>
<str name="buildOnCommit">true</str>
<str name="sourceLocation">solr\conf\spellings.txt</str>
</lst>
</searchComponent>
<requestHandler class="org.apache.solr.handler.component.SearchHandler" name="/suggest">
<lst name="defaults">
<str name="spellcheck">true</str>
<str name="spellcheck.dictionary">suggest</str>
<str name="spellcheck.onlyMorePopular">true</str>
<str name="spellcheck.count">5</str>
<str name="spellcheck.collate">true</str>
</lst>
<arr name="components">
<str>suggest</str>
</arr>
</requestHandler>
②创建索引目录spellcheckIndexDir,默认在内存中创建,
<searchComponent name="suggest" class="solr.SpellCheckComponent">
<lst name="spellchecker">
<str name="name">default</str>
<str name="classname">solr.IndexBasedSpellChecker</str>
<str name="field">spell</str>
<str name="spellcheckIndexDir">D:\opensource\solr\solr1\solr\data\spellchecker</str>
<str name="accuracy">0.7</str>
<float name="thresholdTokenFrequency">.0001</float>
</lst>
<str name="queryAnalyzerFieldType">textSpell</str>
</searchComponent>
<requestHandler name="/suggest" class="solr.SearchHandler">
<lst name="defaults">
<str name="df">text</str>
<str name="spellcheck.dictionary">default</str>
<str name="spellcheck.onlyMorePopular">false</str>
<str name="spellcheck.extendedResults">false</str>
<str name="spellcheck.count">10</str>
</lst>
<arr name="components">
<str>suggest</str>
</arr>
</requestHandler>
添加到solrconfig.xml,重启jetty,访问
http://localhost:8983/solr/suggest?q=pizza&spellcheck.build=true,
spellings.txt内容:
pizza
history
结果:
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">19</int>
</lst>
<str name="command">build</str>
<lst name="spellcheck">
<lst name="suggestions">
<lst name="pizza">
<int name="numFound">1</int>
<int name="startOffset">0</int>
<int name="endOffset">5</int>
<arr name="suggestion">
<str>pizza</str>
</arr>
</lst>
<str name="collation">pizza</str>
</lst>
</lst>
</response>
在程序中访问调用solrj.jar的api.
注意:如果是删掉原来的spellcheck在重启时可能报错:
“SolrException: Unknown Search Component: spellcheck at org.apache.solr.core.SolrCore”
在solrconfig.xml中搜索spellcheck将多余的删掉再重启。
参考:
http://wiki.apache.org/solr/SpellCheckComponent
http://wiki.apache.org/solr/Suggester/