Kiran February 2016

Elastic Search Query using an OR operator inside a bool

I need to build a simple query which translates to a condition as "age" is between 40 - 60 and "country" is US or IN . I am searching for a solution using a bool operator.

I tried with the below option:

    "bool": {
        "must": [
           {
               "range": {
                  "age": {
                     "from": 40,
                     "to": 60
                  }
               }
           },
           {
               "terms": {
                  "country": [
                     "US",
                     "IN"
                  ]
               }
            }
        ]
    }

But this query is not giving me any result. Am I missing something here?

Thanks! K

Answers


bittusarkar February 2016

Your query looks fine. The only reason I think your query is not working is that the "coutry" field is using the default analyzer where the term is lower-cased and stored in the index. If you get expected results for the below query, my suspicion is correct. To fix it, either you can change your query to the one I've mentioned below or change your mapping to make the field "country" non-analyzed.

"bool": {
    "must": [
       {
           "range": {
              "age": {
                 "from": 40,
                 "to": 60
              }
           }
       },
       {
           "terms": {
              "country": [
                 "us",    <---- Note that I'm using lower-cased terms
                 "in"     <---- Note that I'm using lower-cased terms
              ]
           }
        }
    ]
}

Post Status

Asked in February 2016
Viewed 3,804 times
Voted 9
Answered 1 times

Search




Leave an answer