Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Elasticsearch filter increases results instead of reducing them

I am trying to build the following filter logic:

at least one geolocation must be truthy
AND
The status must be equal to 1.

Right now when I add the term query for status the number of results increases, where as it is expected for it to be the same.
All records in the sample index I am using have status equal to 1.

GET /my-index/_search
{
   "query":{
      "bool":{
         "filter":{
            "bool":{
               "must":[
                  {
                     "term":{
                        "status": "1"
                     }
                  }
               ],
               "should":[
                  {
                     "geo_distance":{
                        "distance":"24km",
                        "locations":{
                           "lat":11.11,
                           "lon":13.52
                        }
                     }
                  },
                  {
                     "geo_distance":{
                        "distance":"24km",
                        "locations":{
                           "lat":81.11,
                           "lon":43.52
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

U can do two things.

Filter + Should (I would choose)

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "status": "1"
          }
        }
      ],
      "should": [
        {
          "geo_distance": {
            "distance": "24km",
            "locations": {
              "lat": 11.11,
              "lon": 13.52
            }
          }
        },
        {
          "geo_distance": {
            "distance": "24km",
            "locations": {
              "lat": 81.11,
              "lon": 43.52
            }
          }
        }
      ]
    }
  }
}

Use minimum_should_match

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "status": "1"
          }
        }
      ],
      "minimum_should_match": 1,
      "should": [
        {
          "geo_distance": {
            "distance": "24km",
            "locations": {
              "lat": 11.11,
              "lon": 13.52
            }
          }
        },
        {
          "geo_distance": {
            "distance": "24km",
            "locations": {
              "lat": 81.11,
              "lon": 43.52
            }
          }
        }
      ]
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading