一尘不染

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

elasticsearch

我在elasticsearch中的索引具有以下映射:

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

源文档如下:

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

我正在尝试使用距离脚本来基于地理点计算距离。我在elasticsearch结果中发现了该帖子的Return
distance吗?帮我 我正在尝试获取所有结果,按半径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]]; "

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

谢谢!


阅读 758

收藏
2020-06-22

共1个答案

一尘不染

如我的评论中所述,当您按_geo_distance排序时,返回的“
_sort”字段是实际距离。因此,无需进行单独的计算。此处的详细信息:http : //elasticsearch-users.115913.n3.nabble.com/search-
by-distance-and-getting-the-actual-distance-
td3317140.html#a3936224

2020-06-22