Elasticsearch安裝中文分詞插件ik

Elasticsearch默認提供的分詞器,會把每個漢字分開,而不是我們想要的根據關鍵詞來分詞。例如:

[html] view plaincopy
  1. curl -XPOST  "http://localhost:9200/userinfo/_analyze?analyzer=standard&pretty=true&text=我是中國人"  
我們會得到這樣的結果:

[html] view plaincopy
  1. {  
  2. tokens: [  
  3. {  
  4. token: text  
  5. start_offset: 2  
  6. end_offset: 6  
  7. type: <ALPHANUM>  
  8. position: 1  
  9. }  
  10. {  
  11. token: 我  
  12. start_offset: 9  
  13. end_offset: 10  
  14. type: <IDEOGRAPHIC>  
  15. position: 2  
  16. }  
  17. {  
  18. token: 是  
  19. start_offset: 10  
  20. end_offset: 11  
  21. type: <IDEOGRAPHIC>  
  22. position: 3  
  23. }  
  24. {  
  25. token: 中  
  26. start_offset: 11  
  27. end_offset: 12  
  28. type: <IDEOGRAPHIC>  
  29. position: 4  
  30. }  
  31. {  
  32. token: 國  
  33. start_offset: 12  
  34. end_offset: 13  
  35. type: <IDEOGRAPHIC>  
  36. position: 5  
  37. }  
  38. {  
  39. token: 人  
  40. start_offset: 13  
  41. end_offset: 14  
  42. type: <IDEOGRAPHIC>  
  43. position: 6  
  44. }  
  45. ]  
  46. }  
正常情況下,這不是我們想要的結果,比如我們更希望 “中國人”,“中國”,“我”這樣的分詞,這樣我們就需要安裝中文分詞插件,ik就是實現這個功能的。

elasticsearch-analysis-ik 是一款中文的分詞插件,支持自定義詞庫。

安裝步驟:

1、到github網站下載源代碼,網站地址爲:https://github.com/medcl/elasticsearch-analysis-ik

右側下方有一個按鈕“Download ZIP",點擊下載源代碼elasticsearch-analysis-ik-master.zip。

2、解壓文件elasticsearch-analysis-ik-master.zip,進入下載目錄,執行命令:

[html] view plaincopy
  1. unzip elasticsearch-analysis-ik-master.zip  

3、將解壓目錄文件中config/ik文件夾複製到ES安裝目錄config文件夾下。

4、因爲是源代碼,此處需要使用maven打包,進入解壓文件夾中,執行命令:
[html] view plaincopy
  1. mvn clean package  
5、將打包得到的jar文件elasticsearch-analysis-ik-1.2.8-sources.jar複製到ES安裝目錄的lib目錄下。

6、在ES的配置文件config/elasticsearch.yml中增加ik的配置,在最後增加:

[html] view plaincopy
  1. index:  
  2.   analysis:                     
  3.     analyzer:        
  4.       ik:  
  5.           alias: [ik_analyzer]  
  6.           type: org.elasticsearch.index.analysis.IkAnalyzerProvider  
  7.       ik_max_word:  
  8.           type: ik  
  9.           use_smart: false  
  10.       ik_smart:  
  11.           type: ik  
  12.           use_smart: true  
[html] view plaincopy
  1. index.analysis.analyzer.ik.type : “ik”  
7、重新啓動elasticsearch服務,這樣就完成配置了,收入命令:
[html] view plaincopy
  1. curl -XPOST  "http://localhost:9200/userinfo/_analyze?analyzer=ik&pretty=true&text=我是中國人"  
測試結果如下:
[html] view plaincopy
  1. {  
  2. tokens: [  
  3. {  
  4. token: text  
  5. start_offset: 2  
  6. end_offset: 6  
  7. type: ENGLISH  
  8. position: 1  
  9. }  
  10. {  
  11. token: 我  
  12. start_offset: 9  
  13. end_offset: 10  
  14. type: CN_CHAR  
  15. position: 2  
  16. }  
  17. {  
  18. token: 中國人  
  19. start_offset: 11  
  20. end_offset: 14  
  21. type: CN_WORD  
  22. position: 3  
  23. }  
  24. {  
  25. token: 中國  
  26. start_offset: 11  
  27. end_offset: 13  
  28. type: CN_WORD  
  29. position: 4  
  30. }  
  31. {  
  32. token: 國人  
  33. start_offset: 12  
  34. end_offset: 14  
  35. type: CN_WORD  
  36. position: 5  
  37. }  
  38. ]  
  39. }  
說明:

1、ES安裝插件本來使用使用命令plugin來完成,但是我本機安裝ik時一直不成功,所以就使用源代碼打包安裝了。

2、自定義詞庫的方式,請參考 https://github.com/medcl/elasticsearch-analysis-ik

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章