如何在Elasticsearch中查询title/description缺失且type为A的文档
解决方案
你的需求是筛选出type字段为'A',且title或description至少一个缺失的文档,正确的Elasticsearch查询语句如下:
{ "query": { "bool": { "must": [ { "term": { "type": "A" } }, { "bool": { "should": [ { "bool": { "must_not": { "exists": { "field": "title" } } } }, { "bool": { "must_not": { "exists": { "field": "description" } } } } ], "minimum_should_match": 1 } } ] } } }
逻辑说明:
- 外层
bool的must数组确保两个条件同时满足:term查询匹配type为'A'的文档;- 子
bool查询通过should实现或逻辑:只要title缺失,或者description缺失,两者满足其一即可。minimum_should_match:1用来保证至少触发一个should里的条件,避免无意义的匹配。
你之前的查询只处理了title缺失的情况,通过添加description缺失的should分支,就能覆盖所有符合要求的场景。
内容的提问来源于stack exchange,提问作者Ravi
相关产品推荐
相关产品推荐

