ElasticSearch find document exact match using lat/long

Viewed 1343

I have this field mapping in my index:

 "businessLocation": {
            "type": "geo_point"
          },

I wanted to find documents using exact match with my lat/long coordinates. I've tried geo_distance however, there's a requirement that my distance be greater than 0.

I even tried this query:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "businessLocation",
            "query": "src.lat = 34.47  AND src.lon = -122.37"
          }
        }
      ]
    }
  }
}

However, I got this error:

Geo fields do not support exact searching, use dedicated geo queries instead: [businessLocation]
1 Answers

You need to query geo fields in a different manner. This is what the error states. Please see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html for more information.

If you need hits close to your criteria, consider lowering the distance parameter of the distance query but keep the https://wiki.openstreetmap.org/wiki/Precision_of_coordinates in mind.

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "2km",
                    "businessLocation" : {
                        "lat" : 34.47,
                        "lon" : -122.37
                    }
                }
            }
        }
    }
}
Related