Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文、拼音、同义词搜索
一、簡介
有時候我們需要在項目中支持中文 和 拼音的搜索。采用ik分詞來做中文分詞是目前比好的方式。至于拼音分詞可以采用lc-pinyin,雖然lc-pinyin能很好的解決首字母和全拼的搜索,但是在中文分詞上卻是不支持的,lc-pinyin只能把中文拆成單字來處理。要是能把IK分詞和lc-pinyin分詞結合那該多好,不是么?本文主要介紹如何把ik和lc-pinyin結合起來使用,使我們的搜索既支持中文搜索,又支持拼音搜索。
環境:elasticsearch1.4.5, elasticsearch-analysis-lc-pinyin1.4.5,elasticsearch-analysis-ik1.3.0
二、配置lc-pinyin和ik分詞器
1. 首先需要安裝lc-pinyin和ik分詞,這里就不再講如何安裝,不會的童鞋參考:http://blog.csdn.net/chennanymy/article/details/52336368
2. ?安裝好lc-pinyin和ik分詞插件后就可以配置分詞器額,打開 config/elasticsearch.yml文件,在末尾加上下面的配置
index:analysis:analyzer:ik_max_word:type: ikuse_smart: falseik_smart:type: ikuse_smart: trueik_syno:tokenizer: ikfilter: [ik_synonym_filter]ik_syno_smart:tokenizer: ik_tk_smartfilter: [ik_synonym_filter]lc:alias: [lc_analyzer]type: org.elasticsearch.index.analysis.LcPinyinAnalyzerProviderlc_index:type: lcanalysisMode: indexlc_search:type: lcanalysisMode: searchtokenizer:ik_tk_smart:type: ikuse_smart: truefilter:ik_synonym_filter:type: synonymsynonyms_path: analysis/synonym.txt上面的配置定義了一個同義詞過濾器“ik_synonym_filter” 并指定了一個同義詞文件,該文件目錄結構如下
對同義詞不熟悉的同學,可以參考官網教程:https://www.elastic.co/guide/en/elasticsearch/reference/1.5/analysis-synonym-tokenfilter.html
到這里分詞器就配置完成了。
三、測試同義詞
下面我們就來測試一下同義詞是否有效,打開同義詞配置文件在里面加入兩行
第一行同義詞表示當要索引的文字中包含了 蠟燭、園丁、師傅、先生 都會被轉換成 老師 索引到文檔中
第二行同義詞表示當出現中文、漢語、漢字中任何一個詞的時候都把上述3個詞索引到文檔中。所以這種方式是比較費索引的
上圖可以看到我們配置的同義詞生效了。
四、搜索測試
首先創建索引和mapping,這里我們采用multi_field來做針對同一字段設置不同的分詞器
content字段采用拼音分詞,content.cn采用ik分詞
curl -XPUT http://localhost:9200/index curl -XPOST http://localhost:9200/index/fulltext/_mapping -d' {"fulltext": {"properties": {"content": {"type": "string","index_analyzer": "lc_index","search_analyzer": "lc_search","fields": {"cn": {"type": "string","index_analyzer": "ik_syno","search_analyzer": "ik_syno_smart"}}}}} }'然后索引幾條數據 curl -XPOST http://localhost:9200/index/fulltext/1 -d' {"content":"湖北工業大學"} 'curl -XPOST http://localhost:9200/index/fulltext/2 -d' {"content":"華中科技大學"} 'curl -XPOST http://localhost:9200/index/fulltext/3 -d' {"content":"武漢大學"} 'curl -XPOST http://localhost:9200/index/fulltext/4 -d' {"content":"武漢理工大學"} ' curl -XPOST http://localhost:9200/index/fulltext/5 -d' {"content":"香港中文大學"} '
@Testpublic void testMultiMatch() {final String index = "index";final String type = "fulltext";SearchRequestBuilder requestBuilder = elasticIndexOperateHelper.getClient().prepareSearch(index).setTypes(type);String input = "中文大學";QueryBuilder pinyinSearch = QueryBuilders.matchQuery("content", input).type(MatchQueryBuilder.Type.PHRASE).analyzer("lc_search").boost(4).zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE);QueryBuilder chineseSearch = QueryBuilders.matchQuery("content.cn", input).type(MatchQueryBuilder.Type.BOOLEAN).analyzer("ik_syno_smart").boost(8).zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE);QueryBuilder mixQueryBuilder = QueryBuilders.boolQuery().should(pinyinSearch).should(chineseSearch).minimumNumberShouldMatch(1);requestBuilder = requestBuilder.setQuery(mixQueryBuilder).setHighlighterPreTags("<tag1>", "<tag2>").setHighlighterPostTags("</tag1>", "</tag2>").addHighlightedField("content").addHighlightedField("content.cn").setHighlighterRequireFieldMatch(true);SearchResponse response = requestBuilder.execute().actionGet();System.out.println(requestBuilder);System.out.println(response);}
返回結果 {"took" : 734,"timed_out" : false,"_shards" : {"total" : 2,"successful" : 2,"failed" : 0},"hits" : {"total" : 5,"max_score" : 1.7481089,"hits" : [ {"_index" : "index","_type" : "fulltext","_id" : "5","_score" : 1.7481089,"_source": {"content":"武漢中文大學"} ,"highlight" : {"content.cn" : [ "武漢<tag1>中文</tag1><tag1>大學</tag1>" ],"content" : [ "武漢<tag1>中</tag1><tag1>文</tag1><tag1>大</tag1><tag1>學</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "3","_score" : 0.014395926,"_source": {"content":"武漢大學"} ,"highlight" : {"content.cn" : [ "武漢<tag1>大學</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "1","_score" : 0.009597284,"_source": {"content":"湖北工業大學"} ,"highlight" : {"content.cn" : [ "湖北工業<tag1>大學</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "2","_score" : 0.0077423635,"_source": {"content":"華中科技大學"} ,"highlight" : {"content.cn" : [ "華中科技<tag1>大學</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "4","_score" : 0.0061938907,"_source": {"content":"武漢理工大學"} ,"highlight" : {"content.cn" : [ "武漢理工<tag1>大學</tag1>" ]}} ]} }
搜索:gongyedaxue
{"took" : 105,"timed_out" : false,"_shards" : {"total" : 2,"successful" : 2,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.9900317,"hits" : [ {"_index" : "index","_type" : "fulltext","_id" : "1","_score" : 0.9900317,"_source": {"content":"湖北工業大學"} ,"highlight" : {"content" : [ "湖北<tag1>工</tag1><tag1>業</tag1><tag1>大</tag1><tag1>學</tag1>" ]}} ]} }轉載于:https://www.cnblogs.com/chennanlcy/p/6591792.html
總結
以上是生活随笔為你收集整理的Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文、拼音、同义词搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vijos1451圆环取数[环形DP|区
- 下一篇: 学习SpringMVC——说说视图解析器