Elasticsearch安装ik分词器以及自定义中文词库

1,890 阅读1分钟

github地址:https://github.com/medcl/elasticsearch-analysis-ik

elasticsearch-analysis-ik下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases


1. 安装elasticsearch-analysis-ik插件

最好下载和你安装的elasticsearch版本对应的!

在elasticsearch安装目录下的plugins目录下新建一个文件夹 ik,解压到该目录即可

[esuser@localhost ik]$ pwd
/usr/local/elasticsearch-7.10.2/plugins/ik
[esuser@localhost ik]$ ll
总用量 1432
-rw-r--r--. 1 root root 263965 2月   3 15:07 commons-codec-1.9.jar
-rw-r--r--. 1 root root  61829 2月   3 15:07 commons-logging-1.2.jar
drwxr-xr-x. 2 root root   4096 23 15:07 config
-rw-r--r--. 1 root root  54626 2月   3 15:07 elasticsearch-analysis-ik-7.10.2.jar
-rw-r--r--. 1 root root 736658 2月   3 15:07 httpclient-4.5.2.jar
-rw-r--r--. 1 root root 326724 2月   3 15:07 httpcore-4.4.4.jar
-rw-r--r--. 1 root root   1807 2月   3 15:07 plugin-descriptor.properties
-rw-r--r--. 1 root root    125 2月   3 15:07 plugin-security.policy
[esuser@localhost ik]$ 

重启es,然后测试

post请求,http://ip:9200/_analyze

请求体json

{
    "analyzer": "ik_max_word",
    "text": "工作996,生病ICU"
}

示例code

POST /_analyze HTTP/1.1
Host: 172.16.59.128:9200
Content-Type: application/json
Content-Length: 60

{
    "analyzer": "ik_max_word",
    "text": "工作996,生病ICU"
}

返回数据

{
    "tokens": [
        {
            "token": "工作",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "996",
            "start_offset": 2,
            "end_offset": 5,
            "type": "LETTER",
            "position": 1
        },
        {
            "token": "生病",
            "start_offset": 6,
            "end_offset": 8,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "icu",
            "start_offset": 8,
            "end_offset": 11,
            "type": "ENGLISH",
            "position": 3
        }
    ]
}

ik_max_word 和 ik_smart 什么区别?

  • ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合,适合 Term Query;
  • ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”,适合 Phrase 查询。

2. 自定义中文词库

  • 1. 先关闭elasticsearch

  • 2. 切换回root用户

  • 3. 在 /elasticsearch-7.10.2/plugins/ik/config目录下,修改 IKAnalyzer.cfg.xml文件

修改该项,添加 custom.dic

<entry key="ext_dict">custom.dic</entry>

完整 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.dic</entry>
	 <!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords"></entry>
	<!--用户可以在这里配置远程扩展字典 -->
	<!-- <entry key="remote_ext_dict">words_location</entry> -->
	<!--用户可以在这里配置远程扩展停止词字典-->
	<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
  • 4. 在同级目录下,新建 custom.dic 文件 在里面添加要分的词汇即可,比如我添加了 骚年、打工人两个词
[root@localhost config]$ cat custom.dic 
骚年
打工人
[root@localhost config]$ 
  • 5. 启动es即可