Accessing nested property in Elasticsearch distance script.

别来无恙 提交于 2019-12-08 12:54:29

问题


My index in elastic search has the following mapping:

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

The source document is as follows:

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

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
}

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)"...

Then I get this error:

"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!

Thank you!


回答1:


Managed to get it done with

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

Don't know why but doc['last_location'] does not seem to work at all!




回答2:


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



来源:https://stackoverflow.com/questions/20295351/accessing-nested-property-in-elasticsearch-distance-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!