首页 > 解决方案 > 用于过滤的 Elasticsearch DSL bool 查询

问题描述

我有一个 Elasticsearch DSL 查询如下

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

据我了解,这就像

匹配 if (api = A 或 api = B 或 api = C)

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

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

标签: elasticsearchkibana

解决方案


您可以使用组合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
    }
  }
}

推荐阅读