Elasticsearch中,内置了很多分词器(analyzers)
但是普遍对中文支持不友好
比如使用postman 请求http://192.168.43.66:9200/_analyze
请求参数
{
"analyzer": "standard",
"text": "奥迪A6"
}
{
"tokens": [
{
"token": "奥",
"start_offset": 0,
"end_offset": 1,
"type": "<IDEOGRAPHIC>",
"position": 0
},
{
"token": "迪",
"start_offset": 1,
"end_offset": 2,
"type": "<IDEOGRAPHIC>",
"position": 1
},
{
"token": "a6",
"start_offset": 2,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 2
}
]
}
上面会把奥迪分开成单个汉字
因此引入中文分词器es-ik插件
下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases
注意: es-ik分词插件版本一定要和es安装的版本对应
第一步:下载es的IK插件(资料中有)命名改为ik插件
第二步: 上传到/usr/local/elasticsearch-6.4.3/plugins
上传到plugins文件夹下
第三步: 重启elasticsearch即可
测试
http://192.168.43.66:9200/_analyze
{
"analyzer": "ik_smart",
"text": "奥迪A6"
}
{
"tokens": [
{
"token": "奥迪",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
},
{
"token": "a6",
"start_offset": 2,
"end_offset": 4,
"type": "LETTER",
"position": 1
}
]
}
但是这样依然会有问题 比如一些常见热词依然无法正确分词
比如 马云 会拆分成马,和云两个字 王者荣耀会被拆分为 王者,和荣耀两个词语
为了解决上诉问题可以自定义扩展字典
进入/usr/local/software/es/elasticsearch-6.4.3/plugins/ik/config路径下
mkdir custom创建文件夹
创建文件xuxu.dic 并添加以下内容
修改config目录下 vim IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">custom/xuxu.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
重启es
再次请求分词 效果如下