Solr6 Suggest(智能提示)

  1、介绍

    Solr从1.4开始便提供了检查建议,检索建议目前是各大搜索的标配应用,主要作用是避免用户输入错误的搜索词,同时将用户引导到相应的关键词搜索上。通常,我们将其称为搜索联想。

  其效果如图所示。在Solr中,实现该功能的模块叫做suggest。

    

  2、配置

    配置manage-schema

<fieldType name="text_mmseg4j_simple" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple"/>
    </analyzer>
  </fieldType>
 <field name="text" type="text_mmseg4j_simple" termVectors="true" indexed="true" stored="true"/>
 <field name="suggestion" type="text_mmseg4j_simple" indexed="true" stored="true" termVectors="true" multiValued="true" />
  <copyField source="text" dest="suggestion" />

  配置solrconfig.xml

 <searchComponent class="solr.SpellCheckComponent" name="suggest">
    <str name="queryAnalyzerFieldType">text_spell</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">suggestion</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>

  <queryConverter name="phraseQueryConverter" class="org.apache.solr.spelling.SpellingQueryConverter"/>

  3、重启solr,索引数据

     

  4、效果预览

    

  5、java代码查询

 private static void getSuggest(String message) throws SolrServerException, IOException{
        SolrQuery params = new SolrQuery();
        params.set("qt", "/suggest");
//        params.setQuery("中国");
        params.setQuery(message);
        QueryResponse response = null;
        response = solr.query(params);
        String suggest = response.toString();
        suggest = suggest.replace("=",":");

        System.out.println(suggest);
        JSONObject obj = new JSONObject(suggest);
        Stack<JSONObject> stObj = new Stack<JSONObject>();
        stObj.push(obj);
        Map<String, Object> resultMap =new HashMap<String, Object>();
        JsonToMap(stObj,resultMap);
        if(resultMap.containsKey("suggestion")){
            System.out.println("suggestion:"+resultMap.get("suggestion"));
        }
    }

    /**
     * @Author:sks
     * @Description:把json对象数据存储在map以键值对的形式存储,只存储叶节点
     * @Date:
     */
    private static void JsonToMap(Stack<JSONObject> stObj,Map<String, Object> resultMap)  throws SolrServerException, IOException{

        if(stObj == null && stObj.pop() == null){
            return ;
        }
        JSONObject json = stObj.pop();
        Iterator it = json.keys();
        while(it.hasNext()){
            String key = (String) it.next();
            //得到value的值
            Object value = json.get(key);
            //System.out.println(value);
            if(value instanceof JSONObject)
            {
                stObj.push((JSONObject)value);
                //递归遍历
                JsonToMap(stObj,resultMap);
            }
            else {
                resultMap.put(key, value);
            }
        }
    }
原文地址:https://www.cnblogs.com/shaosks/p/8044055.html