geohashgrid-aggregation.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. "properties": {
  18. "location": {
  19. "type": "geo_point"
  20. }
  21. }
  22. }
  23. }
  24. POST /museums/_bulk?refresh
  25. {"index":{"_id":1}}
  26. {"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
  27. {"index":{"_id":2}}
  28. {"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
  29. {"index":{"_id":3}}
  30. {"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
  31. {"index":{"_id":4}}
  32. {"location": "51.222900,4.405200", "name": "Letterenhuis"}
  33. {"index":{"_id":5}}
  34. {"location": "48.861111,2.336389", "name": "Musée du Louvre"}
  35. {"index":{"_id":6}}
  36. {"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
  37. POST /museums/_search?size=0
  38. {
  39. "aggregations" : {
  40. "large-grid" : {
  41. "geohash_grid" : {
  42. "field" : "location",
  43. "precision" : 3
  44. }
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. // CONSOLE
  50. Response:
  51. [source,js]
  52. --------------------------------------------------
  53. {
  54. ...
  55. "aggregations": {
  56. "large-grid": {
  57. "buckets": [
  58. {
  59. "key": "u17",
  60. "doc_count": 3
  61. },
  62. {
  63. "key": "u09",
  64. "doc_count": 2
  65. },
  66. {
  67. "key": "u15",
  68. "doc_count": 1
  69. }
  70. ]
  71. }
  72. }
  73. }
  74. --------------------------------------------------
  75. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  76. ==== High-precision requests
  77. 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.
  78. [source,js]
  79. --------------------------------------------------
  80. POST /museums/_search?size=0
  81. {
  82. "aggregations" : {
  83. "zoomed-in" : {
  84. "filter" : {
  85. "geo_bounding_box" : {
  86. "location" : {
  87. "top_left" : "52.4, 4.9",
  88. "bottom_right" : "52.3, 5.0"
  89. }
  90. }
  91. },
  92. "aggregations":{
  93. "zoom1":{
  94. "geohash_grid" : {
  95. "field": "location",
  96. "precision": 8
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. --------------------------------------------------
  104. // CONSOLE
  105. // TEST[continued]
  106. The geohashes returned by the `geohash_grid` aggregation can be also used for zooming in. To zoom into the
  107. first geohash `u17` returned in the previous example, it should be specified as both `top_left` and `bottom_right` corner:
  108. [source,js]
  109. --------------------------------------------------
  110. POST /museums/_search?size=0
  111. {
  112. "aggregations" : {
  113. "zoomed-in" : {
  114. "filter" : {
  115. "geo_bounding_box" : {
  116. "location" : {
  117. "top_left" : "u17",
  118. "bottom_right" : "u17"
  119. }
  120. }
  121. },
  122. "aggregations":{
  123. "zoom1":{
  124. "geohash_grid" : {
  125. "field": "location",
  126. "precision": 8
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. --------------------------------------------------
  134. // CONSOLE
  135. // TEST[continued]
  136. [source,js]
  137. --------------------------------------------------
  138. {
  139. ...
  140. "aggregations" : {
  141. "zoomed-in" : {
  142. "doc_count" : 3,
  143. "zoom1" : {
  144. "buckets" : [
  145. {
  146. "key" : "u173zy3j",
  147. "doc_count" : 1
  148. },
  149. {
  150. "key" : "u173zvfz",
  151. "doc_count" : 1
  152. },
  153. {
  154. "key" : "u173zt90",
  155. "doc_count" : 1
  156. }
  157. ]
  158. }
  159. }
  160. }
  161. }
  162. --------------------------------------------------
  163. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  164. For "zooming in" on the system that don't support geohashes, the bucket keys should be translated into bounding boxes using
  165. one of available geohash libraries. For example, for javascript the https://github.com/sunng87/node-geohash[node-geohash] library
  166. can be used:
  167. [source,js]
  168. --------------------------------------------------
  169. var geohash = require('ngeohash');
  170. // bbox will contain [ 52.03125, 4.21875, 53.4375, 5.625 ]
  171. // [ minlat, minlon, maxlat, maxlon]
  172. var bbox = geohash.decode_bbox('u17');
  173. --------------------------------------------------
  174. // NOTCONSOLE
  175. ==== Cell dimensions at the equator
  176. The table below shows the metric dimensions for cells covered by various string lengths of geohash.
  177. Cell dimensions vary with latitude and so the table is for the worst-case scenario at the equator.
  178. [horizontal]
  179. *GeoHash length*:: *Area width x height*
  180. 1:: 5,009.4km x 4,992.6km
  181. 2:: 1,252.3km x 624.1km
  182. 3:: 156.5km x 156km
  183. 4:: 39.1km x 19.5km
  184. 5:: 4.9km x 4.9km
  185. 6:: 1.2km x 609.4m
  186. 7:: 152.9m x 152.4m
  187. 8:: 38.2m x 19m
  188. 9:: 4.8m x 4.8m
  189. 10:: 1.2m x 59.5cm
  190. 11:: 14.9cm x 14.9cm
  191. 12:: 3.7cm x 1.9cm
  192. ==== Options
  193. [horizontal]
  194. field:: Mandatory. The name of the field indexed with GeoPoints.
  195. precision:: Optional. The string length of the geohashes used to define
  196. cells/buckets in the results. Defaults to 5.
  197. The precision can either be defined in terms of the integer
  198. precision levels mentioned above. Values outside of [1,12] will
  199. be rejected.
  200. Alternatively, the precision level can be approximated from a
  201. distance measure like "1km", "10m". The precision level is
  202. calculate such that cells will not exceed the specified
  203. size (diagonal) of the required precision. When this would lead
  204. to precision levels higher than the supported 12 levels,
  205. (e.g. for distances <5.6cm) the value is rejected.
  206. size:: Optional. The maximum number of geohash buckets to return
  207. (defaults to 10,000). When results are trimmed, buckets are
  208. prioritised based on the volumes of documents they contain.
  209. shard_size:: Optional. To allow for more accurate counting of the top cells
  210. returned in the final result the aggregation defaults to
  211. returning `max(10,(size x number-of-shards))` buckets from each
  212. shard. If this heuristic is undesirable, the number considered
  213. from each shard can be over-ridden using this parameter.