Geohex 网格聚合

编辑

一个多桶聚合,将 geo_pointgeo_shape 值分组到代表网格的桶中。生成的网格可以是稀疏的,并且仅包含具有匹配数据的单元格。每个单元格对应于一个 H3 单元格索引,并使用 H3Index 表示进行标记。

请参阅 H3 分辨率的单元格面积表,了解精度(缩放)如何与地面上的大小相关联。此聚合的精度可以在 0 到 15 之间(包括 0 和 15)。

高精度请求在 RAM 和结果大小方面可能非常昂贵。例如,精度为 15 的最高精度 geohex 生成的单元格覆盖面积小于一平方米。我们建议您使用过滤器将高精度请求限制在较小的地理区域内。有关示例,请参阅 高精度请求

简单的低精度请求

编辑
resp = client.indices.create(
    index="museums",
    mappings={
        "properties": {
            "location": {
                "type": "geo_point"
            }
        }
    },
)
print(resp)

resp1 = client.bulk(
    index="museums",
    refresh=True,
    operations=[
        {
            "index": {
                "_id": 1
            }
        },
        {
            "location": "POINT (4.912350 52.374081)",
            "name": "NEMO Science Museum"
        },
        {
            "index": {
                "_id": 2
            }
        },
        {
            "location": "POINT (4.901618 52.369219)",
            "name": "Museum Het Rembrandthuis"
        },
        {
            "index": {
                "_id": 3
            }
        },
        {
            "location": "POINT (4.914722 52.371667)",
            "name": "Nederlands Scheepvaartmuseum"
        },
        {
            "index": {
                "_id": 4
            }
        },
        {
            "location": "POINT (4.405200 51.222900)",
            "name": "Letterenhuis"
        },
        {
            "index": {
                "_id": 5
            }
        },
        {
            "location": "POINT (2.336389 48.861111)",
            "name": "Musée du Louvre"
        },
        {
            "index": {
                "_id": 6
            }
        },
        {
            "location": "POINT (2.327000 48.860000)",
            "name": "Musée d'Orsay"
        }
    ],
)
print(resp1)

resp2 = client.search(
    index="museums",
    size="0",
    aggregations={
        "large-grid": {
            "geohex_grid": {
                "field": "location",
                "precision": 4
            }
        }
    },
)
print(resp2)
response = client.indices.create(
  index: 'museums',
  body: {
    mappings: {
      properties: {
        location: {
          type: 'geo_point'
        }
      }
    }
  }
)
puts response

response = client.bulk(
  index: 'museums',
  refresh: true,
  body: [
    {
      index: {
        _id: 1
      }
    },
    {
      location: 'POINT (4.912350 52.374081)',
      name: 'NEMO Science Museum'
    },
    {
      index: {
        _id: 2
      }
    },
    {
      location: 'POINT (4.901618 52.369219)',
      name: 'Museum Het Rembrandthuis'
    },
    {
      index: {
        _id: 3
      }
    },
    {
      location: 'POINT (4.914722 52.371667)',
      name: 'Nederlands Scheepvaartmuseum'
    },
    {
      index: {
        _id: 4
      }
    },
    {
      location: 'POINT (4.405200 51.222900)',
      name: 'Letterenhuis'
    },
    {
      index: {
        _id: 5
      }
    },
    {
      location: 'POINT (2.336389 48.861111)',
      name: 'Musée du Louvre'
    },
    {
      index: {
        _id: 6
      }
    },
    {
      location: 'POINT (2.327000 48.860000)',
      name: "Musée d'Orsay"
    }
  ]
)
puts response

