面试题答案
一键面试- 基本步骤:
- 定义矩形区域:确定矩形区域的四个顶点的经纬度坐标,该矩形由左下角和右上角的坐标来界定。
- 构建查询:在ElasticSearch查询体中,使用地理范围聚合(
geo_bounding_box
)来指定矩形区域。 - 执行查询:向ElasticSearch发送查询请求,获取统计结果。
- 关键参数:
geo_bounding_box
:聚合类型,用于地理范围聚合。field
:指定文档中存储地理位置信息(经纬度)的字段名。top_left
:矩形区域左上角的经纬度坐标,格式为{"lat": latitude, "lon": longitude}
。bottom_right
:矩形区域右下角的经纬度坐标,格式为{"lat": latitude, "lon": longitude}
。
示例查询体如下:
{
"aggs": {
"within_rectangle": {
"geo_bounding_box": {
"field": "location",
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.63,
"lon": -73.9
}
}
}
}
}
在上述示例中,location
是存储地理位置信息的字段名,通过top_left
和bottom_right
定义了矩形区域。执行此查询后,ElasticSearch会统计位于该矩形区域内的文档数量,并在聚合结果中返回。