solr4.7拼音检索怎么实现

这篇文章主要介绍“solr4.7拼音检索怎么实现”,在日常操作中,相信很多人在solr4.7拼音检索怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”solr4.7拼音检索怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

拼音检索的大致思路是这样的:

 ①将需要使用拼音检索的字段汇集到一个拼音分词字段里(我的拼音分词字段使用pinyin4j+NGram做的)

 加入两个jar包:pinyin4j-2.5.0.jar、pinyinAnalyzer.jar;

schema.xml文件设置:

<field name="pinyin" type ="text_pinyin" indexed ="true" stored ="false" multiValued ="true"/>
<copyField source="name" dest="pinyin"/>
 <copyField source="author" dest="pinyin"/>
 <copyField source="region" dest="pinyin"/>
 <copyField source="theme" dest="pinyin"/> 
<!-- by michael: pinyin  -->
  <fieldType name="text_pinyin" class="solr.TextField" positionIncrementGap="0"> 
     <analyzer type="index"> 
     <tokenizer class="org.apache.lucene.analysis.cn.smart.SmartChineseSentenceTokenizerFactory"/> 
     <filter class="org.apache.lucene.analysis.cn.smart.SmartChineseWordTokenFilterFactory"/> 
     <filter class="com.shentong.search.analyzers.PinyinTransformTokenFilterFactory" minTermLenght="2" /> 
     <filter class="com.shentong.search.analyzers.PinyinNGramTokenFilterFactory" minGram="6" maxGram="20" /> 
     </analyzer> 
     <analyzer type="query"> 
     <tokenizer class="org.apache.lucene.analysis.cn.smart.SmartChineseSentenceTokenizerFactory"/> 
     <filter class="org.apache.lucene.analysis.cn.smart.SmartChineseWordTokenFilterFactory"/> 
     <filter class="com.shentong.search.analyzers.PinyinTransformTokenFilterFactory" minTermLenght="2" /> 
     <filter class="com.shentong.search.analyzers.PinyinNGramTokenFilterFactory" minGram="6" maxGram="20" /> 
     </analyzer> 
  </fieldType>

②使用solrj进行拼音检索:

/**
 * @method: testPhoneticize
 * @Description: 拼音检索
 *    ??? 也会把不符合条件的检索出来
 *    (解决方法:把汉子和相应的拼音建立同义词)
 * @return void
 *
 * @author: ChenYW
 * @date 2014-4-16 下午01:44:57
 */
    public List<Map<String, String>> phoneticize(String pinyin){
  try {
   List<Map<String, String>> list = new ArrayList<Map<String, String>>();
   
   SolrQuery query = new SolrQuery();  
   query.set("q", "pinyin:"+pinyin);//高亮查询字段  
         
         QueryResponse qr=server.query(query);//执行查询  
         SolrDocumentList dlist=qr.getResults();  
         
         System.out.println("总数:"+dlist.getNumFound());
         Map<String, String> mapRe = null;
         for(SolrDocument sd:dlist){  
             mapRe = new HashMap<String, String>();
             mapRe.put("name", sd.getFieldValue("name").toString());
             mapRe.put("content", sd.getFieldValue("content").toString().substring(0,200));
             list.add(mapRe);
         }
         
         return list;
  } catch (SolrServerException e) {
   e.printStackTrace();
  }
  return null;
 }

到此,关于“solr4.7拼音检索怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注蜗牛博客网站,小编会继续努力为大家带来更多实用的文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接