# How to iterate over Geopoints in painless?

**URL:** <https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 12, 2020, 1:33am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648 "2020-08-12T01:33:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ClubberLang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clubberlang/32/72169_2.png) [@ClubberLang](https://discuss.elastic.co/u/ClubberLang)\
**Post date:** [August 12, 2020, 1:33am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648/1 "2020-08-12T01:33:22Z")

</div>

Hi,

I have a painless script, it iterate over a set of geopoints and compute the shortest distance.  
But it seems that there is an error:

> "script\_fields": {  
> "distance": {  
> "script": {  
> "source": "double d = Double.MAX\_VALUE; for (gp in doc['geoLocations']) d = Math.min(d, gp.planeDistance(params.lat,params.lon) \* 0.001); return d;",  
> "lang": "painless",  
> "params": {  
> "lat": {{latitude}},  
> "lon": {{longitude}}  
> }  
> }  
> }

Here is the error message:  
"reason": "dynamic method [org.elasticsearch.common.geo.GeoPoint, planeDistance/2] not found"

I have checked my mapping and I have this:

```
    "geoLocations": {
      "type": "geo_point"
    }

```

So, if someone has an idea of the issue ?

Thanks

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 12, 2020, 8:54am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648/2 "2020-08-12T08:54:18Z")

</div>

I quickly peeked into the source, and you should have a method like `doc['geoLocations'].planeDistance(lat, lon)`...

hope this helps!

---

<div class="post-metadata">

**Author:** ![ClubberLang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clubberlang/32/72169_2.png) [@ClubberLang](https://discuss.elastic.co/u/ClubberLang)\
**Post date:** [August 12, 2020, 9:01am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648/3 "2020-08-12T09:01:43Z")

</div>

Thanks, but the problem with this solution is that it only use the first geopoints, I'm looking for the closest one.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 12, 2020, 9:05am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648/4 "2020-08-12T09:05:03Z")

</div>

Indeed. feel free to open a feature report in github.

Just to make sure I understand the problem space fully: Can you send an additional search request and sort by geo distance instead of using a script? Do you have to use scripting here?

See [https://www.elastic.co/guide/en/elasticsearch/reference/7.8/sort-search-results.html#geo-sorting](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/sort-search-results.html#geo-sorting)

---

<div class="post-metadata">

**Author:** ![ClubberLang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clubberlang/32/72169_2.png) [@ClubberLang](https://discuss.elastic.co/u/ClubberLang)\
**Post date:** [August 12, 2020, 9:21am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648/5 "2020-08-12T09:21:28Z")

</div>

Thanks for your suggestion,

Such request exist since a long time, see:

> <https://github.com/elastic/elasticsearch/issues/25796>
>
> It will be really confortable to allow call to arcDistance from GeoPoint. Indeed… actually we can only call them from GeoPoints (and that will use only the first point).

In the current situation, the script is necessary... it allows me to generate fields for debugging my DSL ! I will remove it in production.

It is to debug another query, that use distance\_feature (That I can't make it work):

> [@Multiply scores between 'distance\_feature' and 'dismax'?](https://discuss.elastic.co/t/multiply-scores-between-distance-feature-and-dismax/244638):
>
> Hi, Imagine I have 3 queries: "distance\_feature": { ... time... } "distance\_feature": { ... location... } "dis\_max" : { ... } Currently, to combine it I do this: "query" : { "bool" : { "must" : ["distance\_feature": { ... time... }, "distance\_feature": { ... location... }, "dis\_max" : { ... },] } } The problem is that the score of computes as a sum, and I'm looking for a way to "multiply" instead. I have try with function score, but I can't put them there, right ? Do y…

My sorting is based on multiples criterions... not only the distance.

But anyway, I suspect that I should use it in my query... (I don't like this, but maybe there are no other choices).

---

<div class="post-metadata">

**Author:** ![ClubberLang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clubberlang/32/72169_2.png) [@ClubberLang](https://discuss.elastic.co/u/ClubberLang)\
**Post date:** [August 12, 2020, 9:46am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648/6 "2020-08-12T09:46:28Z")

</div>

BTW, I can request the feature on GitHub, but the problem is that I need something asap to enter in production, you see 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 9, 2020, 9:46am UTC](https://discuss.elastic.co/t/how-to-iterate-over-geopoints-in-painless/244648/7 "2020-09-09T09:46:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
