之前介紹 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為coayoJcBFHNnLYAKfTML
DELETE .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
注:
- 更新或者刪除詞庫重載後只是對後續寫入的文檔生效,對已索引的文檔無效;
- 因為用户無法直接更改 IK 內置的詞庫(即默認配置路徑下的詞庫文件),因此 reload API 不會影響內置詞庫的信息。
相關閲讀
- [IK 字段級別詞典的升級之路
](https://infinilabs.cn/blog/2025/ik-field-level-dictionarys-3/) - [Easysearch 新功能: IK 字段級別詞典
](https://infinilabs.cn/blog/2025/ik-field-level-dictionarys/)
關於 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/