词库的配置
创建一个词典文件(例如 custom_dictionary.txt),其中包含所有需要作为整体关键词的词组:
how are you
what is your name
where do you live
上传词典文件将词典文件上传到 Elasticsearch 节点的某个路径(例如 /usr/share/elasticsearch/plugins/analysis-icu/custom_dictionary.txt)。
在 Elasticsearch 中配置自定义分析器,使用自定义词典
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"my_custom_filter",
"lowercase"
]
}
},
"filter": {
"my_custom_filter": {
"type": "dictionary",
"language": "en",
"ignore_case": true,
"preserve_original": true,
"words_path": "/usr/share/elasticsearch/plugins/analysis-icu/custom_dictionary.txt"
}
}
}
},
"mappings": {
"properties": {
"text": {
"type": "text",
"analyzer": "custom_analyzer",
"search_analyzer": "standard"
}
}
}
}
词库动态更新的方式
_reload_search_analyzers API
Elasticsearch 7.3 及以上版本引入了 _reload_search_analyzers API ,允许重新加载分析器,而无需重启或重建索引。
# 先允许动态重载分析器
POST /_cluster/settings
{
"transient": {
"indices.analysis.reload_named_analyzers": true
}
}
# 重新加载分析器
POST /_reload_search_analyzers