在Elasticsearch距离脚本中访问嵌套属性. [英] Accessing nested property in Elasticsearch distance script.

查看:196
本文介绍了在Elasticsearch距离脚本中访问嵌套属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My index in elastic search has the following mapping:

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

源文档如下:

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

我正在尝试使用距离脚本来基于地理点计算距离.我发现了这篇帖子 elasticsearch结果中的返回距离?可以帮助我.我正在尝试获取所有结果,按半径1km进行过滤,获取距离,然后对geo_point进行排序.查询的结构如下:

I am trying to use the distance script to calculate the distance based on geo-points. I found this post Return distance in elasticsearch results? to help me out. I am trying to get all results,filter by radius 1km, get the distance, and sort on geo_point. The query is constructed as follows:

{
    "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的以下错误:

I get the following error with status 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]]"

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

I tried rewriting the query in this way:

..."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]]; "

当我从查询中一起删除脚本部分时,排序和过滤就很好了.使用脚本时,是否有另一种访问嵌套字段的方法?任何见解将不胜感激!

When I remove the script part from the query all together, the sorting and filtering works just fine. Is there a different way to access nested fields when using scripts? Any insights would be really appreciated!

谢谢!

推荐答案

正如我在注释中提到的,当您通过_geo_distance进行排序时,返回的"_sort"字段是实际距离.因此,无需进行单独的计算.此处的详细信息:

As mentioned in my comment when you sort by _geo_distance the "_sort" field that is returned, is the actual distance. So there is no need to do a separate computation. Details here: http://elasticsearch-users.115913.n3.nabble.com/search-by-distance-and-getting-the-actual-distance-td3317140.html#a3936224

这篇关于在Elasticsearch距离脚本中访问嵌套属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