elasticsearch 官方默认的分词插件,对中文分词效果不理想,它是把中文词语分成了一个一个的汉字。所以我们引入 es 插件 es-ik。同时为了提升用户体验,引入 es-pinyin 插件。本文介绍这两个 es 插件的安装。
环境
本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch ,这个版本比较稳定,可以用于生产环境。
ik 分词器 和 pinyin 分词器在 github 仓库可以找到,注意版本与 elasticsearch 的版本需要对应,本文使用 5.5.3 版本
如果 elasticsearch 与我的版本不同,可以去官方 github 对应的仓库查看对应版本的 ik 以及 pinyin 分词器。
系列文章
安装 ik 分词器
前文中,我们已经安装了 elasticsearch,我们在其基础上安装分词器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
cd /usr/local/es/
## 下载 ik 分词器 wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.3/elasticsearch-analysis-ik-5.5.3.zip
## 使用 unzip 解压 ## 如果未安装,通过 yum instal unzip 进行安装 unzip elasticsearch-analysis-ik-5.5.3.zip
## 在 elasticsearch 安装目录下的 plugins 文件夹下创建 ik 目录 mkdir /usr/local/es/master/plugins/ik
## 将解压后的 ik 目录下的所有文件移动到 /usr/local/es/master/plugins/ik/ mv /usr/local/es/elasticsearch/* /usr/local/es/master/plugins/ik/
## 重启 elasticsearch 或启动 master elasticsearch ## 注意启动时,需要切换到 esuser 用户
## 启动成功后,日志里会打印此行 [2018-09-02T06:33:43,703][INFO ][o.e.p.PluginsService ] [master] loaded plugin [analysis-ik]
|
验证 ik 分词器是否生效
打开 Restlet Client 或者 postman 工具
GET http://192.168.199.192:9200/_analyze?analyzer=ik_smart&text="中华人民共和国国歌"
移除名为 ik 的analyzer和tokenizer,请分别使用 ik_smart 和 ik_max_word
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
这里 analyzer 可以指定分词类型,发送请求返回结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
{ "tokens": [ { "token": "中华人民共和国", "start_offset": 1, "end_offset": 8, "type": "CN_WORD", "position": 0 }, { "token": "国歌", "start_offset": 8, "end_offset": 10, "type": "CN_WORD", "position": 1 } ] }
|
我这里指定的 analyzer 为 ik_smart,即粗粒度分词,可以看到 ik 分词器已经生效了。接下来继续安装 pinyin 分词器。
安装 pinyin 分词器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
cd /usr/local/es/
## 下载 pinyin 分词器 wget https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v5.5.3/elasticsearch-analysis-pinyin-5.5.3.zip
## 使用 unzip 解压 ## 如果未安装,通过 yum instal unzip 进行安装 unzip elasticsearch-analysis-pinyin-5.5.3.zip
## 在 elasticsearch 安装目录下的 plugins 文件夹下创建 pinyin 目录 mkdir /usr/local/es/master/plugins/pinyin
## 将解压后的 ik 目录下的所有文件移动到 /usr/local/es/master/plugins/pinyin/ mv /usr/local/es/elasticsearch/* /usr/local/es/master/plugins/pinyin/
## 重启 elasticsearch 或启动 master elasticsearch ## 注意启动时,需要切换到 esuser 用户
## 启动成功后,日志里打印 [2018-09-02T07:04:56,203][INFO ][o.e.p.PluginsService ] [master] loaded plugin [analysis-ik] [2018-09-02T07:04:56,203][INFO ][o.e.p.PluginsService ] [master] loaded plugin [analysis-pinyin]
|
验证 pinyin 分词器是否生效
打开 Restlet Client 或者 postman 工具
GET http://192.168.199.192:9200/_analyze?analyzer=pinyin&text="李小龙"
这里 analyzer 可以指定分词为 pinyin,发送请求返回结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
{ "tokens": [ { "token": "li", "start_offset": 1, "end_offset": 2, "type": "word", "position": 0 }, { "token": "xiao", "start_offset": 2, "end_offset": 3, "type": "word", "position": 1 }, { "token": "long", "start_offset": 3, "end_offset": 4, "type": "word", "position": 2 }, { "token": "lxl", "start_offset": 0, "end_offset": 3, "type": "word", "position": 2 } ] }
|
观察结果,说明 pinyin 分词器也已经生效了。
集群其他节点分词器安装
现在只对 master 进行了安装,其他 slave 也需要安装,这里可以通过拷贝的方式直接来完成安装了。
1 2 3
|
cp -r master/plugins/ slave1/
cp -r master/plugins/ slave2/
|
如果其他节点在不同服务器上,通过 scp 命令拷贝即可。