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