elasticsearch 地理位置统计agg_by_distance

说明

指定坐标,统计 100km 内餐馆数量,100-300km 内餐馆数量,300-500km 内餐馆数量

GET /meituan_v1/_search
{
  "size": 0, //其他记录不查询
  "aggs": {
    "count": {  // 自定义名字,在结果中显示
      "geo_distance": {
        "field": "location", // 查询的字段
        "origin": { // 圆心点坐标
          "lat": 78,
          "lon": 36
        },
        "unit": "km",  // 单位
        "ranges": [ // 范围
          {
            "to": 100  // 0-100km
          },
          {
            "from": 100,  // 100-300km
            "to": 300
          },
          {
            "from": 300,  // 300-500km
            "to": 500
          }
        ]
      }
    }
  }
}

查询结果:

{
    "took":0,
    "timed_out":false,
    "_shards":{
        "total":1,
        "successful":1,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":{
            "value":7,
            "relation":"eq"
        },
        "max_score":null,
        "hits":[

        ]
    },
    "aggregations":{
        "count":{ // 查询时自定义名字
            "buckets":[
                {
                    "key":"*-100.0",
                    "from":0,
                    "to":100,
                    "doc_count":3
                },
                {
                    "key":"100.0-300.0",
                    "from":100,
                    "to":300,
                    "doc_count":3
                },
                {
                    "key":"300.0-500.0",
                    "from":300,
                    "to":500,
                    "doc_count":0
                }
            ]
        }
    }
}

原文出处:/show_1IX1rtfAY8iu.html