Geo Bounds Aggregation

A metric aggregation that computes the bounding box containing all geo values for a field.

Example:

  1. PUT /museums
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. }
  10. }
  11. POST /museums/_bulk?refresh
  12. {"index":{"_id":1}}
  13. {"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
  14. {"index":{"_id":2}}
  15. {"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
  16. {"index":{"_id":3}}
  17. {"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
  18. {"index":{"_id":4}}
  19. {"location": "51.222900,4.405200", "name": "Letterenhuis"}
  20. {"index":{"_id":5}}
  21. {"location": "48.861111,2.336389", "name": "Musée du Louvre"}
  22. {"index":{"_id":6}}
  23. {"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
  24. POST /museums/_search?size=0
  25. {
  26. "query": {
  27. "match": { "name": "musée" }
  28. },
  29. "aggs": {
  30. "viewport": {
  31. "geo_bounds": {
  32. "field": "location",
  33. "wrap_longitude": true
  34. }
  35. }
  36. }
  37. }

The geo_bounds aggregation specifies the field to use to obtain the bounds.

wrap_longitude is an optional parameter which specifies whether the bounding box should be allowed to overlap the international date line. The default value is true.

The above aggregation demonstrates how one would compute the bounding box of the location field for all documents with a business type of shop.

The response for the above aggregation:

  1. {
  2. ...
  3. "aggregations": {
  4. "viewport": {
  5. "bounds": {
  6. "top_left": {
  7. "lat": 48.86111099738628,
  8. "lon": 2.3269999679178
  9. },
  10. "bottom_right": {
  11. "lat": 48.85999997612089,
  12. "lon": 2.3363889567553997
  13. }
  14. }
  15. }
  16. }
  17. }

Geo Bounds Aggregation on geo_shape fields

The Geo Bounds Aggregation is also supported on geo_shape fields.

If wrap_longitude is set to true (the default), the bounding box can overlap the international date line and return a bounds where the top_left longitude is larger than the top_right longitude.

For example, the upper right longitude will typically be greater than the lower left longitude of a geographic bounding box. However, when the area crosses the 180° meridian, the value of the lower left longitude will be greater than the value of the upper right longitude. See Geographic bounding box on the Open Geospatial Consortium website for more information.

Example:

  1. PUT /places
  2. {
  3. "mappings": {
  4. "properties": {
  5. "geometry": {
  6. "type": "geo_shape"
  7. }
  8. }
  9. }
  10. }
  11. POST /places/_bulk?refresh
  12. {"index":{"_id":1}}
  13. {"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
  14. {"index":{"_id":2}}
  15. {"name": "Sportpark De Weeren", "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.965305328369141, 52.39347642069457 ], [ 4.966979026794433, 52.391721758934835 ], [ 4.969425201416015, 52.39238958618537 ], [ 4.967944622039794, 52.39420969150824 ], [ 4.965305328369141, 52.39347642069457 ] ] ] } }
  16. POST /places/_search?size=0
  17. {
  18. "aggs": {
  19. "viewport": {
  20. "geo_bounds": {
  21. "field": "geometry"
  22. }
  23. }
  24. }
  25. }
  1. {
  2. ...
  3. "aggregations": {
  4. "viewport": {
  5. "bounds": {
  6. "top_left": {
  7. "lat": 52.39420966710895,
  8. "lon": 4.912349972873926
  9. },
  10. "bottom_right": {
  11. "lat": 52.374080987647176,
  12. "lon": 4.969425117596984
  13. }
  14. }
  15. }
  16. }
  17. }