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

ElasticSearch如何编写实现must_not((A且B且C)或(D且E且F))逻辑的查询

你要实现的逻辑可以通过在must_not内部的bool查询中新增should子句完成,should天然对应「或」逻辑,最终查询DSL如下:

GET file_index_1/_search
{
  "query": {
    "bool": {
      "must_not": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            // 第一组排除条件:A且B且C
            {
              "bool": {
                "must": [
                  {"match": {"field1": "A"}},
                  {"match": {"field2": "B"}},
                  {"match": {"field3": "C"}}
                ]
              }
            },
            // 第二组排除条件:D且E且F
            {
              "bool": {
                "must": [
                  {"match": {"field4": "D"}},
                  {"match": {"field5": "E"}},
                  {"match": {"field6": "F"}}
                ]
              }
            }
          ]
        }
      }
    }
  }
}

逻辑说明

  • 内层bool的should数组里存放所有并列的「或」条件,每一个元素都是一组需要同时满足的must规则
  • minimum_should_match: 1 显式声明只要匹配should中的任意一组条件,就会命中must_not的排除规则,该配置也可以省略,当bool查询中只有should子句时,默认要求至少匹配1条

内容的提问来源于stack exchange,提问作者sysley

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 05:45:00