IK version | ES version |
---|---|
master | 5.x -> master |
5.6.1 | 5.6.1 |
5.5.3 | 5.5.3 |
5.4.3 | 5.4.3 |
5.3.3 | 5.3.3 |
5.2.2 | 5.2.2 |
5.1.2 | 5.1.2 |
1.10.1 | 2.4.1 |
1.9.5 | 2.3.5 |
1.8.1 | 2.2.1 |
1.7.0 | 2.1.1 |
1.5.0 | 2.0.0 |
1.2.6 | 1.0.0 |
1.2.5 | 0.90.x |
1.1.3 | 0.20.x |
1.0.0 | 0.16.2 -> 0.19.0 |
一、安裝。
-
去github下下載對應的ik版本: https://github.com/medcl/elasticsearch-analysis-ik/releases,並解壓到對應的安裝路徑:../elasticsearch-5.5.3/plugin/
-
使用elasticsearch-plugin插件來安裝 ( 版本 > v5.5.1 ):
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.1/elasticsearch-analysis-ik-5.6.1.zip
二、重啟 elasticsearch
三、安裝配置
IKAnalyzer.cfg.xml
can be located at {conf}/analysis-ik/config/IKAnalyzer.cfg.xml
or {plugins}/elasticsearch-analysis-ik-*/config/IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>IK Analyzer 擴展配置</comment> <!--用戶可以在這里配置自己的擴展字典 --> <entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry> <!--用戶可以在這里配置自己的擴展停止詞字典--> <entry key="ext_stopwords">custom/ext_stopword.dic</entry> <!--用戶可以在這里配置遠程擴展字典 --> <entry key="remote_ext_dict">location</entry> <!--用戶可以在這里配置遠程擴展停止詞字典--> <entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry> </properties>
五、熱更新 IK 分詞使用方法
目前該插件支持熱更新 IK 分詞,通過上文在 IK 配置文件中提到的如下配置
<!--用戶可以在這里配置遠程擴展字典 --> <entry key="remote_ext_dict">location</entry> <!--用戶可以在這里配置遠程擴展停止詞字典--> <entry key="remote_ext_stopwords">location</entry>
其中 location
是指一個 url,比如 http://yoursite.com/getCustomDict
,該請求只需滿足以下兩點即可完成分詞熱更新。
-
該 http 請求需要返回兩個頭部(header),一個是
Last-Modified
,一個是ETag
,這兩者都是字符串類型,只要有一個發生變化,該插件就會去抓取新的分詞進而更新詞庫。 -
該 http 請求返回的內容格式是一行一個分詞,換行符用
\n
即可。
滿足上面兩點要求就可以實現熱更新分詞了,不需要重啟 ES 實例。
可以將需自動更新的熱詞放在一個 UTF-8 編碼的 .txt 文件里,放在 nginx 或其他簡易 http server 下,當 .txt 文件修改時,http server 會在客戶端請求該文件時自動返回相應的 Last-Modified 和 ETag。可以另外做一個工具來從業務系統提取相關詞匯,並更新這個 .txt 文件。
六、常見問題
1.自定義詞典為什么沒有生效?
請確保你的擴展詞典的文本格式為 UTF8 編碼
2.如何手動安裝?
git clone https://github.com/medcl/elasticsearch-analysis-ik
cd elasticsearch-analysis-ik git checkout tags/{version} mvn clean mvn compile mvn package
拷貝和解壓release下的文件: #{project_path}/elasticsearch-analysis-ik/target/releases/elasticsearch-analysis-ik-*.zip 到你的 elasticsearch 插件目錄, 如: plugins/ik 重啟elasticsearch
另一種方法是下載源碼包:
1)、到github網站下載源代碼,網站地址為:https://github.com/medcl/elasticsearch-analysis-ik
右側下方有一個按鈕“Download ZIP",點擊下載源代碼elasticsearch-analysis-ik-master.zip。
2)、解壓文件elasticsearch-analysis-ik-master.zip,進入下載目錄,執行命令:
3)、將解壓目錄文件中config/ik文件夾復制到ES安裝目錄config文件夾下。
4)、因為是源代碼,此處需要使用maven打包,進入解壓文件夾中,執行命令:
6)、在ES的配置文件config/elasticsearch.yml中增加ik的配置,在最后增加:
7)、重新啟動elasticsearch服務,這樣就完成配置了。
3.分詞測試失敗 請在某個索引下調用analyze接口測試,而不是直接調用analyze接口 如:http://localhost:9200/your_index/_analyze?text=中華人民共和國MN&tokenizer=my_ik, 版本5.0以后的將使用analyzer=ik_max_word,如: curl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_smart' -d '聯想是全球最大的筆記本廠商'。
4. ik_max_word 和 ik_smart 什么區別?
ik_max_word: 會將文本做最細粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,中華人民,中華,華人,人民共和國,人民,人,民,共和國,共和,和,國國,國歌”,會窮盡各種可能的組合;
ik_smart: 會做最粗粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,國歌”。
【references】
【1】https://github.com/medcl/elasticsearch-analysis-ik
【2】http://blog.csdn.net/jam00/article/details/52983056