用于过滤的 Elasticsearch DSL bool 查询

Elasticsearch DSL bool query for filtration

我有一个如下所示的 Elasticsearch DSL 查询

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "api": "A"
          }
        },
        {
          "match_phrase": {
            "api": "B"
          }
        },
        {
          "match_phrase": {
            "api": "C"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

据我了解,这类似于

匹配如果(api = A 或 api = B 或 api = C)

我想在第三次检查中添加另一个检查不同字段的条件。

E.g match if (api = A or api = B or (api = C and another_field = D ) ) 知道怎么做吗?

您可以使用 bool/must 查询的组合

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "api": "A"
          }
        },
        {
          "match_phrase": {
            "api": "B"
          }
        },
        {
          "bool": {
            "must": [
              {
                "match_phrase": {
                  "api": "C"
                }
              },
              {
                "match_phrase": {
                  "another_field": "D"
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}