简介
准备工作
环境:centos7.0,Elasticsearch6.0.0
下载IK插件压缩包,插件版本也是6.0.0
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.0.0/elasticsearch-analysis-ik-6.0.0.zip
之前的版本看到他们需要自己maven编译,这个版本直接下载zip格式的是已经编译完成的,直接使用
安装IK
解压elasticsearch-analysis-ik-6.0.0.zip
把解压后的elasticsearch下的文件拷贝到elasticsearch安装目录下plugins文件里面创建的ik文件下
unzip elasticsearch-analysis-ik-6.0.0.zip
cd elasticsearch
pwd
/mnt/opt/elasticsearch
#创建安装目录下的插件目录ik
mkdir /mnt/opt/elasticsearch-6.0.0/plugins/ik/
#拷贝当前文件夹下的所有文件到插件目录ik
cp -r ./* /mnt/opt/elasticsearch-6.0.0/plugins/ik/
启动
中文分词测试
使用RESTAPI进行测试
curl -H 'Content-Type: application/json' -XGET 192.168.0.91:9200/_analyze?pretty -d '{"analyzer":"ik_max_word","text":"中华人民共和国国歌"}'
结果打印出来可以看出已经进行分词了
{
"tokens" : [
{
"token" : "中华人民共和国",
"start_offset" : 0,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "中华人民",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "中华",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "华人",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "人民共和国",
"start_offset" : 2,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "人民",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "共和国",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "共和",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "国",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 8
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 9
}
]
}
安装elasticsearch-analysis-pinyin分词器
pinyin分词器可以让用户输入拼音,就能查找到相关的关键词。比如在某个商城搜索中,输入shuihu,就能匹配到水壶。这样的体验还是非常好的。
pinyin分词器的安装与IK是一样的,这里就省略掉了
插件地址
https://github.com/medcl/elasticsearch-analysis-pinyin
提供了多种分词规则:
pinyin,就是普通的把汉字转换成拼音;
pinyin_first_letter,提取汉字的拼音首字母
其他请查看官方文档