Geo-bounding box query

To search for documents that contain geopoint fields, use a geo-bounding box query. The geo-bounding box query returns documents whose geopoints are within the bounding box specified in the query. A document with multiple geopoints matches the query if at least one geopoint is within the bounding box.

Example

You can use a geo-bounding box query to search for documents that contain geopoints.

Create a mapping with the point field mapped as geo_point:

  1. PUT testindex1
  2. {
  3. "mappings": {
  4. "properties": {
  5. "point": {
  6. "type": "geo_point"
  7. }
  8. }
  9. }
  10. }

copy

Index three geopoints as objects with latitudes and longitudes:

  1. PUT testindex1/_doc/1
  2. {
  3. "point": {
  4. "lat": 74.00,
  5. "lon": 40.71
  6. }
  7. }

copy

  1. PUT testindex1/_doc/2
  2. {
  3. "point": {
  4. "lat": 72.64,
  5. "lon": 22.62
  6. }
  7. }

copy

  1. PUT testindex1/_doc/3
  2. {
  3. "point": {
  4. "lat": 75.00,
  5. "lon": 28.00
  6. }
  7. }

copy

Search for all documents and filter the documents whose points lie within the rectangle defined in the query:

  1. GET testindex1/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "point": {
  11. "top_left": {
  12. "lat": 75,
  13. "lon": 28
  14. },
  15. "bottom_right": {
  16. "lat": 73,
  17. "lon": 41
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

copy

The response contains the matching document:

  1. {
  2. "took" : 20,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "testindex1",
  19. "_id" : "1",
  20. "_score" : 1.0,
  21. "_source" : {
  22. "point" : {
  23. "lat" : 74.0,
  24. "lon" : 40.71
  25. }
  26. }
  27. }
  28. ]
  29. }
  30. }

The preceding response does not include the document with a geopoint of "lat": 75.00, "lon": 28.00 because of the geopoint’s limited precision.

Precision

Geopoint coordinates are always rounded down at index time. At query time, the upper boundaries of the bounding box are rounded down, and the lower boundaries are rounded up. Therefore, the documents with geopoints that lie on the lower and left edges of the bounding box might not be included in the results due to rounding error. On the other hand, geopoints that lie on the upper and right edges of the bounding box might be included in the results even though they are outside the boundaries. The rounding error is less than 4.20 × 10−8 degrees for latitude and less than 8.39 × 10−8 degrees for longitude (around 1 cm).

Specifying the bounding box

You can specify the bounding box by providing any of the following combinations of its vertex coordinates:

  • top_left and bottom_right
  • top_right and bottom_left
  • top, left, bottom, and right

The following example shows how to specify the bounding box using the top, left, bottom, and right coordinates:

  1. GET testindex1/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "point": {
  11. "top": 75,
  12. "left": 28,
  13. "bottom": 73,
  14. "right": 41
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }

copy

Parameters

Geo-bounding box queries accept the following parameters.

ParameterData typeDescription
_nameStringThe name of the filter. Optional.
validation_methodStringThe validation method. Valid values are IGNORE_MALFORMED (accept geopoints with invalid coordinates), COERCE (try to coerce coordinates to valid values), and STRICT (return an error when coordinates are invalid). Default is STRICT.
typeStringSpecifies how to execute the filter. Valid values are indexed (index the filter) and memory (execute the filter in memory). Default is memory.
ignore_unmappedBooleanSpecifies whether to ignore an unmapped field. If set to true, the query does not return any documents that have an unmapped field. If set to false, an exception is thrown when the field is unmapped. Default is false.

Accepted formats

You can specify coordinates of the bounding box vertices in any format that the geopoint field type accepts.

Using a geohash to specify the bounding box

If you use a geohash to specify the bounding box, the geohash is treated as a rectangle. The upper-left vertex of the bounding box corresponds to the upper-left vertex of the top_left geohash, and the lower-right vertex of the bounding box corresponds to the lower-right vertex of the bottom_right geohash.

The following example shows how to use a geohash to specify the same bounding box as the previous examples:

  1. GET testindex1/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "point": {
  11. "top_left": "ut7ftjkfxm34",
  12. "bottom_right": "uuvpkcprc4rc"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

copy

To specify a bounding box that covers the whole area of a geohash, provide that geohash as both top_left and bottom_right parameters of the bounding box:

  1. GET testindex1/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_bounding_box": {
  10. "point": {
  11. "top_left": "ut",
  12. "bottom_right": "ut"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

copy