简体   繁体   中英

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

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
              ]
           }
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM