弹性搜索5.x中地理距离查询中的返回距离

Sha*_*s88 3 distance geolocation elasticsearch

我需要使用地理距离查询在弹性搜索中从搜索到的坐标获取距离。我试过了

curl -XGET 'http://127.0.0.1:9200/branch-master/master/_search?pretty=1'  -d '
{
   "script_fields" : {
      "distance" : {
         "params" : {
            "lat" : 18.00,
            "lon" : 72.00
         },
         "script" : "doc[\u0027location\u0027].distanceInKm(lat,lon)"
      }
   }
}
'
Run Code Online (Sandbox Code Playgroud)

这是从Elasticsearch结果中的返回距离获得的

但这给了我错误“ [params]中start_object的未知键”。我不明白这是什么。上面的解决方案似乎是针对较旧版本的ES的。我正在使用ES 5.1。在手册中,我找不到地理距离查询的相关解决方案。有人可以帮我吗?

sch*_*rht 5

该方法distanceInKm已过时,现已删除(5.x)。例如使用arcDistance代替。此处提供更多信息:https : //www.elastic.co/guide/zh-CN/elasticsearch/reference/current/breaking_50_scripting.html#_geopoint_scripts

语法稍有变化。这在Elasticsearch 5.6中对我有用。

{
    "script_fields": {
        "geo_distance": {
            "script": {
                "params": {
                    "lat": 18.00,
                    "lon": 72.00
                },
                "inline": "doc['location'].arcDistance(params.lat, params.lon)"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)