版本說(shuō)明
es
:2.4.3
ik
:1.10.3
下載ik分詞
github地址
不同的es有不同的ik版本對(duì)應(yīng),可在releases
找到對(duì)應(yīng)的版本名秀,直接下載zip文件即可励负。
解壓
在es目錄下的plugins在創(chuàng)建ik
目錄,把下載ik的zip包所有文件解壓進(jìn)去泰偿。
配置成默認(rèn)分詞器
進(jìn)去es的config目錄熄守,編輯elasticsearch.yml
,在空白地方加上index.analysis.analyzer.default.type : "ik"
即可耗跛。
測(cè)試
首先創(chuàng)建索引裕照、類(lèi)型,對(duì)應(yīng)string類(lèi)型的field不指定分詞器
{
"properties": {
"content": {
"type": "string"
},
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"headImg": {
"type": "string",
"index": "not_analyzed"
},
"isClient": {
"type": "integer"
},
"length": {
"type": "integer"
},
"msgType": {
"type": "integer"
},
"nickname": {
"type": "string"
},
"roomId": {
"type": "string",
"index": "not_analyzed"
},
"shareMsg": {
"properties": {
"appid": {
"type": "string",
"index": "not_analyzed"
},
"coverUrl": {
"type": "string",
"index": "not_analyzed"
},
"des": {
"type": "string"
},
"sourceId": {
"type": "string",
"index": "not_analyzed"
},
"sourceName": {
"type": "string"
},
"title": {
"type": "string"
},
"type": {
"type": "integer"
},
"url": {
"type": "string",
"index": "not_analyzed"
}
}
},
"wxUserId": {
"type": "string",
"index": "not_analyzed"
}
}
}
請(qǐng)求localhost:9200/xxx/_analyze?field=nickname&text=我愛(ài)中國(guó)
调塌,xxx
為index的名稱(chēng),field
為任意類(lèi)型為string的字段晋南,text
為希望分詞的內(nèi)容,得到
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "愛(ài)",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "中國(guó)",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 2
}
]
}
去掉elasticsearch.yml
中的index.analysis.analyzer.default.type : "ik"
羔砾,重啟负间,在此請(qǐng)求localhost:9200/xxx/_analyze?field=nickname&text=我愛(ài)中國(guó)
,得到
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "<IDEOGRAPHIC>",
"position": 0
},
{
"token": "愛(ài)",
"start_offset": 1,
"end_offset": 2,
"type": "<IDEOGRAPHIC>",
"position": 1
},
{
"token": "中",
"start_offset": 2,
"end_offset": 3,
"type": "<IDEOGRAPHIC>",
"position": 2
},
{
"token": "國(guó)",
"start_offset": 3,
"end_offset": 4,
"type": "<IDEOGRAPHIC>",
"position": 3
}
]
}
結(jié)果分析:es自帶的默認(rèn)分詞器對(duì)中文支持不友好姜凄,只是簡(jiǎn)單的分割成單個(gè)漢字政溃,ik則更友好,在elasticsearch.yml
配置index.analysis.analyzer.default.type : "ik"
态秧,可把ik配置成默認(rèn)的分詞器董虱。