Elasticsearch - MissingMethodException on running distanceInKm on geo_point array

自古美人都是妖i 提交于 2019-12-11 11:47:41

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32213624/elasticsearch-missingmethodexception-on-running-distanceinkm-on-geo-point-arra

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