最近重新配置一个早期项目的开发环境,需要配置es,记录一下如何安装ik分词器。
// 安装单节点 docker run -d --name es6.5.4 --net my-net -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v /docker/elasticsearch/data:/usr/share/elasticsearch/data docker.elastic.co/elasticsearch/elasticsearch:6.5.4 // 进入容器es6.5.4执行操作 docker exec -it es6.5.4 /bin/bash // 使用elasticsearch-plugin install安装ik插件 bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip // 重启容器生效 docker restart es6.5.4
一个需要注意的点是ik分词器的版本需要与elasticsearch版本一致才能安装成功。
测试ik分词器是否安装成功
// 增加一个测试的索引ik_test
curl -X PUT http://localhost:9200/ik_test
// 成功返回 {"acknowledged":true,"shards_acknowledged":true,"index":"ik_test"}
// ik_smart模式分词
curl -X POST \
'http://127.0.0.1:9200/ik_test/_analyze?pretty=true' \
-H 'Content-Type: application/json' \
-d '{"text":"你好,这个美丽的世界!","tokenizer":"ik_smart"}'
// ik_max_word模式分词
curl -X POST \
'http://127.0.0.1:9200/ik_test/_analyze?pretty=true' \
-H 'Content-Type: application/json' \
-d '{"text":"你好,这个美丽的世界!","tokenizer":"ik_max_word"}'
在测试例子中两种模式返回结果相同
返回结果:
{
"tokens" : [
{
"token" : "你好",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "这个",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "美丽",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "的",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "世界",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 4
}
]
}
记录一下ik_max_word模式和ik_smart模式的区别:
1、ik_max_word
会将文本做最细粒度的拆分,比如会将“中华人民共和国人民大会堂”拆分为“中华人民共和国、中华人民、中华、华人、人民共和国、人民、共和国、大会堂、大会、会堂等词语。
2、ik_smart
会做最粗粒度的拆分,比如会将“中华人民共和国人民大会堂”拆分为中华人民共和国、人民大会堂。