geotilegrid-aggregation.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. [[search-aggregations-bucket-geotilegrid-aggregation]]
  2. === GeoTile Grid Aggregation
  3. A multi-bucket aggregation that works on `geo_point` fields and groups points into
  4. buckets that represent cells in a grid. The resulting grid can be sparse and only
  5. contains cells that have matching data. Each cell corresponds to a
  6. https://en.wikipedia.org/wiki/Tiled_web_map[map tile] as used by many online map
  7. sites. Each cell is labeled using a "{zoom}/{x}/{y}" format, where zoom is equal
  8. to the user-specified precision.
  9. * High precision keys have a larger range for x and y, and represent tiles that
  10. cover only a small area.
  11. * Low precision keys have a smaller range for x and y, and represent tiles that
  12. each cover a large area.
  13. See https://wiki.openstreetmap.org/wiki/Zoom_levels[Zoom level documentation]
  14. on how precision (zoom) correlates to size on the ground. Precision for this
  15. aggregation can be between 0 and 29, inclusive.
  16. WARNING: The highest-precision geotile of length 29 produces cells that cover
  17. less than a 10cm by 10cm of land and so high-precision requests can be very
  18. costly in terms of RAM and result sizes. Please see the example below on how
  19. to first filter the aggregation to a smaller geographic area before requesting
  20. high-levels of detail.
  21. The specified field must be of type `geo_point` (which can only be set
  22. explicitly in the mappings) and it can also hold an array of `geo_point`
  23. fields, in which case all points will be taken into account during aggregation.
  24. ==== Simple low-precision request
  25. [source,console,id=geotilegrid-aggregation-example]
  26. --------------------------------------------------
  27. PUT /museums
  28. {
  29. "mappings": {
  30. "properties": {
  31. "location": {
  32. "type": "geo_point"
  33. }
  34. }
  35. }
  36. }
  37. POST /museums/_bulk?refresh
  38. {"index":{"_id":1}}
  39. {"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
  40. {"index":{"_id":2}}
  41. {"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
  42. {"index":{"_id":3}}
  43. {"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
  44. {"index":{"_id":4}}
  45. {"location": "51.222900,4.405200", "name": "Letterenhuis"}
  46. {"index":{"_id":5}}
  47. {"location": "48.861111,2.336389", "name": "Musée du Louvre"}
  48. {"index":{"_id":6}}
  49. {"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
  50. POST /museums/_search?size=0
  51. {
  52. "aggregations" : {
  53. "large-grid" : {
  54. "geotile_grid" : {
  55. "field" : "location",
  56. "precision" : 8
  57. }
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. Response:
  63. [source,console-result]
  64. --------------------------------------------------
  65. {
  66. ...
  67. "aggregations": {
  68. "large-grid": {
  69. "buckets": [
  70. {
  71. "key" : "8/131/84",
  72. "doc_count" : 3
  73. },
  74. {
  75. "key" : "8/129/88",
  76. "doc_count" : 2
  77. },
  78. {
  79. "key" : "8/131/85",
  80. "doc_count" : 1
  81. }
  82. ]
  83. }
  84. }
  85. }
  86. --------------------------------------------------
  87. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  88. ==== High-precision requests
  89. When requesting detailed buckets (typically for displaying a "zoomed in" map)
  90. a filter like <<query-dsl-geo-bounding-box-query,geo_bounding_box>> should be
  91. applied to narrow the subject area otherwise potentially millions of buckets
  92. will be created and returned.
  93. [source,console]
  94. --------------------------------------------------
  95. POST /museums/_search?size=0
  96. {
  97. "aggregations" : {
  98. "zoomed-in" : {
  99. "filter" : {
  100. "geo_bounding_box" : {
  101. "location" : {
  102. "top_left" : "52.4, 4.9",
  103. "bottom_right" : "52.3, 5.0"
  104. }
  105. }
  106. },
  107. "aggregations":{
  108. "zoom1":{
  109. "geotile_grid" : {
  110. "field": "location",
  111. "precision": 22
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. --------------------------------------------------
  119. // TEST[continued]
  120. [source,console-result]
  121. --------------------------------------------------
  122. {
  123. ...
  124. "aggregations" : {
  125. "zoomed-in" : {
  126. "doc_count" : 3,
  127. "zoom1" : {
  128. "buckets" : [
  129. {
  130. "key" : "22/2154412/1378379",
  131. "doc_count" : 1
  132. },
  133. {
  134. "key" : "22/2154385/1378332",
  135. "doc_count" : 1
  136. },
  137. {
  138. "key" : "22/2154259/1378425",
  139. "doc_count" : 1
  140. }
  141. ]
  142. }
  143. }
  144. }
  145. }
  146. --------------------------------------------------
  147. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  148. ==== Requests with additional bounding box filtering
  149. The `geotile_grid` aggregation supports an optional `bounds` parameter
  150. that restricts the points considered to those that fall within the
  151. bounds provided. The `bounds` parameter accepts the bounding box in
  152. all the same <<query-dsl-geo-bounding-box-query-accepted-formats,accepted formats>> of the
  153. bounds specified in the Geo Bounding Box Query. This bounding box can be used with or
  154. without an additional `geo_bounding_box` query filtering the points prior to aggregating.
  155. It is an independent bounding box that can intersect with, be equal to, or be disjoint
  156. to any additional `geo_bounding_box` queries defined in the context of the aggregation.
  157. [source,console,id=geotilegrid-aggregation-with-bounds]
  158. --------------------------------------------------
  159. POST /museums/_search?size=0
  160. {
  161. "aggregations" : {
  162. "tiles-in-bounds" : {
  163. "geotile_grid" : {
  164. "field" : "location",
  165. "precision" : 22,
  166. "bounds": {
  167. "top_left" : "52.4, 4.9",
  168. "bottom_right" : "52.3, 5.0"
  169. }
  170. }
  171. }
  172. }
  173. }
  174. --------------------------------------------------
  175. // TEST[continued]
  176. [source,console-result]
  177. --------------------------------------------------
  178. {
  179. ...
  180. "aggregations" : {
  181. "tiles-in-bounds" : {
  182. "buckets" : [
  183. {
  184. "key" : "22/2154412/1378379",
  185. "doc_count" : 1
  186. },
  187. {
  188. "key" : "22/2154385/1378332",
  189. "doc_count" : 1
  190. },
  191. {
  192. "key" : "22/2154259/1378425",
  193. "doc_count" : 1
  194. }
  195. ]
  196. }
  197. }
  198. }
  199. --------------------------------------------------
  200. // TESTRESPONSE[s/\.\.\./"took": $body.took,"_shards": $body._shards,"hits":$body.hits,"timed_out":false,/]
  201. [discrete]
  202. [role="xpack"]
  203. ==== Aggregating `geo_shape` fields
  204. Aggregating on <<geo-shape>> fields works just as it does for points, except that a single
  205. shape can be counted for in multiple tiles. A shape will contribute to the count of matching values
  206. if any part of its shape intersects with that tile. Below is an image that demonstrates this
  207. image:images/spatial/geoshape_grid.png[]
  208. ==== Options
  209. [horizontal]
  210. field:: Mandatory. The name of the field indexed with GeoPoints.
  211. precision:: Optional. The integer zoom of the key used to define
  212. cells/buckets in the results. Defaults to 7.
  213. Values outside of [0,29] will be rejected.
  214. bounds: Optional. The bounding box to filter the points in the bucket.
  215. size:: Optional. The maximum number of geohash buckets to return
  216. (defaults to 10,000). When results are trimmed, buckets are
  217. prioritised based on the volumes of documents they contain.
  218. shard_size:: Optional. To allow for more accurate counting of the top cells
  219. returned in the final result the aggregation defaults to
  220. returning `max(10,(size x number-of-shards))` buckets from each
  221. shard. If this heuristic is undesirable, the number considered
  222. from each shard can be over-ridden using this parameter.