response = client.search(
  index: 'museums',
  size: 0,
  body: {
    aggregations: {
      "large-grid": {
        geohex_grid: {
          field: 'location',
          precision: 4
        }
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "museums",
  mappings: {
    properties: {
      location: {
        type: "geo_point",
      },
    },
  },
});
console.log(response);

const response1 = await client.bulk({
  index: "museums",
  refresh: "true",
  operations: [
    {
      index: {
        _id: 1,
      },
    },
    {
      location: "POINT (4.912350 52.374081)",
      name: "NEMO Science Museum",
    },
    {
      index: {
        _id: 2,
      },
    },
    {
      location: "POINT (4.901618 52.369219)",
      name: "Museum Het Rembrandthuis",
    },
    {
      index: {
        _id: 3,
      },
    },
    {
      location: "POINT (4.914722 52.371667)",
      name: "Nederlands Scheepvaartmuseum",
    },
    {
      index: {
        _id: 4,
      },
    },
    {
      location: "POINT (4.405200 51.222900)",
      name: "Letterenhuis",
    },
    {
      index: {
        _id: 5,
      },
    },
    {
      location: "POINT (2.336389 48.861111)",
      name: "Musée du Louvre",
    },
    {
      index: {
        _id: 6,
      },
    },
    {
      location: "POINT (2.327000 48.860000)",
      name: "Musée d'Orsay",
    },
  ],
});
console.log(response1);

const response2 = await client.search({
  index: "museums",
  size: 0,
  aggregations: {
    "large-grid": {
      geohex_grid: {
        field: "location",
        precision: 4,
      },
    },
  },
});
console.log(response2);
PUT /museums
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

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

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

响应

{
  ...
  "aggregations": {
    "large-grid": {
      "buckets": [
        {
          "key": "841969dffffffff",
          "doc_count": 3
        },
        {
          "key": "841fb47ffffffff",
          "doc_count": 2
        },
        {
          "key": "841fa4dffffffff",
          "doc_count": 1
        }
      ]
    }
  }
}

高精度请求

编辑

当请求详细的桶(通常用于显示“放大”的地图)时,应应用像 geo_bounding_box 这样的过滤器来缩小主题区域。否则,可能会创建并返回数百万个桶。

resp = client.search(
    index="museums",
    size="0",
    aggregations={
        "zoomed-in": {
            "filter": {
                "geo_bounding_box": {
                    "location": {
                        "top_left": "POINT (4.9 52.4)",
                        "bottom_right": "POINT (5.0 52.3)"
                    }
                }
            },
            "aggregations": {
                "zoom1": {
                    "geohex_grid": {
                        "field": "location",
                        "precision": 12
                    }
                }
            }
        }
    },
)
print(resp)
const response = await client.search({
  index: "museums",
  size: 0,
  aggregations: {
    "zoomed-in": {
      filter: {
        geo_bounding_box: {
          location: {
            top_left: "POINT (4.9 52.4)",
            bottom_right: "POINT (5.0 52.3)",
          },
        },
      },
      aggregations: {
        zoom1: {
          geohex_grid: {
            field: "location",
            precision: 12,
          },
        },
      },
    },
  },
});
console.log(response);
POST /museums/_search?size=0
{
  "aggregations": {
    "zoomed-in": {
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": "POINT (4.9 52.4)",
            "bottom_right": "POINT (5.0 52.3)"
          }
        }
      },
      "aggregations": {
        "zoom1": {
          "geohex_grid": {
            "field": "location",
            "precision": 12
          }
        }
      }
    }
  }
}

响应

{
  ...
  "aggregations": {
    "zoomed-in": {
      "doc_count": 3,
      "zoom1": {
        "buckets": [
          {
            "key": "8c1969c9b2617ff",
            "doc_count": 1
          },
          {
            "key": "8c1969526d753ff",
            "doc_count": 1
          },
          {
            "key": "8c1969526d26dff",
            "doc_count": 1
          }
        ]
      }
    }
  }
}

带有额外边界框过滤的请求

编辑

