suggest 输入拼音提示中文1.0.0
?jar包见附件
?
第二步:配置 solrconfig.xml 文件,添加以下内容
<searchComponent name="spellcheck" startup="lazy"> <!--默认参数--> <lst name="defaults"> <str name="spellcheck.onlyMorePopular">true</str> <str name="spellcheck.extendedResults">false</str> <!--配置拼写检查提示结果的个数(可以根据需要适当加大)--> <str name="spellcheck.count">3</str> </lst> <arr name="last-components"> <str>spellcheck</str> </arr> <!-- <lst name="defaults"> <str name="spellcheck.dictionary">direct</str> <str name="spellcheck">on</str> <str name="spellcheck.collate">true</str> <str name="spellcheck.collateExtendedResults">true</str> </lst> <arr name="last-components"> <str>spellcheck</str> </arr> --> </requestHandler> <searchComponent name="suggest" startup="lazy"> <lst name="defaults"> <str name="spellcheck">true</str> <str name="spellcheck.dictionary">suggest</str> <!-- 这个参数告诉solr,当查询的结果数多于设定的count数时,返回点击数更多的那些 --> <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>
?第三步:增加索引
?第四步:结合Ajax 实现suggest(这里只贴出主要的solrj查询代码)
JSONObject json = new JSONObject(); //获取请求的实际URL相对于请求的serlvet的url的路径。 String requesturl = request.getPathInfo(); //检索建议 if(requesturl.indexOf("/suggest") != -1){ String data = request.getParameter("data"); if(!StringUtil.empty(data)){ String url = ConfigUtil.getStringValue("solr_url"); SolrjTool solr = SolrjTool.getinstence(); solr.init(url); //创建查询对象 SolrQuery query = new SolrQuery(); query.set("q", data); //set查询关键词 query.set("qt", "/suggest"); //set查询类型为 suggest query.set("spellcheck.count", "10"); //set 查询出的总条数 query.set("spellcheck.dictionary", "pysuggest"); query.set("spellcheck", "on"); query.set("spellcheck.build", "true"); //执行查询 QueryResponse res = solr.execseQuery(query); SpellCheckResponse spellCheckResponse = res.getSpellCheckResponse(); if (spellCheckResponse != null) { //获取Suggestion集合 List<Suggestion> suggestionList = spellCheckResponse.getSuggestions(); List<String> suggestedList = new ArrayList<String>(); for (Suggestion suggestion : suggestionList) { //获取索引库集合 List<String> suggestedWordList = suggestion.getAlternatives(); for (int i = 0; i < suggestedWordList.size(); i++) { String word = suggestedWordList.get(i); suggestedList.add(word); } } if(suggestedList.size() >0 ){ json.put("code", "1"); json.put("data", suggestedList); }else{ //没有结果 json.put("code", "1001"); } }else{ //没有结果 json.put("code", "1001"); } }else{ //没有检索项 json.put("code", "1000"); } out.println(json.toString()); }
?第五步:访问页面查询测试,如图:
?
?
?
?
转载请注明出处:http://lucien-zzy.iteye.com/admin/blogs/2008291