GeoHash grid Aggregation(GeoHash网格聚合)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geohashgrid-aggregation.html

译文链接 : http://www.apache.wiki/display/Elasticsearch(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : @于永超,ApacheCNApache中文网

GeoHash grid Aggregation

在geo_point字段和组上工作的多bucket聚合将指向网格中表示单元格的bucket。生成的网格可以是稀疏的,并且只包含具有匹配数据的单元格。每个单元格使用具有用户可定义精度的geohash进行标记。

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

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

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

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

Simple low-precision request

  1. PUT /museums
  2. {
  3. "mappings": {
  4. "doc": {
  5. "properties": {
  6. "location": {
  7. "type": "geo_point"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. POST /museums/doc/_bulk?refresh
  14. {"index":{"_id":1}}
  15. {"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
  16. {"index":{"_id":2}}
  17. {"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
  18. {"index":{"_id":3}}
  19. {"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
  20. {"index":{"_id":4}}
  21. {"location": "51.222900,4.405200", "name": "Letterenhuis"}
  22. {"index":{"_id":5}}
  23. {"location": "48.861111,2.336389", "name": "Musée du Louvre"}
  24. {"index":{"_id":6}}
  25. {"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
  26. POST /museums/_search?size=0
  27. {
  28. "aggregations" : {
  29. "large-grid" : {
  30. "geohash_grid" : {
  31. "field" : "location",
  32. "precision" : 3
  33. }
  34. }
  35. }
  36. }

响应结果

  1. {
  2. ...
  3. "aggregations": {
  4. "large-grid": {
  5. "buckets": [
  6. {
  7. "key": "u17",
  8. "doc_count": 3
  9. },
  10. {
  11. "key": "u09",
  12. "doc_count": 2
  13. },
  14. {
  15. "key": "u15",
  16. "doc_count": 1
  17. }
  18. ]
  19. }
  20. }
  21. }

High-precision requests

当请求详细的存储区(通常用于显示“zoomed”映射)时,应该应用像geo_bounding_box这样的过滤器来缩小主题区域,否则将创建并返回数百万个buckets(存储桶)。

  1. POST /museums/_search?size=0
  2. {
  3. "aggregations" : {
  4. "zoomed-in" : {
  5. "filter" : {
  6. "geo_bounding_box" : {
  7. "location" : {
  8. "top_left" : "52.4, 4.9",
  9. "bottom_right" : "52.3, 5.0"
  10. }
  11. }
  12. },
  13. "aggregations":{
  14. "zoom1":{
  15. "geohash_grid" : {
  16. "field": "location",
  17. "precision": 8
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }

Cell dimensions at the equator

下面的表显示了由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

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

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

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

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