在Elasticsearch距离脚本中访问嵌套属性。

时间:2013-11-30 01:03:14

标签: elasticsearch geo

我在弹性搜索中的索引具有以下映射:

"couchbaseDocument": {
      "properties": {
         "doc": {
            "properties": {
               "properties": {
                  "properties": {
                     "location": {
                        "type": "geo_point"

源文件如下:

{"properties" : {"location":"43.706596,-79.4030464"}}

我正在尝试使用距离脚本来计算基于地理点的距离。我发现这篇帖子Return distance in elasticsearch results?来帮助我。我试图获得所有结果,按半径1km过滤,获取距离,并在geo_point上排序。查询构造如下:

{
    "query": {
        "match_all": {}
    },
    "filter": {
        "geo_distance": {
           "distance": "1km",
           "doc.properties.location": {
              "lat": 43.710323,
              "lon": -79.395284
           }
        }
    },

    "script_fields": {
       "distancePLANE": {
            "params": {
               "lat": 43.710323,
               "lon": -79.395284
           }, 
          "script": "doc[properties]['location'].distanceInKm(lat, lon)"
       }, 
       "distanceARC" :{
           "params": {
               "lat": 43.710323,
               "lon": -79.395284
           }, 
           "script": "doc[properties]['location'].arcDistanceInKm(lat,lon)"
       }
    }, 

    "sort": [
       {
           "_geo_distance":{
               "doc.properties.location": [-79.395284,43.710323],
                "order": "desc",
                "unit": "km"
           }
       }
    ],
    "track_scores": true
}

状态500出现以下错误:

"PropertyAccessException[[Error: could not access: properties; in class: org.elasticsearch.search.lookup.DocLookup]\n[Near : {... doc[properties]['location'].distan ....}]\n                 ^\n[Line: 1, Column: 5]]"

我尝试用这种方式重写查询:

..."script": "doc['properties']['location'].arcDistanceInKm(lat,lon)"...

然后我收到此错误:

"CompileException[[Error: No field found for [properties] in mapping with types [couchbaseDocument]]\n[Near : {... doc['properties']['location']. ....}]\n             ^\n[Line: 1, Column: 1]]; nested: ElasticSearchIllegalArgumentException[No field found for [properties] in mapping with types [couchbaseDocument]]; "

当我从查询中一起删除脚本部分时,排序和过滤工作正常。使用脚本时是否有不同的方法来访问嵌套字段?任何见解都会非常感激!

谢谢!

2 个答案:

答案 0 :(得分:0)

管理完成

"script" : "doc.last_location.distance(41.12, -71.34)"

不知道为什么,但doc['last_location']似乎根本不起作用!

答案 1 :(得分:0)

正如我在评论中提到的那样,按_geo_distance排序时返回的“_sort”字段是实际距离。所以不需要进行单独的计算。详情请见http://elasticsearch-users.115913.n3.nabble.com/search-by-distance-and-getting-the-actual-distance-td3317140.html#a3936224