GeoHex grid aggregations

The Hexagonal Hierarchical Geospatial Indexing System (H3) partitions the Earth’s areas into identifiable hexagon-shaped cells.

The H3 grid system works well for proximity applications because it overcomes the limitations of Geohash’s non-uniform partitions. Geohash encodes latitude and longitude pairs, leading to significantly smaller partitions near the poles and a degree of longitude near the equator. However, the H3 grid system’s distortions are low and limited to 5 partitions of 122. These five partitions are placed in low-use areas (for example, in the middle of the ocean), leaving the essential areas error free. Thus, grouping documents based on the H3 grid system provides a better aggregation than the Geohash grid.

The GeoHex grid aggregation groups geopoints into grid cells for geographical analysis. Each grid cell corresponds to an H3 cell and is identified using the H3Index representation.

Precision

The precision parameter controls the level of granularity that determines the grid cell size. The lower the precision, the larger the grid cells.

The following example illustrates low-precision and high-precision aggregation requests.

To start, create an index and map the location field as a geo_point:

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

Index the following documents into the sample index:

  1. PUT national_parks/_doc/1
  2. {
  3. "name": "Yellowstone National Park",
  4. "location": "44.42, -110.59"
  5. }
  6. PUT national_parks/_doc/2
  7. {
  8. "name": "Yosemite National Park",
  9. "location": "37.87, -119.53"
  10. }
  11. PUT national_parks/_doc/3
  12. {
  13. "name": "Death Valley National Park",
  14. "location": "36.53, -116.93"
  15. }

You can index geopoints in several formats. For a list of all supported formats, see the geopoint documentation.

Low-precision requests

Run a low-precision request that buckets all three documents together:

  1. GET national_parks/_search
  2. {
  3. "aggregations": {
  4. "grouped": {
  5. "geohex_grid": {
  6. "field": "location",
  7. "precision": 1
  8. }
  9. }
  10. }
  11. }

You can use either the GET or POST HTTP method for GeoHex grid aggregation queries.

The response groups documents 2 and 3 together because they are close enough to be bucketed in one grid cell:

  1. {
  2. "took" : 4,
  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" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "national_parks",
  19. "_id" : "1",
  20. "_score" : 1.0,
  21. "_source" : {
  22. "name" : "Yellowstone National Park",
  23. "location" : "44.42, -110.59"
  24. }
  25. },
  26. {
  27. "_index" : "national_parks",
  28. "_id" : "2",
  29. "_score" : 1.0,
  30. "_source" : {
  31. "name" : "Yosemite National Park",
  32. "location" : "37.87, -119.53"
  33. }
  34. },
  35. {
  36. "_index" : "national_parks",
  37. "_id" : "3",
  38. "_score" : 1.0,
  39. "_source" : {
  40. "name" : "Death Valley National Park",
  41. "location" : "36.53, -116.93"
  42. }
  43. }
  44. ]
  45. },
  46. "aggregations" : {
  47. "grouped" : {
  48. "buckets" : [
  49. {
  50. "key" : "8129bffffffffff",
  51. "doc_count" : 2
  52. },
  53. {
  54. "key" : "8128bffffffffff",
  55. "doc_count" : 1
  56. }
  57. ]
  58. }
  59. }
  60. }

High-precision requests

Now run a high-precision request:

  1. GET national_parks/_search
  2. {
  3. "aggregations": {
  4. "grouped": {
  5. "geohex_grid": {
  6. "field": "location",
  7. "precision": 6
  8. }
  9. }
  10. }
  11. }

