ElasticSearch添加映射遇mapper_exception:同义词分析器无法索引时运行
问题原因
你配置的my_synonym_filter开启了updateable: true参数,该参数会限制同义词过滤器仅能在搜索阶段运行,无法用于索引阶段。而你将包含这个过滤器的分析器分配给了字段的analyzer(索引时生效的分析器),因此触发了报错。
解决方案
根据是否需要动态更新同义词文件,可选择以下两种处理方式:
方式一:无需动态更新同义词
如果不需要后续修改同义词文件后自动生效,移除updateable: true参数即可让过滤器同时支持索引和搜索阶段:
- 关闭索引:
POST /my_index/_close
- 更新索引设置,去掉
updateable: true:
PUT /my_index/_settings { "settings": { "analysis": { "filter": { "my_synonym_filter": { "type": "synonym", "synonyms_path": "synonyms.txt" } }, "analyzer": { "my_synonym_analyzer": { "tokenizer": "standard", "filter": [ "lowercase", "my_synonym_filter" ] } } } } }
- 打开索引:
POST /my_index/_open
- 重新设置字段映射:
PUT /my_index/_mapping { "properties": { "content": { "type": "text", "analyzer": "my_synonym_analyzer" } } }
方式二:需要动态更新同义词
若必须保留updateable: true以支持同义词文件的动态更新,需将带同义词的分析器设为搜索专用,索引阶段使用普通分析器:
- 打开索引(若之前处于关闭状态):
POST /my_index/_open
- 修改字段映射,指定索引分析器为
standard,搜索分析器为你的同义词分析器:
PUT /my_index/_mapping { "properties": { "content": { "type": "text", "analyzer": "standard", "search_analyzer": "my_synonym_analyzer" } } }
此配置下,文档索引时用standard分析器处理,搜索时启用同义词分析器,既支持同义词动态更新,又能实现搜索同义词匹配效果。
内容的提问来源于stack exchange,提问作者Andre Bergmann
相关产品推荐
相关产品推荐

