GeoTile Grid Aggregation

A multi-bucket aggregation that works on geo_point fields and groups points into buckets that represent cells in a grid. The resulting grid can be sparse and only contains cells that have matching data. Each cell corresponds to a map tile as used by many online map sites. Each cell is labeled using a “{zoom}/{x}/{y}” format, where zoom is equal to the user-specified precision.

  • High precision keys have a larger range for x and y, and represent tiles that cover only a small area.
  • Low precision keys have a smaller range for x and y, and represent tiles that each cover a large area.

See Zoom level documentation on how precision (zoom) correlates to size on the ground. Precision for this aggregation can be between 0 and 29, inclusive.

The highest-precision geotile of length 29 produces cells that cover less than a 10cm by 10cm of land and so high-precision requests can be very costly in terms of RAM and result sizes. Please see the example below on how to first filter the aggregation to a smaller geographic area before requesting high-levels of detail.

The specified field must be of type geo_point (which can only be set explicitly in the mappings) and it can also hold an array of geo_point fields, in which case all points will be taken into account during aggregation.

Simple low-precision request

  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. "aggregations": {
  27. "large-grid": {
  28. "geotile_grid": {
  29. "field": "location",
  30. "precision": 8
  31. }
  32. }
  33. }
  34. }

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "large-grid": {
  5. "buckets": [
  6. {
  7. "key": "8/131/84",
  8. "doc_count": 3
  9. },
  10. {
  11. "key": "8/129/88",
  12. "doc_count": 2
  13. },
  14. {
  15. "key": "8/131/85",
  16. "doc_count": 1
  17. }
  18. ]
  19. }
  20. }
  21. }

High-precision requests

When requesting detailed buckets (typically for displaying a “zoomed in” map) a filter like geo_bounding_box should be applied to narrow the subject area otherwise potentially millions of buckets will be created and returned.

  1. POST /museums/_search?size=0
  2. {
  3. "aggregations": {
  4. "zoomed-in": {
  5. "filter": {
  6. "geo_bounding_box": {
  7. "location": {
  8. "top_left": "52.4, 4.9",
  9. "bottom_right": "52.3, 5.0"
  10. }
  11. }
  12. },
  13. "aggregations": {
  14. "zoom1": {
  15. "geotile_grid": {
  16. "field": "location",
  17. "precision": 22
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  1. {
  2. ...
  3. "aggregations": {
  4. "zoomed-in": {
  5. "doc_count": 3,
  6. "zoom1": {
  7. "buckets": [
  8. {
  9. "key": "22/2154412/1378379",
  10. "doc_count": 1
  11. },
  12. {
  13. "key": "22/2154385/1378332",
  14. "doc_count": 1
  15. },
  16. {
  17. "key": "22/2154259/1378425",
  18. "doc_count": 1
  19. }
  20. ]
  21. }
  22. }
  23. }
  24. }

Requests with additional bounding box filtering

The geotile_grid aggregation supports an optional bounds parameter that restricts the points considered to those that fall within the bounds provided. The bounds parameter accepts the bounding box in all the same accepted formats of the bounds specified in the Geo Bounding Box Query. This bounding box can be used with or without an additional geo_bounding_box query filtering the points prior to aggregating. It is an independent bounding box that can intersect with, be equal to, or be disjoint to any additional geo_bounding_box queries defined in the context of the aggregation.

  1. POST /museums/_search?size=0
  2. {
  3. "aggregations": {
  4. "tiles-in-bounds": {
  5. "geotile_grid": {
  6. "field": "location",
  7. "precision": 22,
  8. "bounds": {
  9. "top_left": "52.4, 4.9",
  10. "bottom_right": "52.3, 5.0"
  11. }
  12. }
  13. }
  14. }
  15. }
  1. {
  2. ...
  3. "aggregations": {
  4. "tiles-in-bounds": {
  5. "buckets": [
  6. {
  7. "key": "22/2154412/1378379",
  8. "doc_count": 1
  9. },
  10. {
  11. "key": "22/2154385/1378332",
  12. "doc_count": 1
  13. },
  14. {
  15. "key": "22/2154259/1378425",
  16. "doc_count": 1
  17. }
  18. ]
  19. }
  20. }
  21. }

Aggregating geo_shape fields

Aggregating on Geo-shape fields works just as it does for points, except that a single shape can be counted for in multiple tiles. A shape will contribute to the count of matching values if any part of its shape intersects with that tile. Below is an image that demonstrates this:

geoshape grid

Options

field

Mandatory. The name of the field indexed with GeoPoints.

precision

Optional. The integer zoom of the key used to define cells/buckets in the results. Defaults to 7. Values outside of [0,29] will be rejected.

bounds: Optional. The bounding box to filter the points in the bucket.

size

Optional. The maximum number of geohash buckets to return (defaults to 10,000). When results are trimmed, buckets are prioritised based on the volumes of documents they contain.

shard_size

Optional. To allow for more accurate counting of the top cells returned in the final result the aggregation defaults to returning max(10,(size x number-of-shards)) buckets from each shard. If this heuristic is undesirable, the number considered from each shard can be over-ridden using this parameter.