Elasticsearch多匹配条件查询:单查询匹配三类completed状态记录
Elasticsearch 复合查询实现三类通话记录匹配
可以使用 bool 查询的 should 子句来组合这三个场景,只要满足其中任意一个条件就会返回结果。以下是完整的查询DSL:
{ "query": { "bool": { "should": [ // 场景1:lastOutboundStatus为busy/no-answer/rejected 且 lastInboundStatus为completed { "bool": { "must": [ { "terms": { "lastOutboundStatus": ["busy", "no-answer", "rejected"] } }, { "term": { "lastInboundStatus": "completed" } } ] } }, // 场景2:lastInboundStatus为completed 且 lastOutboundStatus不存在 { "bool": { "must": [ { "term": { "lastInboundStatus": "completed" } }, { "bool": { "must_not": [ { "exists": { "field": "lastOutboundStatus" } } ] } } ] } }, // 场景3:lastOutboundStatus为completed(lastInboundStatus是否存在不影响) { "term": { "lastOutboundStatus": "completed" } } ], "minimum_should_match": 1 } } }
关键说明:
should数组中的每个元素对应一个场景,minimum_should_match: 1确保至少满足其中一个条件(默认值就是1,显式写出更清晰)。- 场景2中用
must_not + exists判断lastOutboundStatus字段不存在。 - 场景3仅需匹配
lastOutboundStatus为completed,无需关注lastInboundStatus是否存在,直接用单个term查询即可。
内容的提问来源于stack exchange,提问作者Amit
相关产品推荐
相关产品推荐

