ElasticSearch系列十一:掌握ES使用地理位置

一、geo point地理位置数据类型
1.建立geo_point类型的mapping
PUT /my_index 
{
  "mappings": {
"my_type": {
 "properties": {
"location": {
 "type": "geo_point"
}
 }
}
  }
}
2.写入geo_point的3种方法
PUT my_index/my_type/1
{
  "text": "Geo-point as an object",
  "location": { 
"lat": 41.12,
"lon": -71.34
  }
}
PUT my_index/my_type/2
{
  "text": "Geo-point as a string",
  "location": "41.12,-71.34" 
}
PUT my_index/my_type/4
{
  "text": "Geo-point as an array",
  "location": [ -71.34, 41.12 ] 
}
3.查询某个矩形的地理位置范围内的坐标点,比如41.12,-71.34是一个酒店,搜索的是从42,-72(代表了大厦A)和40,-74(代表了马路B)作为矩形的范围
GET /my_index/my_type/_search 
{
  "query": {
"geo_bounding_box": {
 "location": {
"top_left": {
 "lat": 42,
 "lon": -72
},
"bottom_right": {
 "lat": 40,
 "lon": -74
}
 }
}
  }
}
4.搜索出200km内的酒店
GET /hotel_app/hotels/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": {
        "geo_distance": {
          "distance": "200km",
          "pin.location": {
            "lat": 40,
            "lon": -70
          }
        }
      }
    }
  }
}
5.当前的位置的每段范围内,有多少家酒店
GET /hotel_app/hotels/_search
{
  "size": 0,
  "aggs": {
    "agg_by_distance_range": {
      "geo_distance": {
        "field": "pin.location",
        "origin": {
          "lat": 40,
          "lon": -70
        },
        "unit": "mi", 
        "ranges": [
          {
            "to": 100
          },
          {
            "from": 100,
            "to": 300
          },
          {
            "from": 300
          }
        ]
      }
    }
  }
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值