suggest 输入拼音提示中文1.0.0

 

转载请注明出处:http://lucien-zzy.iteye.com/admin/blogs/2008291

 

在完成solr的搭建后(solr搭建可以参考这篇:http://lucien-zzy.iteye.com/blog/2002087)

 

我们来看看如何实现suggest输入拼音提示中文的功能:

 

这里我是基于http://lucien-zzy.iteye.com/blog/2002087 这篇来搭建的。

 

第一步:将这俩jar包放入 F:\winsolr\solr_home\solr\collection1\contrib\extraction\lib 中



 jar包见附件

 

第二步:配置 solrconfig.xml 文件,添加以下内容

<searchComponent name="spellcheck" class="solr.SpellCheckComponent">
     <str name="queryAnalyzerFieldType">text_spell</str> 
     <lst name="spellchecker"> 

           <str name="name">default</str>  
           <!--这里指明需要根据哪个字段的索引为依据进行拼写检查。现配置 名为 name 的字段-->  
           <str name="field">text</str>  
           <!--拼写检查索引的目录-->  
           <str name="spellcheckIndexDir">spellchecker</str>  
           <!--当commit的时候,对拼写检查索引进行构建。(只有构建后,拼写检查才有效果)-->  
           <!--当然,也可以选择在optimize的时候,进行构建。那么只需要将"buildOnCommint"换为 "buildOnOptimize"-->  
           <str name="buildOnCommit">true</str> 

         <str name="classname">solr.FileBasedSpellChecker</str>
         <str name="sourceLocation">spellings.txt</str>
         <str name="characterEncoding">UTF-8</str>
        
<!--
       <str name="name">direct</str> 
       <str name="field">text</str> 
       <str name="classname">solr.DirectSolrSpellChecker</str> 
       <str name="distanceMeasure">internal</str> 
       <float name="accuracy">0.5</float> 
       <int name="maxEdits">2</int> 
       <int name="minPrefix">1</int> 
       <int name="maxInspections">5</int> 
       <int name="minQueryLength">2</int> 
       <float name="maxQueryFrequency">0.01</float> 
-->
</lst> 
  </searchComponent> 

  <requestHandler name="/spell" class="solr.SearchHandler" 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" class="solr.SpellCheckComponent">
    <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">text</str>
        <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>
	
	<!--用于输入拼音提示功能-->
	<lst name="spellchecker">
        <str name="name">pysuggest</str>
        <str name="classname">shentong.tsearch.spelling.suggest.Suggester</str>
        <str name="lookupImpl">shentong.tsearch.spelling.suggest.py.PYLookup</str>
        <str name="field">text</str>
        <float name="threshold">0.0001</float> 
	<str name="pySuggestIndexDir">suggestIndex</str>
        <str name="comparatorClass">freq</str>
        <str name="buildOnOptimize">true</str>
        <!--<str name="buildOnCommit">true</str>-->
    </lst>
</searchComponent>
<requestHandler  name="/suggest" class="solr.SearchHandler" 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值