ElasticSearch 地理定位搜索----Geo-point / geo_point 的使用方法


我们以地图资源分为两类 医院&景点
在这里插入图片描述

Mapping

三种定义位置的方式

  • “location”: {“lon”:116.403103,“lat”:39.923973}
  • “location”: [116.403103,39.923973]
  • “location”:“POINT (116.402888 39.932605)”
PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      },
      "name":{
        "type": "text"
      },
      "class":{
        "type":"keyword"
      }
    }
  }
}
POST /example/_bulk
{ "index":{} }
{"name": "故宫","class":"景点", "location": {"lon":116.403103,"lat":39.923973}}
{ "index":{} }
{"name": "天安门","class":"景点", "location": [116.403103,39.923973] } 
{ "index":{} }
{"name": "景山","class":"景点", "location":"POINT (116.402888 39.932605)" }
{ "index":{} }
{"name": "北海","class":"景点", "location":[116.391641,39.938552] }
{ "index":{} }
{"name": "北京大学第一医院","class":"医院", "location":[116.387329,39.93822] }
{ "index":{} }
{"name": "305医院","class":"医院", "location":[116.391066,39.93183] }
{ "index":{} }
{"name": "妇儿医院","class":"医院", "location":[116.388694,39.929811]}
{ "index":{} }
{"name": "协和医院","class":"医院", "location": [116.374106,39.918855] }

Geo-bounding box (矩形查询)

**加粗样式**
搜索范围内的景区

GET /example/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "class": {
              "value": "景点"
            }
          }
        }
      ], 
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": [116.39664,39.929244],
            "bottom_right": [116.412055,39.908243]
          }
        }
      }
    }
  }
}

边界框的顶点可以通过top_left和bottom_right或者top_right和bottom_left参数设置。 topLeft, bottomRight, topRight and bottomLeft都支持。可以使用简单的名称top、left、bottom和right分别设置值,而不是成对设置值。

"geo_bounding_box": {
   "location": {
      "top": 40.73,
      "left": -74.1,
      "bottom": 40.01,
      "right": -71.12
    }
  }
}

Geo-distance (距离查询)

在这里插入图片描述

  • 710m 北京大学第一医院
  • 1.35km 305医院
  • 1.4km 妇儿医院
  • 2.5km 妇儿医院

GET /example/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "class": {
              "value": "医院"
            }
          }
        }
      ], 
      "filter": {
        "geo_distance": {
          "distance": "2.5km",
          "location": [116.379285,39.939783]
        }
      }
    }
  }
}

Geo-polygon (多边形范围查询)

查询图形范围内的医院
在这里插入图片描述
设置图形点一定要首尾呼应A,B,C,D,A

GET /example/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "class": {
              "value": "医院"
            }
          }
        }
      ], 
      "filter": {
        "geo_polygon": {
          "location": {
            "points": [
              [116.379352,39.938856],[116.393222,39.939437],[116.398755,39.928704],
              [116.391784,39.922009],[116.380142,39.92212],[116.379352,39.938856]
            ]
          }
        }
      }
    }
  }
}

在这里插入图片描述

聚合

geo_distance 根据范围统计

  • unit : 默认情况下,距离单位是m(米),但它也可以接受:mi(英里),in(英寸),yd(码),km(公里),cm(厘米),mm(毫米)。
  • distance_type : 有两种距离计算模式:arc(默认)和plane。arc计算是最精确的。plane是最快的,但最不准确。当你的搜索环境比较“狭窄”,并且覆盖的地理区域比较小(约5公里)时,可以考虑使用acr搜索。对于非常大范围的搜索(例如跨大陆搜索),Plane将返回更高的误差范围。距离计算类型可以使用distance_type参数来设置
  • keyed : 将keyed标志设置为true将为每个bucket关联一个唯一的字符串键,并以散列而不是数组的形式返回范围:

GET /example/_search
{
  "size": 0,
  "aggs": {
    "testGeo": {
      "geo_distance": {
        "field": "location",
        "origin":[116.397862,39.913916],
        "unit": "m", 
        "keyed": false,
        "ranges": [
          {"from": 500, "to": 1000 ,"key":"500米-1公里"},
          {"from": 1000, "to": 2000,"key":"1-2公里" },
          {"from": 2000, "to": 5000,"key":"2-5公里"}
        ]
      }
    }
  }
}

keyed=false
在这里插入图片描述

keyed=true
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值