1、介紹
elasticseach默認所有分詞解析器對中文都不友好,開發建議使用Ik分詞;
IK Analyzer是一個開源的,基於java語言開發的輕量級的中文分詞工具包。從2006年12月推出1.0版開始, IKAnalyzer已經推出了3個大版本。最初,它是以開源項目Luence爲應用主體的,結合詞典分詞和文法分析算法的中文分詞組件。新版本的IK Analyzer 3.0則發展爲面向Java的公用分詞組件,獨立於Lucene項目,同時提供了對Lucene的默認優化實現。
2、安裝
安裝ik分詞插件之前,前提是已經安裝了es(安裝步驟可參考:https://blog.csdn.net/zhanyu1/article/details/88079626),安裝的ik版本和es版本也要對應, 版本對應關係詳見ik主頁:https://github.com/medcl/elasticsearch-analysis-ik
ik主頁中有兩種安裝方式
-
optional 1 - download pre-build package from here: https://github.com/medcl/elasticsearch-analysis-ik/releases
create plugin folder cd your-es-root/plugins/ && mkdir ik
unzip plugin to folder your-es-root/plugins/ik
-
optional 2 - use elasticsearch-plugin to install ( supported from version v5.5.1 ):
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
我們使用第一種,按照上述說明的步驟,直接下載解壓即可。
安裝後,plugins下,會有一個analysis-ik目錄,目錄下有jar包和配置文件,然後我們重啓es;
接着再安裝下head插件,用來測試ik分詞的效果,安裝步驟可參考:https://blog.csdn.net/zhanyu1/article/details/88083290
3、測試
ik分詞有兩種ik_smart , ik_max_word,強烈建議用後者ik_max_word,分詞結果很多,提高命中率;
創建index索引 put http://localhost:9200/index
創建映射
post http://localhost:9200/index/fulltext/_mapping
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
索引幾個文檔
post http://localhost:9200/index/fulltext/1
{"content":"美國留給伊拉克的是個爛攤子嗎"}
post http://localhost:9200/index/fulltext/2
{"content":"公安部:各地校車將享最高路權"}
post http://localhost:9200/index/fulltext/3
{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
post http://localhost:9200/index/fulltext/4
{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}
然後我們來搜索測試:
post http://localhost:9200/index/fulltext/_search/
{
"query" : { "match" : { "content" : "中國嫌犯" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}