geohashgrid-aggregation.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. [[search-aggregations-bucket-geohashgrid-aggregation]]
  2. === GeoHash grid Aggregation
  3. A multi-bucket aggregation that works on `geo_point` fields and groups points into buckets that represent cells in a grid.
  4. The resulting grid can be sparse and only contains cells that have matching data. Each cell is labeled using a http://en.wikipedia.org/wiki/Geohash[geohash] which is of user-definable precision.
  5. * High precision geohashes have a long string length and represent cells that cover only a small area.
  6. * Low precision geohashes have a short string length and represent cells that each cover a large area.
  7. Geohashes used in this aggregation can have a choice of precision between 1 and 12.
  8. WARNING: The highest-precision geohash of length 12 produces cells that cover less than a square metre of land and so high-precision requests can be very costly in terms of RAM and result sizes.
  9. Please see the example below on how to first filter the aggregation to a smaller geographic area before requesting high-levels of detail.
  10. 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.
  11. ==== Simple low-precision request
  12. [source,js]
  13. --------------------------------------------------
  14. PUT /museums
  15. {
  16. "mappings": {
  17. "_doc": {
  18. "properties": {
  19. "location": {
  20. "type": "geo_point"
  21. }
  22. }
  23. }
  24. }
  25. }
  26. POST /museums/_doc/_bulk?refresh
  27. {"index":{"_id":1}}
  28. {"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
  29. {"index":{"_id":2}}
  30. {"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
  31. {"index":{"_id":3}}
  32. {"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
  33. {"index":{"_id":4}}
  34. {"location": "51.222900,4.405200", "name": "Letterenhuis"}
  35. {"index":{"_id":5}}
  36. {"location": "48.861111,2.336389", "name": "Musée du Louvre"}
  37. {"index":{"_id":6}}
  38. {"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
  39. POST /museums/_search?size=0
  40. {
  41. "aggregations" : {
  42. "large-grid" : {
  43. "geohash_grid" : {
  44. "field" : "location",
  45. "precision" : 3
  46. }
  47. }
  48. }
  49. }
  50. --------------------------------------------------
  51. // CONSOLE
  52. Response:
  53. [source,js]
  54. --------------------------------------------------
  55. {
  56. ...
  57. "aggregations": {
  58. "large-grid": {
  59. "buckets": [
  60. {
  61. "key": "u17",
  62. "doc_count": 3
  63. },
  64. {
  65. "key": "u09",
  66. "doc_count": 2
  67. },
  68. {
  69. "key": "u15",
  70. "doc_count": 1
  71. }
  72. ]
  73. }
  74. }
  75. }
  76. --------------------------------------------------
  77. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  78. ==== High-precision requests
  79. When requesting detailed buckets (typically for displaying a "zoomed in" map) a filter like <<query-dsl-geo-bounding-box-query,geo_bounding_box>> should be applied to narrow the subject area otherwise potentially millions of buckets will be created and returned.
  80. [source,js]
  81. --------------------------------------------------
  82. POST /museums/_search?size=0
  83. {
  84. "aggregations" : {
  85. "zoomed-in" : {
  86. "filter" : {
  87. "geo_bounding_box" : {
  88. "location" : {
  89. "top_left" : "52.4, 4.9",
  90. "bottom_right" : "52.3, 5.0"
  91. }
  92. }
  93. },
  94. "aggregations":{
  95. "zoom1":{
  96. "geohash_grid" : {
  97. "field": "location",
  98. "precision": 8
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. --------------------------------------------------
  106. // CONSOLE
  107. // TEST[continued]
  108. ==== Cell dimensions at the equator
  109. The table below shows the metric dimensions for cells covered by various string lengths of geohash.
  110. Cell dimensions vary with latitude and so the table is for the worst-case scenario at the equator.
  111. [horizontal]
  112. *GeoHash length*:: *Area width x height*
  113. 1:: 5,009.4km x 4,992.6km
  114. 2:: 1,252.3km x 624.1km
  115. 3:: 156.5km x 156km
  116. 4:: 39.1km x 19.5km
  117. 5:: 4.9km x 4.9km
  118. 6:: 1.2km x 609.4m
  119. 7:: 152.9m x 152.4m
  120. 8:: 38.2m x 19m
  121. 9:: 4.8m x 4.8m
  122. 10:: 1.2m x 59.5cm
  123. 11:: 14.9cm x 14.9cm
  124. 12:: 3.7cm x 1.9cm
  125. ==== Options
  126. [horizontal]
  127. field:: Mandatory. The name of the field indexed with GeoPoints.
  128. precision:: Optional. The string length of the geohashes used to define
  129. cells/buckets in the results. Defaults to 5.
  130. The precision can either be defined in terms of the integer
  131. precision levels mentioned above. Values outside of [1,12] will
  132. be rejected.
  133. Alternatively, the precision level can be approximated from a
  134. distance measure like "1km", "10m". The precision level is
  135. calculate such that cells will not exceed the specified
  136. size (diagonal) of the required precision. When this would lead
  137. to precision levels higher than the supported 12 levels,
  138. (e.g. for distances <5.6cm) the value is rejected.
  139. size:: Optional. The maximum number of geohash buckets to return
  140. (defaults to 10,000). When results are trimmed, buckets are
  141. prioritised based on the volumes of documents they contain.
  142. shard_size:: Optional. To allow for more accurate counting of the top cells
  143. returned in the final result the aggregation defaults to
  144. returning `max(10,(size x number-of-shards))` buckets from each
  145. shard. If this heuristic is undesirable, the number considered
  146. from each shard can be over-ridden using this parameter.