solr搜索建议配置

来源:互联网 发布:中国硕士论文数据库 编辑:程序博客网 时间:2024/04/29 20:47
<searchComponent class="solr.SpellCheckComponent" name="suggest">  
        <str name="queryAnalyzerFieldType">string</str>  
        <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>  
            <str name="field">userName</str>  
            <!-- the indexed field to derive suggestions from -->  
            <float name="threshold">0.0001</float>  
            <str name="spellcheckIndexDir">spellchecker</str>  
            <str name="comparatorClass">freq</str>  
            <str name="buildOnOptimize">true</str>  
            <str name="buildOnCommit">true</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.extendedResults">false</str>  
            <str name="spellcheck.count">10</str>  
            <str name="spellcheck.collate">true</str>  
        </lst>  
        <arr name="components">  
            <str>suggest</str>  
        </arr>  

   </requestHandler>


1、在apache-tomcat-7.0.42\solr\db\conf下面的solrconfig.xml文件配置一上内容。

2、<str name="queryAnalyzerFieldType">string</str>  为搜索词的字段类型,   <str name="field">userName</str> 在schema.xml配置的搜索字段名称,要和schema.xml

里面的对应不能有错。


java代码:


public List<String> suggest(String word) throws Exception {
List<String> wordList = new ArrayList<String>();
SolrClient client = getSolrClient();
SolrQuery query = new SolrQuery("userName:" + word);
query.set("qt", "/suggest");// 请求到suggest中
QueryResponse rsp = client.query(query);
SpellCheckResponse re = rsp.getSpellCheckResponse();// 获取拼写检查的结果集
if (re != null) {
for (Suggestion s : re.getSuggestions()) {
List<String> list = s.getAlternatives();// 获取所有 的检索词
for (String spellWord : list) {
wordList.add(spellWord);
}
}
}
return wordList;
}

1 0
原创粉丝点击