# Looping through hits using painless script

**URL:** <https://discuss.elastic.co/t/looping-through-hits-using-painless-script/117718>\
**Category:** Elasticsearch\
**Created:** [January 30, 2018, 11:26pm UTC](https://discuss.elastic.co/t/looping-through-hits-using-painless-script/117718 "2018-01-30T23:26:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prabhath](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@prabhath](https://discuss.elastic.co/u/prabhath)\
**Post date:** [January 30, 2018, 11:26pm UTC](https://discuss.elastic.co/t/looping-through-hits-using-painless-script/117718/1 "2018-01-30T23:26:45Z")

</div>

Is there a way In Elasticsearch 5.3 to loop through query results using painless script to compute difference between the hits returned by query.

I was trying to loop through the results in a for loop in script and could not what entity to loop on, i was hopping that scripts have something like the update api has ctx.payload.hits.hits that can be used in for loop.

```
"script": {
			"lang": "painless",
			"inline": "int total = 0; for (int i = 0; i < ??? ; i++) { total += ???[i]._source['age'].value; } return total;"
		}

```

Thanks

---

<div class="post-metadata">

**Author:** ![jpcarey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpcarey/32/46668_2.png) [@jpcarey](https://discuss.elastic.co/u/jpcarey)\
**Post date:** [January 31, 2018, 12:10am UTC](https://discuss.elastic.co/t/looping-through-hits-using-painless-script/117718/2 "2018-01-31T00:10:50Z")

</div>

I'm not entirely clear on what you are trying to accomplish. If you are looking to calculate the difference between each resulting item based upon the query criteria, you would likely need to use a scripted aggregation.  
[https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-scripted-metric-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-scripted-metric-aggregation.html)

---

<div class="post-metadata">

**Author:** ![prabhath](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@prabhath](https://discuss.elastic.co/u/prabhath)\
**Post date:** [January 31, 2018, 1:03am UTC](https://discuss.elastic.co/t/looping-through-hits-using-painless-script/117718/3 "2018-01-31T01:03:18Z")

</div>

How will that turn out if the field is a Timestamp field. During the combine phase of Scripted metric aggregation. if the data is randomly stored across the shards then there is a possibility of overlapping ranges.

Shard A:

```
"params" : {

  "_agg" : {

    "transactions" : [1/01/2018, 5/01/2018]

  }

}

```

Shard B:

```
"params" : {

"_agg" : {

"transactions" : [2/01/2018, 4/01/2018]

}

}

```

if the map function is to subtract the results in each shard.

result of shard A is [4]

result of shard B is [2]

and final reduce function is to calculate sum of results from above 2 shards.

```
{

"results": 6

}

```

when it should have been 4 months.

---

<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:** [February 28, 2018, 1:16am UTC](https://discuss.elastic.co/t/looping-through-hits-using-painless-script/117718/4 "2018-02-28T01:16:38Z")

</div>

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