Elastic Search Query 中的 Should query 子句中的 Must query

Must query in Should query clause in Elastic Search Query

我有一个场景,在弹性搜索查询的 should 子句中,我需要有一个 must 子句。

例如,我需要以这样一种方式过滤数据:如果数据应该只针对发货区域 ID 仅为 10 且承运人 ID 仅为 1、2、3 的订单,但也应该为所有订单提取数据驱动程序 ID 为 1,2,3.

在我当前的场景中,它提取所有运营商 ID 为 1、2、3 和调度区域 ID 10 以及非 10 的调度区域的数据。即,如果运营商 ID 1 的调度区域 ID 为 9,则该数据也来了。

如何在 should 查询中添加 must 子句。

{
      "from": 0,
      "size": 10000,
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  [
                    {
                      "terms": {
                        "dispatchAreaId": [
                          10
                        ]
                      }
                    },
                    {
                      "terms": {
                        "carrierId": [
                          1,2,3
                        ]
                      }
                    },
                    {
                      "terms": {
                        "driverIds": [
                          1,2,3
                        ]
                      }
                    }
                  ]
                ]
              }
            }
          ]
        }
      }
    }

尝试下面的查询

{
  "from": 0,
  "size": 10000,
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "dispatchAreaId": [
              10
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "dispatchAreaId": [
                    10
                  ]
                }
              },
              {
                "terms": {
                  "carrierId": [
                    1,
                    2,
                    3
                  ]
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

希望对您有所帮助!!