在Elasticsearch中默认的分词器对中文的支持不好,会分隔成一个一个的汉字。而IK分词器对中文的支持比较好,主要有两种模式"ik_smart""ik_max_word"

安装IK分词器

方法一: 自动安装IK分词器

注意: 必须保证centos系统是联网的。

IK分词器的 GitHub 地址,选择跟自己的Elasticsearch对应的版本,本文使用的版本是Elasticsearch7.5.1版本。

更多版本地址

找到IK分词器的7.5.1的地址然后使用elasticsearch-plugin命令安装:

# ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.5.1/elasticsearch-analysis-ik-7.5.1.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.5.1/elasticsearch-analysis-ik-7.5.1.zip
[=================================================] 100%   
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik
#查看安装的插件
# ./elasticsearch-plugin list
analysis-ik

方法二: 手动安装IK分词器

1.下载

在这里插入图片描述

2.上传到服务器

然后解压到elasticsearch目录,将ik文件夹放在elasticsearch/plugins目录下,重启elasticsearch
在这里插入图片描述
ik_smart:会做最粗粒度的拆分,比如会将"中华人民共和国人民大会堂”拆分为中华人民共和国、人民大会堂"。

ik_max_word:比如会将"中华人民共和国人民大会堂" 拆分为"中华人民共和国、中华人民、中华、华人、人民共和国、人民、共和国、大会堂、大会、会堂"等词语。

配置Ik自定义词典

官网

编辑配置文件,因每次修改词典都需要重启 ES,为解决这不现实的操作,打开远程扩展字典,让支持热词更新

vim /usr/local/elasticsearch/config/analysis-ik/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"></entry>
         <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
        <entry key="remote_ext_dict">http://www.xxx.com/es_dic/dic.txt</entry>
        <!--用户可以在这里配置远程扩展停止词字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

第一次配置需要重启 ES

nginx下配置如下

server {
      ......
        location /es_dic {
               charset utf-8;
               root   /data/web/dic/;
         }
}

这样就可以实现远程热词更新了!

安装拼音分词

# ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.5.1/elasticsearch-analysis-pinyin-7.5.1.zip

安装后重启es

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