Cant access a nested field when using distance script


(Levon Tamrazov) #1

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 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:
"CompileException[[Error: No field found for [properties.location] in mapping with types [couchbaseDocument]]\n[Near : {... doc['properties.location'].arc ....}]\n ^\n[Line: 1, Column: 1]]; nested: ElasticSearchIllegalArgumentException[No field found for [properties.location] in mapping with types [couchbaseDocument]]; "

I tried rewriting the query in this way:

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

Got the same error. Then tried this :

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

Then I get this error:

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

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

Thank you!


(system) #2