# Painless : Loop through all documents of index

**URL:** <https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 19, 2020, 11:03am UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302 "2020-05-19T11:03:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elisavet](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@elisavet](https://discuss.elastic.co/u/elisavet)\
**Post date:** [May 19, 2020, 11:03am UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302/1 "2020-05-19T11:03:15Z")

</div>

Hi!

Is there a way to loop through all documents with scripted fields?

I want to divide two sums for a given day, and since dividing two aggregated values is impossible, I was thinking if there is a workaround with scripted fileds.

I am thinking of doing something like this:

```auto
double totalprice=0; 
double totalquantity=0; 
def current_day=doc['day_of_week'].value; 

for (int i=0;i< ALL DOCUMENTS ;++i) 
{ 
   if (current_day==doc['day_of_week'][i]) 
    { totalprice=totalprice+doc['products.price'][i];
      totalquantity=totalquantity+doc['products.quantity'][i];
    } 
    
}

return totalprice/totalquantity; 

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 23, 2020, 9:42pm UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302/2 "2020-05-23T21:42:21Z")

</div>

I do not think that is possible as scripted fields run in the context of a single document.

---

<div class="post-metadata">

**Author:** ![elisavet](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@elisavet](https://discuss.elastic.co/u/elisavet)\
**Post date:** [May 25, 2020, 6:25am UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302/3 "2020-05-25T06:25:15Z")

</div>

Ok! Thank you for your answer!

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [May 25, 2020, 7:12am UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302/4 "2020-05-25T07:12:15Z")

</div>

Hello @elisavet

I think you can use an `average aggregation` to get the same result. See  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html)

At the same time, it would be possible to use `scripted metric` to do what you wish, but the iteration will be done by Elasticsearch. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)

---

<div class="post-metadata">

**Author:** ![elisavet](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@elisavet](https://discuss.elastic.co/u/elisavet)\
**Post date:** [May 25, 2020, 7:40am UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302/5 "2020-05-25T07:40:46Z")

</div>

Hello @Luca_Belluccini!

Thank you for your answer. I forgot to mention that I want the ratio to be visualised in kibana . Therefore, how could I use the `average aggregation` and `scripted metric` to show the ratio in kibana? Should I create a new index?

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [May 25, 2020, 8:07am UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302/6 "2020-05-25T08:07:26Z")

</div>

In Kibana the average will be calculated for the time range you'll choose.  
If you want to get the average on all the documents regardless the time picker, you will have to create an index pattern without time field.

---

<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:** [June 22, 2020, 8:07am UTC](https://discuss.elastic.co/t/painless-loop-through-all-documents-of-index/233302/7 "2020-06-22T08:07:29Z")

</div>

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