写点什么

IK 字段级别词典升级:IK reload API

作者:极限实验室
  • 2025-08-03
    北京
  • 本文字数:2175 字

    阅读完需:约 7 分钟

IK 字段级别词典升级:IK reload API

之前介绍 IK 字段级别字典 使用的时候,对于字典的更新只是支持词典库的新增,并不支持对存量词典库的修改或者删除。经过这段时间的开发,已经可以兼容词典库的更新,主要通过 IK reload API 来实现。

IK reload API

IK reload API 通过对词典库的全量重新加载来实现词典库的更新或者删除。用户可以通过下面的命令实现:


# 测试索引准备
PUT my-index-000001{ "settings": { "number_of_shards": 3, "analysis": { "analyzer": { "my_custom_analyzer": { "type": "custom", "tokenizer": "my_tokenizer" } }, "tokenizer": { "my_tokenizer": {
"type": "ik_smart", "custom_dict_enable": true, "load_default_dicts":false, # 这里不包含默认词库 "lowcase_enable": true, "dict_key": "test_dic" } } } }, "mappings": { "properties": { "test_ik": { "type": "text", "analyzer": "my_custom_analyzer" } } }}
# 原来词库分词效果,只预置了分词“自强不息”GET my-index-000001/_analyze{ "analyzer": "my_custom_analyzer", "text":"自强不息,杨树林"}
{ "tokens": [ { "token": "自强不息", "start_offset": 0, "end_offset": 4, "type": "CN_WORD", "position": 0 }, { "token": "杨", "start_offset": 5, "end_offset": 6, "type": "CN_CHAR", "position": 1 }, { "token": "树", "start_offset": 6, "end_offset": 7, "type": "CN_CHAR", "position": 2 }, { "token": "林", "start_offset": 7, "end_offset": 8, "type": "CN_CHAR", "position": 3 } ]}
# 更新词库POST .analysis_ik/_doc{ "dict_key": "test_dic", "dict_type": "main_dicts", "dict_content":"杨树林"}# 删除词库,词库文档的id为coayoJcBFHNnLYAKfTMLDELETE .analysis_ik/_doc/coayoJcBFHNnLYAKfTML?refresh=true
# 重载词库POST _ik/_reload{}
# 更新后的词库效果GET my-index-000001/_analyze{ "analyzer": "my_custom_analyzer", "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 }, { "token": "不", "start_offset": 2, "end_offset": 3, "type": "CN_CHAR", "position": 2 }, { "token": "息", "start_offset": 3, "end_offset": 4, "type": "CN_CHAR", "position": 3 }, { "token": "杨树林", "start_offset": 5, "end_offset": 8, "type": "CN_WORD", "position": 4 } ]}
复制代码


这里是实现索引里全部的词库更新。


也可以实现单独的词典库更新


POST _ik/_reload{"dict_key":"test_dic”}
# debug 日志[2025-07-09T15:30:29,439][INFO ][o.e.a.i.ReloadIK ] [ik-1] 收到重载IK词典的请求,将在所有节点上执行。dict_key: test_dic, dict_index: .analysis_ik[2025-07-09T15:30:29,439][INFO ][o.e.a.i.a.TransportReloadIKDictionaryAction] [ik-1] 在节点 [R6ESV5h1Q8OZMNoosSDEmg] 上执行词典重载操作,dict_key: test_dic, dict_index: .analysis_ik
复制代码


这里传入的 dict_key 对应的词库 id。


对于自定义的词库存储索引,也可以指定词库索引的名称,如果不指定则默认使用 .analysis_ik


POST _ik/_reload{"dict_index":"ik_index"}
# debug 日志[2025-07-09T15:32:59,196][INFO ][o.e.a.i.a.TransportReloadIKDictionaryAction] [ik-1] 在节点 [R6ESV5h1Q8OZMNoosSDEmg] 上执行词典重载操作,dict_key: null, dict_index: test_ik[2025-07-09T15:32:59,196][INFO ][o.w.a.d.ReloadDict ] [ik-1] Reloading all dictionaries
复制代码


注:


  1. 更新或者删除词库重载后只是对后续写入的文档生效,对已索引的文档无效;

  2. 因为用户无法直接更改 IK 内置的词库(即默认配置路径下的词库文件),因此 reload API 不会影响内置词库的信息。


相关阅读


关于 IK Analysis


IK Analysis 插件集成了 Lucene IK 分析器,并支持自定义词典。它支持 Easysearch\Elasticsearch\OpenSearch 的主要版本。由 INFINI Labs 维护并提供支持。


该插件包含分析器:ik_smart 和 ik_max_word,以及分词器:ik_smart 和 ik_max_word


开源地址:https://github.com/infinilabs/analysis-ik


作者:金多安,极限科技(INFINI Labs)搜索运维专家,Elastic 认证专家,搜索客社区日报责任编辑。一直从事与搜索运维相关的工作,日常会去挖掘 ES / Lucene 方向的搜索技术原理,保持搜索相关技术发展的关注。

原文:https://infinilabs.cn/blog/2025/ik-field-level-dictionarys-2/

发布于: 刚刚阅读数: 3
用户头像

简单、易用、极致、创新 2021-11-22 加入

极限实验室(INFINI Labs)致力于打造极致易用的数据探索与分析体验。

评论

发布
暂无评论
IK 字段级别词典升级:IK reload API_ik_极限实验室_InfoQ写作社区