# GeoHash网格聚合(GeoHash grid Aggregation)

在geo\_point字段和组上工作的多bucket聚合将指向网格中表示单元格的bucket。生成的网格可以是稀疏的，并且只包含具有匹配数据的单元格。每个单元格使用具有用户可定义精度的[geohash](http://en.wikipedia.org/wiki/Geohash)进行标记。

* 高精度geohash具有较长的字符串长度，代表仅覆盖小面积的单元格。
* 低精度geohashes具有短的字符串长度，并且表示每个覆盖大面积的单元格。

在此聚合中使用的地理位置可以选择1到12之间的精度。

> 长度为12的最高精度的geohash产生覆盖小于一平方米土地的单元，因此高精度请求在RAM和结果大小方面可能非常昂贵。 请参阅下面的示例，了解如何在请求高级细节之前首先将聚合过滤到较小的地理区域。

指定字段必须为geo\_point类型(这只能在映射中明确设置)并且它还可以保存一组geo\_point字段，在这种情况下，聚合期间将考虑所有点。

## Simple low-precision request <a href="#geohashgridaggregationgeohash-wang-ge-ju-he-simplelowprecisionrequest" id="geohashgridaggregationgeohash-wang-ge-ju-he-simplelowprecisionrequest"></a>

```
PUT /museums
{
    "mappings": {
        "doc": {
            "properties": {
                "location": {
                    "type": "geo_point"
                }
            }
        }
    }
}

POST /museums/doc/_bulk?refresh
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d'Orsay"}

POST /museums/_search?size=0
{
    "aggregations" : {
        "large-grid" : {
            "geohash_grid" : {
                "field" : "location",
                "precision" : 3
            }
        }
    }
}
```

响应结果

```
{
    ...
    "aggregations": {
        "large-grid": {
            "buckets": [
                {
                    "key": "u17",
                    "doc_count": 3
                },
                {
                    "key": "u09",
                    "doc_count": 2
                },
                {
                    "key": "u15",
                    "doc_count": 1
                }
            ]
        }
    }
}
```

## High-precision requests <a href="#geohashgridaggregationgeohash-wang-ge-ju-he-highprecisionrequests" id="geohashgridaggregationgeohash-wang-ge-ju-he-highprecisionrequests"></a>

当请求详细的存储区（通常用于显示“zoomed”映射）时，应该应用像[geo\_bounding\_box](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html)这样的过滤器来缩小主题区域，否则将创建并返回数百万个buckets(存储桶)。

```
POST /museums/_search?size=0
{
    "aggregations" : {
        "zoomed-in" : {
            "filter" : {
                "geo_bounding_box" : {
                    "location" : {
                        "top_left" : "52.4, 4.9",
                        "bottom_right" : "52.3, 5.0"
                    }
                }
            },
            "aggregations":{
                "zoom1":{
                    "geohash_grid" : {
                        "field": "location",
                        "precision": 8
                    }
                }
            }
        }
    }
}
```

## Cell dimensions at the equator <a href="#geohashgridaggregationgeohash-wang-ge-ju-he-celldimensionsattheequator" id="geohashgridaggregationgeohash-wang-ge-ju-he-celldimensionsattheequator"></a>

下面的表显示了由geohash的各种字符串长度覆盖的单元格的度量维度。

| GeoHash length | Area width x height   |
| -------------- | --------------------- |
| 1              | 5,009.4km x 4,992.6km |
| 2              | 1,252.3km x 624.1km   |
| 3              | 156.5km x 156km       |
| 4              | 39.1km x 19.5km       |
| 5              | 4.9km x 4.9km         |
| 6              | 1.2km x 609.4m        |
| 7              | 152.9m x 152.4m       |
| 8              | 38.2m x 19m           |
| 9              | 4.8m x 4.8m           |
| 10             | 1.2m x 59.5cm         |
| 11             | 14.9cm x 14.9cm       |
| 12             | 3.7cm x 1.9cm         |

## Options <a href="#geohashgridaggregationgeohash-wang-ge-ju-he-options" id="geohashgridaggregationgeohash-wang-ge-ju-he-options"></a>

field 强制性。 使用GeoPoints索引的字段的名称。

precision 可选的。 用于在结果中定义单元格/桶的geohash的字符串长度。 默认为5。

size 可选的。返回的geohash桶的最大数量(默认为10,000)。在处理结果时，根据所包含的文档的数量优先级排序。

shard\_size 可选的。为了能够更精确地计算顶部单元格返回的最终结果，聚合默认值将从每个shard返回最大(10,(size x number-of-shards),如果这个heuristic(启发式)是不可取的, 使用这个参数可以覆盖每个碎片上数量
