ElasticSearch地理位置搜索基础

1. 创建mapping并插入数据

PUT /hotel_app
{
"mappings": {
"hotels": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
}

PUT /hotel_app/hotels/1
{
"name": "喜来登大酒店",
"pin" : {
"location" : {
"lat" : 40.12,
"lon" : -71.34
}
}
}

2. 在指定范围以内的搜索

GET /hotel_app/hotels/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": {
"geo_bounding_box": {
"pin.location": {
"top_left" : {
"lat" : 40.73,
"lon" : -74.1
},
"bottom_right" : {
"lat" : 40.01,
"lon" : -71.12
}
}
}
}
}
}
}

GET /hotel_app/hotels/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": {
"geo_polygon": {
"pin.location": {
"points": [
{"lat" : 40.73, "lon" : -74.1},
{"lat" : 40.01, "lon" : -71.12},
{"lat" : 50.56, "lon" : -90.58}
]
}
}
}
}
}
}

3. 附近xx米以内的点

GET /hotel_app/hotels/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}

4. 聚合函数

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
      }
    ]
  }
}

}
}

你可能感兴趣的:(ElasticSearch地理位置搜索基础)