0
votes

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!

2
Sorry, I missed adding the closing brackets for the mapping in the question, but everything is present in the source code. - Levon Tamrazov
This doesn't really answer the question, but its a work around that works for me. I found out that 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. elasticsearch-users.115913.n3.nabble.com/… - Levon Tamrazov

2 Answers

1
votes

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!

1
votes

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