用于过滤的 Elasticsearch DSL bool 查询

时间:2021-05-10 08:30:06

标签: elasticsearch kibana

我有一个如下的 Elasticsearch DSL 查询

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

据我所知,这类似于

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

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

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

1 个答案:

答案 0 :(得分:1)

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