拼音检索"/>
solr4.9 拼音检索
1、在apache-tomcat-8.0.9\webapps\solr\WEB-INF\lib目录下,添加pinyin4j-2.5.0.jar、pinyinAnalyzer4.3.1.jar、lucene-analyzers-smartcn-4.9.0.jar jar包
2、创建用于拼音检索的字段类型:
<fieldType name="text_pinyin" class="solr.TextField" positionIncrementGap="0"> <analyzer type="index"> <tokenizer class="org.apache.lucene.analysis.smart.SmartChineseSentenceTokenizerFactory"/> <filter class="org.apache.lucene.analysis.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.smart.SmartChineseSentenceTokenizerFactory" /> <filter class="org.apache.lucene.analysis.smart.SmartChineseWordTokenFilterFactory" /> <filter class="com.shentong.search.analyzers.PinyinTransformTokenFilterFactory" minTermLenght="2" /> <filter class="com.shentong.search.analyzers.PinyinNGramTokenFilterFactory" minGram="6" maxGram="20" /> </analyzer>
</fieldType>
3、创建需要使用的拼音检索的字段
<field name="news_pinyin" type="text_pinyin" indexed="true" stored="true" multiValued="true" />
<copyField source="news_title" dest="news_pinyin" />
<copyField source="news_source" dest="news_pinyin" />
<span style="white-space:pre"> </span>/*** 拼音检索* @param pinyin 中文拼音串*/public static void pinyinQuery(String pinyin) {try {SolrServer server = SolrUtils.getInstance().getSolrServer();SolrQuery query = new SolrQuery();query.set("q", "news_pinyin:" + pinyin); QueryResponse qr = server.query(query); SolrDocumentList dlist = qr.getResults();System.out.println("总数:" + dlist.getNumFound());for (SolrDocument doc : dlist) {System.out.println("newsId: " + doc.getFieldValue("id").toString() + ", news_title: " + doc.getFieldValue("news_title") + ", news_source: " + doc.getFieldValue("news_source")+ ", url: " + doc.getFieldValue("source_url"));}} catch (SolrServerException e) {e.printStackTrace();}}
更多推荐
solr4.9 拼音检索
发布评论