Elasticsearch 8.9版本中如何在Filter上下文使用Not子句查询文档
解决Elasticsearch 8.9中Filter上下文使用Not子句的解析异常问题
你遇到的parsing_exception是因为Elasticsearch 8.9不支持直接使用not作为查询类型,要实现排除特定文档的过滤逻辑,得用bool查询嵌套must_not子句来替代。
修正后的查询语句如下:
{ "size": 0, "query": { "bool": { "must": [ { "match": { "cid": { "query": "AFM" } } }, { "match": { "web_code": "P" } } ], "filter": [ { "bool": { "must_not": [ { "term": { "web_code": "PS" } } ] } } ] } } }
关键说明:
- 在Filter上下文中,通过
bool查询的must_not子句可以实现"排除匹配文档"的逻辑,这是Elasticsearch官方推荐的写法 must_not在Filter上下文同样不会影响文档的相关性得分,完全符合Filter的使用场景
内容的提问来源于stack exchange,提问作者Ajay Takur
相关产品推荐
相关产品推荐

