geohashgrid-aggregation.asciidoc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. [[search-aggregations-bucket-geohashgrid-aggregation]]
  2. === Geohash grid aggregation
  3. ++++
  4. <titleabbrev>Geohash grid</titleabbrev>
  5. ++++
  6. A multi-bucket aggregation that groups <<geo-point,`geo_point`>> and
  7. <<geo-shape,`geo_shape`>> values into buckets that represent a grid.
  8. The resulting grid can be sparse and only contains cells that have matching data. Each cell is labeled using a {wikipedia}/Geohash[geohash] which is of user-definable precision.
  9. * High precision geohashes have a long string length and represent cells that cover only a small area.
  10. * Low precision geohashes have a short string length and represent cells that each cover a large area.
  11. Geohashes used in this aggregation can have a choice of precision between 1 and 12.
  12. 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.
  13. Please see the example below on how to first filter the aggregation to a smaller geographic area before requesting high-levels of detail.
  14. You can only use `geohash_grid` to aggregate an explicitly mapped `geo_point` or
  15. `geo_shape` field. If the `geo_point` field contains an array, `geohash_grid`
  16. aggregates all the array values.
  17. ==== Simple low-precision request
  18. [source,console,id=geohashgrid-aggregation-low-precision-example]
  19. --------------------------------------------------
  20. PUT /museums
  21. {
  22. "mappings": {
  23. "properties": {
  24. "location": {
  25. "type": "geo_point"
  26. }
  27. }
  28. }
  29. }
  30. POST /museums/_bulk?refresh
  31. {"index":{"_id":1}}
  32. {"location": "POINT (4.912350 52.374081)", "name": "NEMO Science Museum"}
  33. {"index":{"_id":2}}
  34. {"location": "POINT (4.901618 52.369219)", "name": "Museum Het Rembrandthuis"}
  35. {"index":{"_id":3}}
  36. {"location": "POINT (4.914722 52.371667)", "name": "Nederlands Scheepvaartmuseum"}
  37. {"index":{"_id":4}}
  38. {"location": "POINT (4.405200 51.222900)", "name": "Letterenhuis"}
  39. {"index":{"_id":5}}
  40. {"location": "POINT (2.336389 48.861111)", "name": "Musée du Louvre"}
  41. {"index":{"_id":6}}
  42. {"location": "POINT (2.327000 48.860000)", "name": "Musée d'Orsay"}
  43. POST /museums/_search?size=0
  44. {
  45. "aggregations": {
  46. "large-grid": {
  47. "geohash_grid": {
  48. "field": "location",
  49. "precision": 3
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. Response:
  56. [source,console-result]
  57. --------------------------------------------------
  58. {
  59. ...
  60. "aggregations": {
  61. "large-grid": {
  62. "buckets": [
  63. {
  64. "key": "u17",
  65. "doc_count": 3
  66. },
  67. {
  68. "key": "u09",
  69. "doc_count": 2
  70. },
  71. {
  72. "key": "u15",
  73. "doc_count": 1
  74. }
  75. ]
  76. }
  77. }
  78. }
  79. --------------------------------------------------
  80. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  81. ==== High-precision requests
  82. 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.
  83. [source,console,id=geohashgrid-aggregation-high-precision-example]
  84. --------------------------------------------------
  85. POST /museums/_search?size=0
  86. {
  87. "aggregations": {
  88. "zoomed-in": {
  89. "filter": {
  90. "geo_bounding_box": {
  91. "location": {
  92. "top_left": "POINT (4.9 52.4)",
  93. "bottom_right": "POINT (5.0 52.3)"
  94. }
  95. }
  96. },
  97. "aggregations": {
  98. "zoom1": {
  99. "geohash_grid": {
  100. "field": "location",
  101. "precision": 8
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. --------------------------------------------------
  109. // TEST[continued]
  110. The geohashes returned by the `geohash_grid` aggregation can be also used for zooming in. To zoom into the
  111. first geohash `u17` returned in the previous example, it should be specified as both `top_left` and `bottom_right` corner:
  112. [source,console]
  113. --------------------------------------------------
  114. POST /museums/_search?size=0
  115. {
  116. "aggregations": {
  117. "zoomed-in": {
  118. "filter": {
  119. "geo_bounding_box": {
  120. "location": {
  121. "top_left": "u17",
  122. "bottom_right": "u17"
  123. }
  124. }
  125. },
  126. "aggregations": {
  127. "zoom1": {
  128. "geohash_grid": {
  129. "field": "location",
  130. "precision": 8
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. --------------------------------------------------
  138. // TEST[continued]
  139. [source,console-result]
  140. --------------------------------------------------
  141. {
  142. ...
  143. "aggregations": {
  144. "zoomed-in": {
  145. "doc_count": 3,
  146. "zoom1": {
  147. "buckets": [
  148. {
  149. "key": "u173zy3j",
  150. "doc_count": 1
  151. },
  152. {
  153. "key": "u173zvfz",
  154. "doc_count": 1
  155. },
  156. {
  157. "key": "u173zt90",
  158. "doc_count": 1
  159. }
  160. ]
  161. }
  162. }
  163. }
  164. }
  165. --------------------------------------------------
  166. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  167. For "zooming in" on the system that don't support geohashes, the bucket keys should be translated into bounding boxes using
  168. one of available geohash libraries. For example, for javascript the https://github.com/sunng87/node-geohash[node-geohash] library
  169. can be used:
  170. [source,js]
  171. --------------------------------------------------
  172. var geohash = require('ngeohash');
  173. // bbox will contain [ 52.03125, 4.21875, 53.4375, 5.625 ]
  174. // [ minlat, minlon, maxlat, maxlon]
  175. var bbox = geohash.decode_bbox('u17');
  176. --------------------------------------------------
  177. // NOTCONSOLE
  178. ==== Requests with additional bounding box filtering
  179. The `geohash_grid` aggregation supports an optional `bounds` parameter
  180. that restricts the cells considered to those that intersects the
  181. bounds provided. The `bounds` parameter accepts the bounding box in
  182. all the same <<query-dsl-geo-bounding-box-query-accepted-formats,accepted formats>> of the
  183. bounds specified in the Geo Bounding Box Query. This bounding box can be used with or
  184. without an additional `geo_bounding_box` query filtering the points prior to aggregating.
  185. It is an independent bounding box that can intersect with, be equal to, or be disjoint
  186. to any additional `geo_bounding_box` queries defined in the context of the aggregation.
  187. [source,console,id=geohashgrid-aggregation-with-bounds]
  188. --------------------------------------------------
  189. POST /museums/_search?size=0
  190. {
  191. "aggregations": {
  192. "tiles-in-bounds": {
  193. "geohash_grid": {
  194. "field": "location",
  195. "precision": 8,
  196. "bounds": {
  197. "top_left": "POINT (4.21875 53.4375)",
  198. "bottom_right": "POINT (5.625 52.03125)"
  199. }
  200. }
  201. }
  202. }
  203. }
  204. --------------------------------------------------
  205. // TEST[continued]
  206. [source,console-result]
  207. --------------------------------------------------
  208. {
  209. ...
  210. "aggregations": {
  211. "tiles-in-bounds": {
  212. "buckets": [
  213. {
  214. "key": "u173zy3j",
  215. "doc_count": 1
  216. },
  217. {
  218. "key": "u173zvfz",
  219. "doc_count": 1
  220. },
  221. {
  222. "key": "u173zt90",
  223. "doc_count": 1
  224. }
  225. ]
  226. }
  227. }
  228. }
  229. --------------------------------------------------
  230. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  231. ==== Cell dimensions at the equator
  232. The table below shows the metric dimensions for cells covered by various string lengths of geohash.
  233. Cell dimensions vary with latitude and so the table is for the worst-case scenario at the equator.
  234. [horizontal]
  235. *GeoHash length*:: *Area width x height*
  236. 1:: 5,009.4km x 4,992.6km
  237. 2:: 1,252.3km x 624.1km
  238. 3:: 156.5km x 156km
  239. 4:: 39.1km x 19.5km
  240. 5:: 4.9km x 4.9km
  241. 6:: 1.2km x 609.4m
  242. 7:: 152.9m x 152.4m
  243. 8:: 38.2m x 19m
  244. 9:: 4.8m x 4.8m
  245. 10:: 1.2m x 59.5cm
  246. 11:: 14.9cm x 14.9cm
  247. 12:: 3.7cm x 1.9cm
  248. [discrete]
  249. [role="xpack"]
  250. ==== Aggregating `geo_shape` fields
  251. Aggregating on <<geo-shape>> fields works just as it does for points, except that a single
  252. shape can be counted for in multiple tiles. A shape will contribute to the count of matching values
  253. if any part of its shape intersects with that tile. Below is an image that demonstrates this:
  254. image:images/spatial/geoshape_grid.png[]
  255. ==== Options
  256. [horizontal]
  257. field:: Mandatory. The name of the field indexed with GeoPoints.
  258. precision:: Optional. The string length of the geohashes used to define
  259. cells/buckets in the results. Defaults to 5.
  260. The precision can either be defined in terms of the integer
  261. precision levels mentioned above. Values outside of [1,12] will
  262. be rejected.
  263. Alternatively, the precision level can be approximated from a
  264. distance measure like "1km", "10m". The precision level is
  265. calculate such that cells will not exceed the specified
  266. size (diagonal) of the required precision. When this would lead
  267. to precision levels higher than the supported 12 levels,
  268. (e.g. for distances <5.6cm) the value is rejected.
  269. bounds:: Optional. The bounding box to filter the points in the bucket.
  270. size:: Optional. The maximum number of geohash buckets to return
  271. (defaults to 10,000). When results are trimmed, buckets are
  272. prioritised based on the volumes of documents they contain.
  273. shard_size:: Optional. To allow for more accurate counting of the top cells
  274. returned in the final result the aggregation defaults to
  275. returning `max(10,(size x number-of-shards))` buckets from each
  276. shard. If this heuristic is undesirable, the number considered
  277. from each shard can be over-ridden using this parameter.