Elasticsearch默認提供的分詞器,會把每個漢字分開,而不是我們想要的根據關鍵詞來分詞。例如:
- curl -XPOST "http://localhost:9200/userinfo/_analyze?analyzer=standard&pretty=true&text=我是中國人"
- {
- tokens: [
- {
- token: text
- start_offset: 2
- end_offset: 6
- type: <ALPHANUM>
- position: 1
- }
- {
- token: 我
- start_offset: 9
- end_offset: 10
- type: <IDEOGRAPHIC>
- position: 2
- }
- {
- token: 是
- start_offset: 10
- end_offset: 11
- type: <IDEOGRAPHIC>
- position: 3
- }
- {
- token: 中
- start_offset: 11
- end_offset: 12
- type: <IDEOGRAPHIC>
- position: 4
- }
- {
- token: 國
- start_offset: 12
- end_offset: 13
- type: <IDEOGRAPHIC>
- position: 5
- }
- {
- token: 人
- start_offset: 13
- end_offset: 14
- type: <IDEOGRAPHIC>
- position: 6
- }
- ]
- }
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,進入下載目錄,執行命令:
- unzip elasticsearch-analysis-ik-master.zip
3、將解壓目錄文件中config/ik文件夾複製到ES安裝目錄config文件夾下。
4、因爲是源代碼,此處需要使用maven打包,進入解壓文件夾中,執行命令:- mvn clean package
6、在ES的配置文件config/elasticsearch.yml中增加ik的配置,在最後增加:
- index:
- analysis:
- analyzer:
- ik:
- alias: [ik_analyzer]
- type: org.elasticsearch.index.analysis.IkAnalyzerProvider
- ik_max_word:
- type: ik
- use_smart: false
- ik_smart:
- type: ik
- use_smart: true
- index.analysis.analyzer.ik.type : “ik”
- curl -XPOST "http://localhost:9200/userinfo/_analyze?analyzer=ik&pretty=true&text=我是中國人"
- {
- tokens: [
- {
- token: text
- start_offset: 2
- end_offset: 6
- type: ENGLISH
- position: 1
- }
- {
- token: 我
- start_offset: 9
- end_offset: 10
- type: CN_CHAR
- position: 2
- }
- {
- token: 中國人
- start_offset: 11
- end_offset: 14
- type: CN_WORD
- position: 3
- }
- {
- token: 中國
- start_offset: 11
- end_offset: 13
- type: CN_WORD
- position: 4
- }
- {
- token: 國人
- start_offset: 12
- end_offset: 14
- type: CN_WORD
- position: 5
- }
- ]
- }
1、ES安裝插件本來使用使用命令plugin來完成,但是我本機安裝ik時一直不成功,所以就使用源代碼打包安裝了。
2、自定義詞庫的方式,請參考 https://github.com/medcl/elasticsearch-analysis-ik