# Painless loop on geopoints array

**URL:** <https://discuss.elastic.co/t/painless-loop-on-geopoints-array/170037>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 26, 2019, 3:35pm UTC](https://discuss.elastic.co/t/painless-loop-on-geopoints-array/170037 "2019-02-26T15:35:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [February 26, 2019, 3:35pm UTC](https://discuss.elastic.co/t/painless-loop-on-geopoints-array/170037/1 "2019-02-26T15:35:26Z")

</div>

Hi there!

I'm trying to loop over a geo\_point array in my painless script, but I can't find a way to make it work.  
My script is called in a [script\_score stage](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score).

Here is the part of code not working :

```auto
for (def i=0; i < doc['locations'].values.length; i++) {
    distance = Math.min(distance, doc['locations'].values[i].arcDistance(lat, lon) * 0.001);
}

```

And here's what I get :

```auto
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
      "distance = Math.min(distance, doc['locations'].values[i].arcDistance(lat, lon) * 0.001); }",
      " ^---- HERE"
],

```

I also have this :  
`"reason": "Unable to find dynamic method [arcDistance] with [2] arguments for class [org.elasticsearch.common.geo.GeoPoint]."`  
I tried multiple things with my `i`...

Do anyone of you see what I'm doing wrong?

Thank you!

---

<div class="post-metadata">

**Author:** ![daganm](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@daganm](https://discuss.elastic.co/u/daganm)\
**Post date:** [February 27, 2019, 4:46pm UTC](https://discuss.elastic.co/t/painless-loop-on-geopoints-array/170037/2 "2019-02-27T16:46:05Z")

</div>

I've found a few things from here [https://github.com/elastic/elasticsearch/issues/25796](https://github.com/elastic/elasticsearch/issues/25796)

And as the only suggestion is labelled for v7.0, I used the last answer loop, which calculates the distance without the arcDistance function...

---

<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:** [March 27, 2019, 4:46pm UTC](https://discuss.elastic.co/t/painless-loop-on-geopoints-array/170037/3 "2019-03-27T16:46:07Z")

</div>

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