多个字段的 ElasticSearch 术语查询

ElasticSearch Term Query for multiple fields

我的以下 elasticsearch 查询以 0 条记录响应我。然而,如果我分别查询 ApprovedDeclined,它会给出我想要的确切结果。

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "responseType": "Approved"
          }
        },
        {
          "term": {
            "responseType": "Declined"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

如果你想要OR语义,那么你有两个选择:

选项 1:bool/should 查询:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "term": {
            "responseType.keyword": "Approved"
          }
        },
        {
          "term": {
            "responseType.keyword": "Declined"
          }
        }
      ],
      "must_not": [],
      "must": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

选项 2:terms 查询:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "responseType.keyword": ["Approved", "Declined"]
          }
        }
      ],
      "must": [],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}