Geo-distance query

Filters documents that include only hits that exists within a specific distance from a geo point. Assuming the following mapping and indexed document:

  1. PUT /my_locations
  2. {
  3. "mappings": {
  4. "properties": {
  5. "pin": {
  6. "properties": {
  7. "location": {
  8. "type": "geo_point"
  9. }
  10. }
  11. }
  12. }
  13. }
  14. }
  15. PUT /my_locations/_doc/1
  16. {
  17. "pin": {
  18. "location": {
  19. "lat": 40.12,
  20. "lon": -71.34
  21. }
  22. }
  23. }

Then the following simple query can be executed with a geo_distance filter:

  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "200km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

Accepted Formats

In much the same way the geo_point type can accept different representations of the geo point, the filter can accept it as well:

Lat Lon As Properties
  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }
Lat Lon As Array

Format in [lon, lat], note, the order of lon/lat here in order to conform with GeoJSON.

  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": [ -70, 40 ]
  12. }
  13. }
  14. }
  15. }
  16. }
Lat Lon As String

Format in lat,lon.

  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": "40,-70"
  12. }
  13. }
  14. }
  15. }
  16. }
Geohash
  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": "drm3btev3e86"
  12. }
  13. }
  14. }
  15. }
  16. }

Options

The following are options allowed on the filter:

distance

The radius of the circle centred on the specified location. Points which fall into this circle are considered to be matches. The distance can be specified in various units. See Distance Units.

distance_type

How to compute the distance. Can either be arc (default), or plane (faster, but inaccurate on long distances and close to the poles).

_name

Optional name field to identify the query

validation_method

Set to IGNORE_MALFORMED to accept geo points with invalid latitude or longitude, set to COERCE to additionally try and infer correct coordinates (default is STRICT).

geo_point Type

The filter requires the geo_point type to be set on the relevant field.

Multi Location Per Document

The geo_distance filter can work with multiple locations / points per document. Once a single location / point matches the filter, the document will be included in the filter.

Ignore Unmapped

When set to true the ignore_unmapped option will ignore an unmapped field and will not match any documents for this query. This can be useful when querying multiple indexes which might have different mappings. When set to false (the default value) the query will throw an exception if the field is not mapped.