geohashgrid-aggregation.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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,console,id=geohashgrid-aggregation-low-precision-example]
  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. Response:
  50. [source,console-result]
  51. --------------------------------------------------
  52. {
  53. ...
  54. "aggregations": {
  55. "large-grid": {
  56. "buckets": [
  57. {
  58. "key": "u17",
  59. "doc_count": 3
  60. },
  61. {
  62. "key": "u09",
  63. "doc_count": 2
  64. },
  65. {
  66. "key": "u15",
  67. "doc_count": 1
  68. }
  69. ]
  70. }
  71. }
  72. }
  73. --------------------------------------------------
  74. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  75. ==== High-precision requests
  76. 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.
  77. [source,console,id=geohashgrid-aggregation-high-precision-example]
  78. --------------------------------------------------
  79. POST /museums/_search?size=0
  80. {
  81. "aggregations" : {
  82. "zoomed-in" : {
  83. "filter" : {
  84. "geo_bounding_box" : {
  85. "location" : {
  86. "top_left" : "52.4, 4.9",
  87. "bottom_right" : "52.3, 5.0"
  88. }
  89. }
  90. },
  91. "aggregations":{
  92. "zoom1":{
  93. "geohash_grid" : {
  94. "field": "location",
  95. "precision": 8
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. --------------------------------------------------
  103. // TEST[continued]
  104. The geohashes returned by the `geohash_grid` aggregation can be also used for zooming in. To zoom into the
  105. first geohash `u17` returned in the previous example, it should be specified as both `top_left` and `bottom_right` corner:
  106. [source,console]
  107. --------------------------------------------------
  108. POST /museums/_search?size=0
  109. {
  110. "aggregations" : {
  111. "zoomed-in" : {
  112. "filter" : {
  113. "geo_bounding_box" : {
  114. "location" : {
  115. "top_left" : "u17",
  116. "bottom_right" : "u17"
  117. }
  118. }
  119. },
  120. "aggregations":{
  121. "zoom1":{
  122. "geohash_grid" : {
  123. "field": "location",
  124. "precision": 8
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. --------------------------------------------------
  132. // TEST[continued]
  133. [source,console-result]
  134. --------------------------------------------------
  135. {
  136. ...
  137. "aggregations" : {
  138. "zoomed-in" : {
  139. "doc_count" : 3,
  140. "zoom1" : {
  141. "buckets" : [
  142. {
  143. "key" : "u173zy3j",
  144. "doc_count" : 1
  145. },
  146. {
  147. "key" : "u173zvfz",
  148. "doc_count" : 1
  149. },
  150. {
  151. "key" : "u173zt90",
  152. "doc_count" : 1
  153. }
  154. ]
  155. }
  156. }
  157. }
  158. }
  159. --------------------------------------------------
  160. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  161. For "zooming in" on the system that don't support geohashes, the bucket keys should be translated into bounding boxes using
  162. one of available geohash libraries. For example, for javascript the https://github.com/sunng87/node-geohash[node-geohash] library
  163. can be used:
  164. [source,js]
  165. --------------------------------------------------
  166. var geohash = require('ngeohash');
  167. // bbox will contain [ 52.03125, 4.21875, 53.4375, 5.625 ]
  168. // [ minlat, minlon, maxlat, maxlon]
  169. var bbox = geohash.decode_bbox('u17');
  170. --------------------------------------------------
  171. // NOTCONSOLE
  172. ==== Requests with additional bounding box filtering
  173. The `geohash_grid` aggregation supports an optional `bounds` parameter
  174. that restricts the points considered to those that fall within the
  175. bounds provided. The `bounds` parameter accepts the bounding box in
  176. all the same <<query-dsl-geo-bounding-box-query-accepted-formats,accepted formats>> of the
  177. bounds specified in the Geo Bounding Box Query. This bounding box can be used with or
  178. without an additional `geo_bounding_box` query filtering the points prior to aggregating.
  179. It is an independent bounding box that can intersect with, be equal to, or be disjoint
  180. to any additional `geo_bounding_box` queries defined in the context of the aggregation.
  181. [source,console,id=geohashgrid-aggregation-with-bounds]
  182. --------------------------------------------------
  183. POST /museums/_search?size=0
  184. {
  185. "aggregations" : {
  186. "tiles-in-bounds" : {
  187. "geohash_grid" : {
  188. "field" : "location",
  189. "precision" : 8,
  190. "bounds": {
  191. "top_left" : "53.4375, 4.21875",
  192. "bottom_right" : "52.03125, 5.625"
  193. }
  194. }
  195. }
  196. }
  197. }
  198. --------------------------------------------------
  199. // TEST[continued]
  200. [source,console-result]
  201. --------------------------------------------------
  202. {
  203. ...
  204. "aggregations" : {
  205. "tiles-in-bounds" : {
  206. "buckets" : [
  207. {
  208. "key" : "u173zy3j",
  209. "doc_count" : 1
  210. },
  211. {
  212. "key" : "u173zvfz",
  213. "doc_count" : 1
  214. },
  215. {
  216. "key" : "u173zt90",
  217. "doc_count" : 1
  218. }
  219. ]
  220. }
  221. }
  222. }
  223. --------------------------------------------------
  224. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  225. ==== Cell dimensions at the equator
  226. The table below shows the metric dimensions for cells covered by various string lengths of geohash.
  227. Cell dimensions vary with latitude and so the table is for the worst-case scenario at the equator.
  228. [horizontal]
  229. *GeoHash length*:: *Area width x height*
  230. 1:: 5,009.4km x 4,992.6km
  231. 2:: 1,252.3km x 624.1km
  232. 3:: 156.5km x 156km
  233. 4:: 39.1km x 19.5km
  234. 5:: 4.9km x 4.9km
  235. 6:: 1.2km x 609.4m
  236. 7:: 152.9m x 152.4m
  237. 8:: 38.2m x 19m
  238. 9:: 4.8m x 4.8m
  239. 10:: 1.2m x 59.5cm
  240. 11:: 14.9cm x 14.9cm
  241. 12:: 3.7cm x 1.9cm
  242. ==== Options
  243. [horizontal]
  244. field:: Mandatory. The name of the field indexed with GeoPoints.
  245. precision:: Optional. The string length of the geohashes used to define
  246. cells/buckets in the results. Defaults to 5.
  247. The precision can either be defined in terms of the integer
  248. precision levels mentioned above. Values outside of [1,12] will
  249. be rejected.
  250. Alternatively, the precision level can be approximated from a
  251. distance measure like "1km", "10m". The precision level is
  252. calculate such that cells will not exceed the specified
  253. size (diagonal) of the required precision. When this would lead
  254. to precision levels higher than the supported 12 levels,
  255. (e.g. for distances <5.6cm) the value is rejected.
  256. bounds: Optional. The bounding box to filter the points in the bucket.
  257. size:: Optional. The maximum number of geohash buckets to return
  258. (defaults to 10,000). When results are trimmed, buckets are
  259. prioritised based on the volumes of documents they contain.
  260. shard_size:: Optional. To allow for more accurate counting of the top cells
  261. returned in the final result the aggregation defaults to
  262. returning `max(10,(size x number-of-shards))` buckets from each
  263. shard. If this heuristic is undesirable, the number considered
  264. from each shard can be over-ridden using this parameter.