由於elasticsearch基於lucene,所以天然地就多了許多lucene上的中文分詞的支持,比如 IK, Paoding, MMSEG4J等lucene中文分詞原理上都能在elasticsearch上使用。當然前提是有elasticsearch的插件。 至於插件怎么開發,這里有一片文章介紹:
http://log.medcl.net/item/2011/07/diving-into-elasticsearch-3-custom-analysis-plugin/
暫時還沒時間看,留在以后仔細研究, 這里只記錄本人使用medcl提供的IK分詞插件的集成步驟。
安裝步驟:
1、到github網站下載源代碼,網站地址為:https://github.com/medcl/elasticsearch-analysis-ik
右側下方有一個按鈕“Download ZIP",點擊下載源代碼elasticsearch-analysis-ik-master.zip。
2、解壓文件elasticsearch-analysis-ik-master.zip,進入下載目錄,執行命令:
unzip elasticsearch-analysis-ik-master.zip
3、因為是源代碼,此處需要使用maven打包,進入解壓文件夾中,執行命令:
4、將打包后,得到的目錄文件target/releases下的elasticsearch-analysis-ik-1.9.4.zip復制到ES安裝目錄的plugins/analysis-ik目錄下。
5、在plugins/analysis-ik目錄下解壓elasticsearch-analysis-ik-1.9.4.zip
6、在ES的配置文件elasticsearch.yml中增加ik的配置,在最后增加:
index.analysis.analyzer.ik.type: "ik"
7、重新啟動elasticsearch服務,這樣就完成配置了,收入命令:
curl -XPOST "http://localhost:9200/_analyze?analyzer=ik&pretty=true&text=helloworld,中華人民共和國"
測試結果如下:
{ "tokens" : [ { "token" : "helloworld", "start_offset" : 0, "end_offset" : 10, "type" : "ENGLISH", "position" : 0 }, { "token" : "中華人民共和國", "start_offset" : 11, "end_offset" : 18, "type" : "CN_WORD", "position" : 1 }, { "token" : "中華人民", "start_offset" : 11, "end_offset" : 15, "type" : "CN_WORD", "position" : 2 }, { "token" : "中華", "start_offset" : 11, "end_offset" : 13, "type" : "CN_WORD", "position" : 3 }, { "token" : "華人", "start_offset" : 12, "end_offset" : 14, "type" : "CN_WORD", "position" : 4 }, { "token" : "人民共和國", "start_offset" : 13, "end_offset" : 18, "type" : "CN_WORD", "position" : 5 }, { "token" : "人民", "start_offset" : 13, "end_offset" : 15, "type" : "CN_WORD", "position" : 6 }, { "token" : "共和國", "start_offset" : 15, "end_offset" : 18, "type" : "CN_WORD", "position" : 7 }, { "token" : "共和", "start_offset" : 15, "end_offset" : 17, "type" : "CN_WORD", "position" : 8 }, { "token" : "國", "start_offset" : 17, "end_offset" : 18, "type" : "CN_CHAR", "position" : 9 } ] }
注意點:
本人繞了很多彎路,網上很多都不行,總結:
一、maven一定要編譯,因為elasticsearch和ik各個版本不同,對應編譯生成的文件就不同,所以想引用elasticsearch-rtm包的朋友,一定要注意區分。
二、我是通過rpm安裝elasticsearch,事實證明字典config目錄,可以在plugins目錄下,和插件unzip放在一起
參考資料: