Geo-bounding box query

A query allowing to filter hits based on a point location using a bounding box. Assuming the following 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_bounding_box filter:

  1. GET my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "pin.location": {
  11. "top_left": {
  12. "lat": 40.73,
  13. "lon": -74.1
  14. },
  15. "bottom_right": {
  16. "lat": 40.01,
  17. "lon": -71.12
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

Query Options

OptionDescription

_name

Optional name field to identify the filter

validation_method

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

type

Set to one of indexed or memory to defines whether this filter will be executed in memory or indexed. See Type below for further details Default is memory.

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_bounding_box": {
  10. "pin.location": {
  11. "top_left": {
  12. "lat": 40.73,
  13. "lon": -74.1
  14. },
  15. "bottom_right": {
  16. "lat": 40.01,
  17. "lon": -71.12
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
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_bounding_box": {
  10. "pin.location": {
  11. "top_left": [ -74.1, 40.73 ],
  12. "bottom_right": [ -71.12, 40.01 ]
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }
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_bounding_box": {
  10. "pin.location": {
  11. "top_left": "40.73, -74.1",
  12. "bottom_right": "40.01, -71.12"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }
Bounding Box as Well-Known Text (WKT)
  1. GET my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "pin.location": {
  11. "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
Geohash
  1. GET my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "pin.location": {
  11. "top_left": "dr5r9ydj2y73",
  12. "bottom_right": "drj7teegpus6"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

When geohashes are used to specify the bounding the edges of the bounding box, the geohashes are treated as rectangles. The bounding box is defined in such a way that its top left corresponds to the top left corner of the geohash specified in the top_left parameter and its bottom right is defined as the bottom right of the geohash specified in the bottom_right parameter.

In order to specify a bounding box that would match entire area of a geohash the geohash can be specified in both top_left and bottom_right parameters:

  1. GET my_locations/_search
  2. {
  3. "query": {
  4. "geo_bounding_box": {
  5. "pin.location": {
  6. "top_left": "dr",
  7. "bottom_right": "dr"
  8. }
  9. }
  10. }
  11. }

In this example, the geohash dr will produce the bounding box query with the top left corner at 45.0,-78.75 and the bottom right corner at 39.375,-67.5.

Vertices

The vertices of the bounding box can either be set by top_left and bottom_right or by top_right and bottom_left parameters. More over the names topLeft, bottomRight, topRight and bottomLeft are supported. Instead of setting the values pairwise, one can use the simple names top, left, bottom and right to set the values separately.

  1. GET my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "pin.location": {
  11. "top": 40.73,
  12. "left": -74.1,
  13. "bottom": 40.01,
  14. "right": -71.12
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }

geo_point Type

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

Multi Location Per Document

The 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

Type

The type of the bounding box execution by default is set to memory, which means in memory checks if the doc falls within the bounding box range. In some cases, an indexed option will perform faster (but note that the geo_point type must have lat and lon indexed in this case). Note, when using the indexed option, multi locations per document field are not supported. Here is an example:

  1. GET my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "pin.location": {
  11. "top_left": {
  12. "lat": 40.73,
  13. "lon": -74.1
  14. },
  15. "bottom_right": {
  16. "lat": 40.10,
  17. "lon": -71.12
  18. }
  19. },
  20. "type": "indexed"
  21. }
  22. }
  23. }
  24. }
  25. }

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.

Notes on Precision

Geopoints have limited precision and are always rounded down during index time. During the query time, upper boundaries of the bounding boxes are rounded down, while lower boundaries are rounded up. As a result, the points along on the lower bounds (bottom and left edges of the bounding box) might not make it into the bounding box due to the rounding error. At the same time points alongside the upper bounds (top and right edges) might be selected by the query even if they are located slightly outside the edge. The rounding error should be less than 4.20e-8 degrees on the latitude and less than 8.39e-8 degrees on the longitude, which translates to less than 1cm error even at the equator.