检索建议目前是各大搜索的标配应用,主要作用是避免用户输入错误的搜索词,同时将用户引导到相应的关键词搜索上。Solr内置了检索建议功能,他在Solr里叫做Suggest模块。该模块可选择基于提示词文本做检索建议,还支持针对索引的某个字段建立索引词库来检索建议。在诸多文档中都推荐使用基于索引来做做检索建议,因此我们目前的实现也是采取该方案。
现在我们开始配置Suggest模块,首先在solrconfig.xml文件中配置Suggest依赖的SpellCheck模块,然后再配置Suggest模块,所以这两个都需要配置。
<searchComponent name = "suggest" class = "solr.SpellCheckComponent">
</searchComponent>
<requestHandler name = "/suggest" class = "solr.SearchHandler" startup = "lazy">
<lst name = "defaults">
<str name = "spellcheck">true</str>
<str name = "spellcheck.dictionary">suggest</str>
<str name = "spellcheck.onlyMorePopular">true</str>
<str name = "spellcheck.extendedResults">false</str>
<str name = "spellcheck.count">10</str>
<str name = "spellcheck.collate">true</str>
</lst>
<arr name = "compontents">
<str>suggest</str>
</arr>
</requestHandler>
配置完成之后,我们进行一下测试,重启Solr后,访问如下链接:
http://localhost:8080/solr/collection1/suggest?wt=xml&indent=true&spellcheck=true&spellcheck.q=%E4%B8%AD%E5%9B%BD
<?xml version = "1,0" encoding = "UTF-8"?>
<response>
<lst name = "responseHeader">
<int name= "status">0</int>
<int name= "QTime">4</int>
<lst>
<lst name = "spellcheck">
<lst name = "suggestions">
<lst name = "中国">
<int name= "numFound">4</int>
<int name= "startOffset">0</int>
<int name= "endOffset">2</int>
<arr name = "suggestion">
<str>中国队</str>
<str>中国证监会</str>
<str>中国足协</str>
<str>中国银行</str>
</arr>
</lst>
</lst>
</lst>
</response>
使用Solr时也同样加入参数就可以。
SolrQuery query = new SolrQuery();
query.set("q" , token);
query.set("qt" , "/suggest");
query.set("spellcheck.count" , "10");
QueryResponse response = server.query(query);
SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
if (spellCheckResponse != null ) {
List<SpellCheckResponse.Suggestion> suggestionList = spellCheckResponse.getSuggestions();
for (SpellCheckResponse.Suggestion suggestion : suggestionList) {
List<String> suggestedWordList = suggestion.getAlternatives();
for(int i = 0 ; i < suggestedWordList.size(); i ++) {
String word = suggestedWordList.get(i);
}
}
return results;
}
通过threshold参数来限制一些不常用的词不出现在智能提示列表中,当这个值设置过大时,可能导致结果太少,需要引起注意。目前主要存在的问题是使用freq排序算法,返回的结果完全基于索引中字符的出现次数,没有兼顾用户搜索词语的频率,因此无法将一些热门词排在更靠前的位置。这块可定制SuggestWordScoreComparator来实现,目前还没有着手做这件事情。
因篇幅问题不能全部显示,请点此查看更多更全内容