Search code examples
groovyelasticsearchelasticsearch-geo-shape

Elasticsearch - MissingMethodException on running distanceInKm on geo_point array


I have an elasticsearch document which has an array of geo_points. I have created the mapping as:

{
    "test": {
        "properties": {
            "locations": {
                "type": "geo_point"
            }
        }
    }
}

Now, I am trying to create a query in which I want to do some processing on the array of geo_points. I have created my query like this:

{
    "query": {
        "filtered": {
            "filter": {
                "script": {
                    "script": "sDistance = doc['locations'].values[0].distanceInKm(28.51818,77.096080);"
                }
            }
        }
    }
}

I want to calculate the distance of the point (28.51818,77.096080) from the first element in the locations array.

It is giving me this error:

GroovyScriptExecutionException[MissingMethodException[No signature of method: org.elasticsearch.common.geo.GeoPoint.distanceInKm() is applicable for argument types: (java.lang.Double, java.lang.Double) values: [28.51818, 77.09608]]

I tried using sDistance = doc['locations'][0].distanceInKm(28.51818,77.096080); but it also resulted in the same error.

What am I doing wrong here?

Thanks in advance.


Solution

  • Your script should not retrieve the first geo point by itself, but simply call distanceInKm on the locations field directly, like this:

    {
      "query": {
        "filtered": {
          "filter": {
            "script": {
              "script": "sDistance = doc['locations'].distanceInKm(28.51818,77.096080);"
            }
          }
        }
      }
    }
    

    Under the hood, the first geo point will be retrieved and the distance will be computed on it.