Geotile 网格聚合

编辑

一种多桶聚合,将 geo_pointgeo_shape 值分组到表示网格的桶中。生成的网格可以是稀疏的,并且仅包含具有匹配数据的单元格。每个单元格对应于许多在线地图网站使用的地图切片。每个单元格都使用“{zoom}/{x}/{y}”格式标记,其中 zoom 等于用户指定的精度。

  • 高精度键具有较大的 x 和 y 范围,表示仅覆盖小区域的切片。
  • 低精度键具有较小的 x 和 y 范围,表示每个切片都覆盖较大区域。

请参阅 缩放级别文档,了解精度(缩放)如何与地面大小相关。此聚合的精度可以在 0 到 29 之间(包括 0 和 29)。

长度为 29 的最高精度 geotile 生成的单元格覆盖面积小于 10 厘米 x 10 厘米的陆地,因此高精度请求在 RAM 和结果大小方面可能非常昂贵。请参阅下面的示例,了解如何在请求高细节级别之前先将聚合过滤到较小的地理区域。

您只能使用 geotile_grid 来聚合显式映射的 geo_pointgeo_shape 字段。如果 geo_point 字段包含数组,则 geotile_grid 会聚合所有数组值。

简单的低精度请求

编辑
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": {
            "geotile_grid": {
                "field": "location",
                "precision": 8
            }
        }
    },
)
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": {
        geotile_grid: {
          field: 'location',
          precision: 8
        }
      }
    }
  }
)
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": {
      geotile_grid: {
        field: "location",
        precision: 8,
      },
    },
  },
});
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": {
      "geotile_grid": {
        "field": "location",
        "precision": 8
      }
    }
  }
}

响应

{
  ...
  "aggregations": {
    "large-grid": {
      "buckets": [
        {
          "key": "8/131/84",
          "doc_count": 3
        },
        {
          "key": "8/129/88",
          "doc_count": 2
        },
        {
          "key": "8/131/85",
          "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": {
                    "geotile_grid": {
                        "field": "location",
                        "precision": 22
                    }
                }
            }
        }
    },
)
print(resp)
response = client.search(
  index: 'museums',
  size: 0,
  body: {
    aggregations: {
      "zoomed-in": {
        filter: {
          geo_bounding_box: {
            location: {
              top_left: 'POINT (4.9 52.4)',
              bottom_right: 'POINT (5.0 52.3)'
            }
          }
        },
        aggregations: {
          "zoom1": {
            geotile_grid: {
              field: 'location',
              precision: 22
            }
          }
        }
      }
    }
  }
)
puts response
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: {
          geotile_grid: {
            field: "location",
            precision: 22,
          },
        },
      },
    },
  },
});
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": {
          "geotile_grid": {
            "field": "location",
            "precision": 22
          }
        }
      }
    }
  }
}

响应

{
  ...
  "aggregations": {
    "zoomed-in": {
      "doc_count": 3,
      "zoom1": {
        "buckets": [
          {
            "key": "22/2154412/1378379",
            "doc_count": 1
          },
          {
            "key": "22/2154385/1378332",
            "doc_count": 1
          },
          {
            "key": "22/2154259/1378425",
            "doc_count": 1
          }
        ]
      }
    }
  }
}

使用其他边界框筛选的请求

编辑

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

resp = client.search(
    index="museums",
    size="0",
    aggregations={
        "tiles-in-bounds": {
            "geotile_grid": {
                "field": "location",
                "precision": 22,
                "bounds": {
                    "top_left": "POINT (4.9 52.4)",
                    "bottom_right": "POINT (5.0 52.3)"
                }
            }
        }
    },
)
print(resp)
response = client.search(
  index: 'museums',
  size: 0,
  body: {
    aggregations: {
      "tiles-in-bounds": {
        geotile_grid: {
          field: 'location',
          precision: 22,
          bounds: {
            top_left: 'POINT (4.9 52.4)',
            bottom_right: 'POINT (5.0 52.3)'
          }
        }
      }
    }
  }
)
puts response
const response = await client.search({
  index: "museums",
  size: 0,
  aggregations: {
    "tiles-in-bounds": {
      geotile_grid: {
        field: "location",
        precision: 22,
        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": {
      "geotile_grid": {
        "field": "location",
        "precision": 22,
        "bounds": {
          "top_left": "POINT (4.9 52.4)",
          "bottom_right": "POINT (5.0 52.3)"
        }
      }
    }
  }
}

响应

{
  ...
  "aggregations": {
    "tiles-in-bounds": {
      "buckets": [
        {
          "key": "22/2154412/1378379",
          "doc_count": 1
        },
        {
          "key": "22/2154385/1378332",
          "doc_count": 1
        },
        {
          "key": "22/2154259/1378425",
          "doc_count": 1
        }
      ]
    }
  }
}

聚合 geo_shape 字段

编辑

Geoshape 字段进行聚合的工作方式与点几乎相同,只是单个形状可以在多个切片中进行计数。如果形状的任何部分与该切片相交,则该形状将计入匹配值的计数。下面是演示此情况的图像

geoshape grid

选项

编辑

field

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

precision

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

bounds

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

size

(可选,整数)要返回的最大桶数。默认为 10,000。当结果被截断时,桶会根据其中包含的文档量进行优先级排序。

shard_size

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