geohashgrid-aggregation.asciidoc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. [[search-aggregations-bucket-geohashgrid-aggregation]]
  2. === Geohash grid aggregation
  3. ++++
  4. <titleabbrev>Geohash grid</titleabbrev>
  5. ++++
  6. A multi-bucket aggregation that works on `geo_point` fields and groups points into buckets that represent cells in a grid.
  7. 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.
  8. * High precision geohashes have a long string length and represent cells that cover only a small area.
  9. * Low precision geohashes have a short string length and represent cells that each cover a large area.
  10. Geohashes used in this aggregation can have a choice of precision between 1 and 12.
  11. 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.
  12. Please see the example below on how to first filter the aggregation to a smaller geographic area before requesting high-levels of detail.
  13. 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.
  14. ==== Simple low-precision request
  15. [source,console,id=geohashgrid-aggregation-low-precision-example]
  16. --------------------------------------------------
  17. PUT /museums
  18. {
  19. "mappings": {
  20. "properties": {
  21. "location": {
  22. "type": "geo_point"
  23. }
  24. }
  25. }
  26. }
  27. POST /museums/_bulk?refresh
  28. {"index":{"_id":1}}
  29. {"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
  30. {"index":{"_id":2}}
  31. {"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
  32. {"index":{"_id":3}}
  33. {"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
  34. {"index":{"_id":4}}
  35. {"location": "51.222900,4.405200", "name": "Letterenhuis"}
  36. {"index":{"_id":5}}
  37. {"location": "48.861111,2.336389", "name": "Musée du Louvre"}
  38. {"index":{"_id":6}}
  39. {"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
  40. POST /museums/_search?size=0
  41. {
  42. "aggregations": {
  43. "large-grid": {
  44. "geohash_grid": {
  45. "field": "location",
  46. "precision": 3
  47. }
  48. }
  49. }
  50. }
  51. --------------------------------------------------
  52. Response:
  53. [source,console-result]
  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,console,id=geohashgrid-aggregation-high-precision-example]
  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. // TEST[continued]
  107. The geohashes returned by the `geohash_grid` aggregation can be also used for zooming in. To zoom into the
  108. first geohash `u17` returned in the previous example, it should be specified as both `top_left` and `bottom_right` corner:
  109. [source,console]
  110. --------------------------------------------------
  111. POST /museums/_search?size=0
  112. {
  113. "aggregations": {
  114. "zoomed-in": {
  115. "filter": {
  116. "geo_bounding_box": {
  117. "location": {
  118. "top_left": "u17",
  119. "bottom_right": "u17"
  120. }
  121. }
  122. },
  123. "aggregations": {
  124. "zoom1": {
  125. "geohash_grid": {
  126. "field": "location",
  127. "precision": 8
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. --------------------------------------------------
  135. // TEST[continued]
  136. [source,console-result]
  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. ==== Requests with additional bounding box filtering
  176. The `geohash_grid` aggregation supports an optional `bounds` parameter
  177. that restricts the points considered to those that fall within the
  178. bounds provided. The `bounds` parameter accepts the bounding box in
  179. all the same <<query-dsl-geo-bounding-box-query-accepted-formats,accepted formats>> of the
  180. bounds specified in the Geo Bounding Box Query. This bounding box can be used with or
  181. without an additional `geo_bounding_box` query filtering the points prior to aggregating.
  182. It is an independent bounding box that can intersect with, be equal to, or be disjoint
  183. to any additional `geo_bounding_box` queries defined in the context of the aggregation.
  184. [source,console,id=geohashgrid-aggregation-with-bounds]
  185. --------------------------------------------------
  186. POST /museums/_search?size=0
  187. {
  188. "aggregations": {
  189. "tiles-in-bounds": {
  190. "geohash_grid": {
  191. "field": "location",
  192. "precision": 8,
  193. "bounds": {
  194. "top_left": "53.4375, 4.21875",
  195. "bottom_right": "52.03125, 5.625"
  196. }
  197. }
  198. }
  199. }
  200. }
  201. --------------------------------------------------
  202. // TEST[continued]
  203. [source,console-result]
  204. --------------------------------------------------
  205. {
  206. ...
  207. "aggregations": {
  208. "tiles-in-bounds": {
  209. "buckets": [
  210. {
  211. "key": "u173zy3j",
  212. "doc_count": 1
  213. },
  214. {
  215. "key": "u173zvfz",
  216. "doc_count": 1
  217. },
  218. {
  219. "key": "u173zt90",
  220. "doc_count": 1
  221. }
  222. ]
  223. }
  224. }
  225. }
  226. --------------------------------------------------
  227. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  228. ==== Cell dimensions at the equator
  229. The table below shows the metric dimensions for cells covered by various string lengths of geohash.
  230. Cell dimensions vary with latitude and so the table is for the worst-case scenario at the equator.
  231. [horizontal]
  232. *GeoHash length*:: *Area width x height*
  233. 1:: 5,009.4km x 4,992.6km
  234. 2:: 1,252.3km x 624.1km
  235. 3:: 156.5km x 156km
  236. 4:: 39.1km x 19.5km
  237. 5:: 4.9km x 4.9km
  238. 6:: 1.2km x 609.4m
  239. 7:: 152.9m x 152.4m
  240. 8:: 38.2m x 19m
  241. 9:: 4.8m x 4.8m
  242. 10:: 1.2m x 59.5cm
  243. 11:: 14.9cm x 14.9cm
  244. 12:: 3.7cm x 1.9cm
  245. [discrete]
  246. [role="xpack"]
  247. ==== Aggregating `geo_shape` fields
  248. Aggregating on <<geo-shape>> fields works just as it does for points, except that a single
  249. shape can be counted for in multiple tiles. A shape will contribute to the count of matching values
  250. if any part of its shape intersects with that tile. Below is an image that demonstrates this:
  251. image:images/spatial/geoshape_grid.png[]
  252. ==== Options
  253. [horizontal]
  254. field:: Mandatory. The name of the field indexed with GeoPoints.
  255. precision:: Optional. The string length of the geohashes used to define
  256. cells/buckets in the results. Defaults to 5.
  257. The precision can either be defined in terms of the integer
  258. precision levels mentioned above. Values outside of [1,12] will
  259. be rejected.
  260. Alternatively, the precision level can be approximated from a
  261. distance measure like "1km", "10m". The precision level is
  262. calculate such that cells will not exceed the specified
  263. size (diagonal) of the required precision. When this would lead
  264. to precision levels higher than the supported 12 levels,
  265. (e.g. for distances <5.6cm) the value is rejected.
  266. bounds:: Optional. The bounding box to filter the points in the bucket.
  267. size:: Optional. The maximum number of geohash buckets to return
  268. (defaults to 10,000). When results are trimmed, buckets are
  269. prioritised based on the volumes of documents they contain.
  270. shard_size:: Optional. To allow for more accurate counting of the top cells
  271. returned in the final result the aggregation defaults to
  272. returning `max(10,(size x number-of-shards))` buckets from each
  273. shard. If this heuristic is undesirable, the number considered
  274. from each shard can be over-ridden using this parameter.