ik分词器安装
# 进入es bin 目录,运行如下脚本安装ik分词器
cd /usr/local/elasticsearch/bin
[ajtuser@centos7 bin]$ ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.3.0/elasticsearch-analysis-ik-7.3.0.zip
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/java/jdk1.8/jre] does not meet this requirement
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.3.0/elasticsearch-analysis-ik-7.3.0.zip
[=================================================] 100%
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: plugin requires additional permissions @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.
Continue with installation? [y/N]
[ajtuser@centos7 bin]$ cd ../plugins/
[ajtuser@centos7 plugins]$ ll
total 0
drwxr-xr-x. 2 ajtuser ajtuser 229 Aug 1 13:50 analysis-ik
[ajtuser@centos7 plugins]$
可以看到ik分词器已经安装到elasticsearch中了,需要重启elasticsearch
# 前台启动,按ctrl+c后会退出程序
[ajtuser@centos7 local]$ ./elasticsearch/bin/elasticsearch
# 后台启动
[ajtuser@centos7 local]$ ./elasticsearch/bin/elasticsearch -d
验证ik分词器是否生效
在kibana控制台使用analyze api验证
GET _analyze?pretty
{
"analyzer": "ik_smart",
"text": "中华人民共和国国歌"
}
分词结果
{
"tokens" : [
{
"token" : "中华人民共和国",
"start_offset" : 0,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 1
}
]
}
与standard分词对比
GET _analyze?pretty
{
"analyzer": "standard",
"text": "中华人民共和国国歌"
}
分词结果
{
"tokens" : [
{
"token" : "中",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "华",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "人",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "民",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "共",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "和",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
},
{
"token" : "国",
"start_offset" : 6,
"end_offset" : 7,
"type" : "<IDEOGRAPHIC>",
"position" : 6
},
{
"token" : "国",
"start_offset" : 7,
"end_offset" : 8,
"type" : "<IDEOGRAPHIC>",
"position" : 7
},
{
"token" : "歌",
"start_offset" : 8,
"end_offset" : 9,
"type" : "<IDEOGRAPHIC>",
"position" : 8
}
]
}
ik分词最佳实践:
在索引阶段用ik_max_word分词器,最可能分出有意义的词,而在搜索阶段则用ik_smart分词器分词,