ES默认分词器 使用 standard 分词器 将字符串分割成单独的字词 ,对中文的分词效果不是很好,例如查询“白珠”,他会把包含“白”、“珠”的所有查询结果都返回(“白”、“白梅珠”、“珠珠”、“珠”等)。我们可以选择IK分词器解决中文分词的问题。
1、下载IK分词器
下载网址:https://github.com/medcl/elasticsearch-analysis-ik/releases
选择和自己ES版本对应的IK分词器:
这里我选的是V5.5.2,下载编译好的zip包。
2、安装IK分词器
在ES的安装目录下有个 plugs 文件夹,在下面建一个IK文件夹,把刚才下载的 zip 包解压到 ik 文件夹里,最后重启 ES
3、测试
打开kibana 使用其中的工具执行如下命令:
GET _analyze
{
"analyzer":"ik_smart",
"text":"中华人民共和国"
}
返回结果:
{
"tokens": [
{
"token": "中华人民共和国",
"start_offset": 0,
"end_offset": 7,
"type": "CN_WORD",
"position": 0
}
]
}
GET _analyze
{
"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
}
]
}
如果想把IK分词器用到新索引上,可以在建索引的时候指定分词器:
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
}
}
}
安装成功!