geohex_grid 聚合支持一个可选的 bounds 参数,该参数将考虑的单元格限制为与提供的边界相交的单元格。bounds 参数接受与地理边界框查询相同的 边界框格式。此边界框可以与额外的 geo_bounding_box 查询一起使用,也可以不使用额外的 geo_bounding_box 查询来过滤聚合之前的点。它是一个独立的边界框,可以与聚合上下文中定义的任何额外的 geo_bounding_box 查询相交、相等或不相交。

resp = client.search(
    index="museums",
    size="0",
    aggregations={
        "tiles-in-bounds": {
            "geohex_grid": {
                "field": "location",
                "precision": 12,
                "bounds": {
                    "top_left": "POINT (4.9 52.4)",
                    "bottom_right": "POINT (5.0 52.3)"
                }
            }
        }
    },
)
print(resp)
const response = await client.search({
  index: "museums",
  size: 0,
  aggregations: {
    "tiles-in-bounds": {
      geohex_grid: {
        field: "location",
        precision: 12,
        bounds: {
          top_left: "POINT (4.9 52.4)",
          bottom_right: "POINT (5.0 52.3)",
        },
      },
    },
  },
});
console.log(response);
POST /museums/_search?size=0
{
  "aggregations": {
    "tiles-in-bounds": {
      "geohex_grid": {
        "field": "location",
        "precision": 12,
        "bounds": {
          "top_left": "POINT (4.9 52.4)",
          "bottom_right": "POINT (5.0 52.3)"
        }
      }
    }
  }
}

响应

{
  ...
  "aggregations": {
    "tiles-in-bounds": {
      "buckets": [
        {
          "key": "8c1969c9b2617ff",
          "doc_count": 1
        },
        {
          "key": "8c1969526d753ff",
          "doc_count": 1
        },
        {
          "key": "8c1969526d26dff",
          "doc_count": 1
        }
      ]
    }
  }
}

聚合 geo_shape 字段

编辑

Geoshape 字段进行聚合的工作方式与对点进行聚合几乎相同。 有两个关键差异:

  • 当聚合 geo_point 数据时,如果点位于大圆定义的边缘内,则认为点位于六边形瓦片内。换句话说,计算是使用球面坐标完成的。但是,当聚合 geo_shape 数据时,如果形状位于等矩投影上的直线定义的边缘内,则认为形状位于六边形内。原因是 Elasticsearch 和 Lucene 在索引和搜索时使用等矩投影处理边缘。为了确保搜索结果和聚合结果对齐,我们在聚合中也使用等矩投影。对于大多数数据,差异是细微的或者没有注意到。但是,对于低缩放级别(低精度),尤其是远离赤道的地方,这可能会很明显。例如,如果相同的点数据索引为 geo_pointgeo_shape,则在较低分辨率下进行聚合时可能会得到不同的结果。
  • geotile_grid 的情况一样,单个形状可以在多个瓦片中进行计数。如果形状的任何部分与该瓦片相交,则该形状将有助于匹配值的计数。下面是一张演示此情况的图像:

geoshape hexgrid

选项

编辑

field

(必需,字符串)包含索引的地理坐标点或地理形状值的字段。必须显式映射为 geo_pointgeo_shape 字段。如果该字段包含一个数组,则 geohex_grid 会聚合所有数组值。

precision

(可选,整数)用于定义结果中单元格/桶的键的整数缩放。默认为 6。将拒绝 [0,15] 范围之外的值。

bounds

(可选,对象)用于过滤每个桶中的地理坐标点或地理形状的边界框。接受与 地理边界框查询 相同的边界框格式。

size

(可选,整数)要返回的最大桶数。默认为 10,000。当结果被修剪时,桶的优先级基于它们包含的文档数量。

shard_size

(可选,整数)从每个分片返回的桶数。默认为 max(10,(size x number-of-shards)),以便在最终结果中更准确地计数顶级单元格。由于每个分片可能有不同的顶级结果顺序,因此在此处使用更大的数字可以降低计数不准确的风险,但会产生性能成本。