- 官方文档https://github.com/medcl/elasticsearch-analysis-ik
- 参考链接:https://blog.csdn.net/shihuacai/article/details/8494360/
我是用手工安装的插件,然后准备本地配置下扩展字典。步骤如下:
-
在 https://github.com/medcl/elasticsearch-analysis-ik/releases 下载和自己的 es 版本相匹配的 ik 分词器。
-
解压 elasticsearch-analysis-ik-7.12.0.zip ,然后把解压文件夹拷贝到 /xxxxxx/elasticsearch-7.12.0/plugins/ 下。
-
然后把 elasticsearch-analysis-ik-7.12.0 文件夹重命名为 ik 。
-
然后编辑 /xxxxxx/elasticsearch-7.12.0/plugins/ik/config/IKAnalyzer.cfg.xml 配置文件。内容如下:
<properties> <comment>IK Analyzer 扩展配置</comment> <!--用户可以在这里配置自己的扩展字典 --> <entry key="ext_dict">/xxxxxx/elasticsearch-7.12.0/plugins/ik/shfq_ext_dic.dic</entry> <!--用户可以在这里配置自己的扩展停止词字典--> <entry key="ext_stopwords"></entry> <!--用户可以在这里配置远程扩展字典 --> <!-- <entry key="remote_ext_dict">words_location</entry> --> <!--用户可以在这里配置远程扩展停止词字典--> <!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties>
-
shfq_ext_dic.dic 扩展字典内容如下:
民共 程隆 程隆腾达
-
重启 es ,然后在 kibana 对 “民共” 进行分词,结果没生效。
GET _analyze { "analyzer": "ik_max_word", "text" : "民共" } 分词结果: { "tokens" : [ { "token" : "民", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "共", "start_offset" : 1, "end_offset" : 2, "type" : "CN_CHAR", "position" : 1 } ] }
百思不得其解,后来重新启动 es ,在启动日志里看到了下面的信息:
[Ext Loading] file not found: /xxxxxx/elasticsearch-7.12.0/plugins/ik/config/xxxxxx/elasticsearch-7.12.0/plugins/ik/shfq_ext_dic.dic
我在 IKAnalyzer.cfg.xml 配置文件中配置的扩展字典的地址是绝对地址,实际上只能用相对地址,必须把扩展字典文件放到 /xxxxxx/elasticsearch-7.12.0/plugins/ik/config/ 目录下 。
修改之后,再对 “民共” 分词之后,结果如下:
{
"tokens" : [
{
"token" : "民共",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
}
]
}
符合预期。
有两点需要注意:
- 字典文件要求是 utf-8 编码格式的。
- 字典的路径是相对的,不能随便放,必须放到 config 目录下。