Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch how can perform a "TERMS" AND "RANGE" query together

In elasticsearch, I am working well with Terms query to search multiple ID in one query, my original terms query

{
  "query": {
    "terms": {
      "Id":  ["134","156"],
      
    }
  }
}

however, I need to add an extra condition like the following:

{   
    "query": {
        "terms": {
            "id": ["163","121","569","579"]
                },        
        
            "range":{
                    "age":
                    {"gt":10}
                }
            }            
}  

the "id" field can be a long array.

like image 939
Man Man Yu Avatar asked Sep 06 '25 15:09

Man Man Yu


1 Answers

You can combine both the queries using bool query

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "Id": [
              "134",
              "156"
            ]
          }
        },
        {
          "range": {
            "age": {
              "gt": 10
            }
          }
        }
      ]
    }
  }
}
like image 97
ESCoder Avatar answered Sep 09 '25 16:09

ESCoder