You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 09:25:37