AI自媒体写作平台推荐:按每天交稿量选写作和排期工具
2026-11-28
2026-11-27 0
在地图上显示当前屏幕范围内的门店,和查找中心点附近的门店,是两种不同的筛选任务。下面用 Elasticsearch 8.19.0 和四条合成坐标演示,可以在 Kibana 的开发工具 → Console 逐段运行。
先在新实验索引中把 location 映射为 geo_point。对象用 lat 表示纬度、lon 表示经度。若改用 JSON 数组,顺序为 [经度, 纬度],逗号字符串的顺序为“纬度,经度”。写入时使用 refresh=wait_for,使对应请求返回后该文档对搜索可见。
PUT /geo-point-lab-32
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
PUT /geo-point-lab-32/_doc/center?refresh=wait_for
{
"location": {
"lat": 31.2304,
"lon": 121.4737
}
}
PUT /geo-point-lab-32/_doc/east?refresh=wait_for
{
"location": {
"lat": 31.2304,
"lon": 121.499
}
}
PUT /geo-point-lab-32/_doc/north?refresh=wait_for
{
"location": {
"lat": 31.265,
"lon": 121.4737
}
}
PUT /geo-point-lab-32/_doc/corner?refresh=wait_for
{
"location": {
"lat": 31.249,
"lon": 121.492
}
}
GET /geo-point-lab-32/_mapping

以 center 的坐标为圆心,筛选 2.5 公里内的文档。distance 显式带单位,geo_distance 默认使用 arc 计算地表距离。查询放进 bool.filter,因为这里仅按地理条件筛选。_source 设为 false,响应只需查看 hits.total.value 和各条命中的 _id。
GET /geo-point-lab-32/_search
{
"track_total_hits": true,
"_source": false,
"query": {
"bool": {
"filter": [
{
"geo_distance": {
"distance": "2.5km",
"location": {
"lat": 31.2304,
"lon": 121.4737
}
}
}
]
}
}
}

用左上角 top_left 和右下角 bottom_right 描述视口。在本例不跨日期变更线,左上角是西北角,右下角是东南角,北纬上限为 31.25,经度从 121.45 到 121.495。边线附近若涉及严格阈值,应考虑地理坐标的索引舍入,用业务样本实测临界点。
GET /geo-point-lab-32/_search
{
"track_total_hits": true,
"_source": false,
"query": {
"bool": {
"filter": [
{
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 31.25,
"lon": 121.45
},
"bottom_right": {
"lat": 31.21,
"lon": 121.495
}
}
}
}
]
}
}
}

把两个查询同时放进 bool.filter 数组,文档必须满足两个筛选条件。若 location 存储多个点,两条过滤可能分别由不同的点满足。本节的每条文档只含一个坐标,因此可以直接用图中的交集验证。
GET /geo-point-lab-32/_search
{
"track_total_hits": true,
"_source": false,
"query": {
"bool": {
"filter": [
{
"geo_distance": {
"distance": "2.5km",
"location": {
"lat": 31.2304,
"lon": 121.4737
}
}
},
{
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 31.25,
"lon": 121.45
},
"bottom_right": {
"lat": 31.21,
"lon": 121.495
}
}
}
}
]
}
}
}

附近搜索的输入是中心和半径,视口筛选的输入是矩形对角坐标。两种筛选都启用时放入同一组 filter。遇到空结果,可以依次核对字段映射、经纬度次序与搜索可见性。