Home > database >  ElasticSearch Term Query for multiple fields
ElasticSearch Term Query for multiple fields

Time:12-01

My below elasticsearch query responding me with 0 records. While, if I queried upon Approved or Declined separately, it is giving me the exact result which I want.

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

CodePudding user response:

If you want OR semantics, then you have two options:

Option 1: bool/should query:

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

Option 2: terms query:

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