一、ik中文分詞器的安裝,兩種方法:
到官網(wǎng)下載沒有編譯的版本自己編譯好了進行安裝。官方地址:https://github.com/medcl/elasticsearch-analysis-ik。不過這種方法稍微麻煩一點,我們可以用第二種方法:直接下載官方已經(jīng)編譯好的版本。
- ①下載編譯好的安裝包:https://github.com/medcl/elasticsearch-analysis-ik/releases。注意下載版本要對應(yīng)。
②下載好了之后解壓,將解壓后的文件夾放在elasticsearch目錄下的plugins目錄下,并重命名為analysis-ik
③將analysis-ik下config目錄整個拷貝到elasticsearch目錄下的config目錄下,并重命名為ik
④ 重啟elasticsearch
二、分詞器的使用
1、ik帶有兩個分詞器:
- ik_max_word :會將文本做最細(xì)粒度的拆分;盡可能多的拆分出詞語
- ik_smart:會做最粗粒度的拆分;已被分出的詞語將不會再次被其它詞語占有
看下邊的例子就會明白他們的區(qū)別了:
ik_smart:
在終端輸入以下語句:
curl -XGET 'http://192.168.198.223:9200/_analyze?pretty&analyzer=ik_smart' -d '五星紅旗迎風(fēng)飄揚'
返回如下內(nèi)容:
{
"tokens" : [
{
"token" : "五星紅旗",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "迎風(fēng)",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "飄揚",
"start_offset" : 6,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 2
}
]
}
ik_max_word:
在終端輸入以下內(nèi)容:
curl -XGET 'http://192.168.198.223:9200/_analyze?pretty&analyzer=ik_max_word' -d '五星紅旗迎風(fēng)飄揚'
返回如下內(nèi)容:
{
"tokens" : [
{
"token" : "五星紅旗",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "五星",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "五",
"start_offset" : 0,
"end_offset" : 1,
"type" : "TYPE_CNUM",
"position" : 2
},
{
"token" : "星",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "紅旗",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "迎風(fēng)",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "飄揚",
"start_offset" : 6,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "飄",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "揚",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 8
}
]
}
2、: