Elasticsearch 7.12版本wildcard查询case_insensitive参数报错求助
问题原因及解决方案
根本原因
Elasticsearch 7.12版本不支持wildcard查询的case_insensitive参数,该参数是在7.13版本才被引入的。因此你的查询中包含了当前版本无法识别的参数,导致触发parsing_exception解析错误。
解决方案
方案1:升级Elasticsearch版本
如果允许升级,直接将集群升级到7.13或更高版本,即可正常使用case_insensitive参数。
方案2:使用小写归一化字段(无需升级)
通过修改索引映射,添加一个经过小写归一化的字段,实现大小写不敏感的通配符查询:
- 先更新索引的设置和映射,添加小写归一器和对应字段:
PUT /your_index_name/_settings { "analysis": { "normalizer": { "lowercase_normalizer": { "type": "custom", "filter": ["lowercase"] } } } } PUT /your_index_name/_mapping { "properties": { "attribute_values": { "properties": { "text_value": { "type": "text", "fields": { "keyword": {"type": "keyword"}, "lowercase": { "type": "keyword", "normalizer": "lowercase_normalizer" } } } } } } }
- 重新索引数据后,查询时将输入值转为小写,再匹配归一化后的字段:
wildcard: { 'attribute_values.text_value.lowercase': { value: '*' + textValue.toLowerCase() + '*' } }
方案3:使用正则查询替代
通过regexp查询的flags: 'i'参数实现大小写不敏感匹配(注意:正则查询性能略低于wildcard):
regexp: { 'attribute_values.text_value.keyword': { value: '.*' + textValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '.*', flags: 'i' } }
注:需要对输入值中的正则特殊字符进行转义,避免查询语法错误。
内容的提问来源于stack exchange,提问作者Powerbenben
相关产品推荐
相关产品推荐