All three documents are bucketed separately because of higher granularity:

  1. {
  2. "took" : 5,
  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" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "national_parks",
  19. "_id" : "1",
  20. "_score" : 1.0,
  21. "_source" : {
  22. "name" : "Yellowstone National Park",
  23. "location" : "44.42, -110.59"
  24. }
  25. },
  26. {
  27. "_index" : "national_parks",
  28. "_id" : "2",
  29. "_score" : 1.0,
  30. "_source" : {
  31. "name" : "Yosemite National Park",
  32. "location" : "37.87, -119.53"
  33. }
  34. },
  35. {
  36. "_index" : "national_parks",
  37. "_id" : "3",
  38. "_score" : 1.0,
  39. "_source" : {
  40. "name" : "Death Valley National Park",
  41. "location" : "36.53, -116.93"
  42. }
  43. }
  44. ]
  45. },
  46. "aggregations" : {
  47. "grouped" : {
  48. "buckets" : [
  49. {
  50. "key" : "8629ab6dfffffff",
  51. "doc_count" : 1
  52. },
  53. {
  54. "key" : "8629857a7ffffff",
  55. "doc_count" : 1
  56. },
  57. {
  58. "key" : "862896017ffffff",
  59. "doc_count" : 1
  60. }
  61. ]
  62. }
  63. }
  64. }

Filtering requests

High-precision requests are resource intensive, so we recommend using a filter like geo_bounding_box to limit the geographical area. For example, the following query applies a filter to limit the search area:

  1. GET national_parks/_search
  2. {
  3. "size" : 0,
  4. "aggregations": {
  5. "filtered": {
  6. "filter": {
  7. "geo_bounding_box": {
  8. "location": {
  9. "top_left": "38, -120",
  10. "bottom_right": "36, -116"
  11. }
  12. }
  13. },
  14. "aggregations": {
  15. "grouped": {
  16. "geohex_grid": {
  17. "field": "location",
  18. "precision": 6
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

The response contains the two documents that are within the geo_bounding_box bounds:

  1. {
  2. "took" : 4,
  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" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "filtered" : {
  20. "doc_count" : 2,
  21. "grouped" : {
  22. "buckets" : [
  23. {
  24. "key" : "8629ab6dfffffff",
  25. "doc_count" : 1
  26. },
  27. {
  28. "key" : "8629857a7ffffff",
  29. "doc_count" : 1
  30. }
  31. ]
  32. }
  33. }
  34. }
  35. }

You can also restrict the geographical area by providing the coordinates of the bounding envelope in the bounds parameter. Both bounds and geo_bounding_box coordinates can be specified in any of the geopoint formats. The following query uses the well-known text (WKT) “POINT(longitude latitude)” format for the bounds parameter:

  1. GET national_parks/_search
  2. {
  3. "size": 0,
  4. "aggregations": {
  5. "grouped": {
  6. "geohex_grid": {
  7. "field": "location",
  8. "precision": 6,
  9. "bounds": {
  10. "top_left": "POINT (-120 38)",
  11. "bottom_right": "POINT (-116 36)"
  12. }
  13. }
  14. }
  15. }
  16. }

The response contains only the two results that are within the specified bounds:

  1. {
  2. "took" : 3,
  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" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "grouped" : {
  20. "buckets" : [
  21. {
  22. "key" : "8629ab6dfffffff",
  23. "doc_count" : 1
  24. },
  25. {
  26. "key" : "8629857a7ffffff",
  27. "doc_count" : 1
  28. }
  29. ]
  30. }
  31. }
  32. }

The bounds parameter can be used with or without the geo_bounding_box filter; these two parameters are independent and can have any spatial relationship to each other.

Supported parameters

GeoHex grid aggregation requests support the following parameters.

ParameterData typeDescription
fieldStringThe field that contains the geopoints. This field must be mapped as a geo_point field. If the field contains an array, all array values are aggregated. Required.
precisionIntegerThe zoom level used to determine grid cells for bucketing results. Valid values are in the [0, 15] range. Optional. Default is 5.
boundsObjectThe bounding box for filtering geopoints. The bounding box is defined by the top left and bottom right vertices. The vertices are specified as geopoints in one of the following formats:
- An object with a latitude and longitude
- An array in the [longitude, latitude] format
- A string in the “latitude,longitude” format
- A Geohash
- WKT
See the geopoint formats for formatting examples. Optional.
sizeIntegerThe maximum number of buckets to return. When there are more buckets than size, OpenSearch returns buckets with more documents. Optional. Default is 10,000.
shard_sizeIntegerThe maximum number of buckets to return from each shard. Optional. Default is max (10, size · number of shards), which provides a more accurate count of more highly prioritized buckets.

GeoHex grid aggregations - 图1