# In a painless script how do I iterate through document keys based on a pattern?

**URL:** <https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552>\
**Category:** Kibana\
**Created:** [June 24, 2020, 7:32pm UTC](https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552 "2020-06-24T19:32:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gthang](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@gthang](https://discuss.elastic.co/u/gthang)\
**Post date:** [June 24, 2020, 7:32pm UTC](https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552/1 "2020-06-24T19:32:34Z")

</div>

Hello,

I would like to come up with a scripted field of sum of all values based on document keys. So for example I have the following k=v pairs.  
doc['totalA'] = 1  
doc['totalB'] = 3  
doc['totalC'] = 3

I would like to sum all the values if the key matches total\*. How do I go about this? I can obviously explicitly mention each document name, I just would like to match a pattern and sum it all if possible?

Thanks!

---

<div class="post-metadata">

**Author:** ![mattkime](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattkime/32/43522_2.png) [@mattkime](https://discuss.elastic.co/u/mattkime)\
**Post date:** [June 24, 2020, 7:43pm UTC](https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552/2 "2020-06-24T19:43:54Z")

</div>

@gthang

Have you tried iterating over the `doc` map? [https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-field-context.html](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-field-context.html)

Someone working through a similar problem - [Loop over a map in Painless](https://discuss.elastic.co/t/loop-over-a-map-in-painless/70308)

---

<div class="post-metadata">

**Author:** ![gthang](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@gthang](https://discuss.elastic.co/u/gthang)\
**Post date:** [June 25, 2020, 11:46pm UTC](https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552/3 "2020-06-25T23:46:00Z")

</div>

If somebody stumbles into this when they ddg'ing or so,

I figured this out, I feel pretty dumb it took this long to figure something so simple out.

I found this [documentation](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference-shared-java-lang.html#painless-api-reference-shared-String) very useful.

```
double total = 0.0;
for (key in params._source.keySet()) {
    if (key.startsWith("total")) {
        total = total + doc[key].value;
    }
}
```

---

<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:** [July 23, 2020, 11:46pm UTC](https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552/4 "2020-07-23T23:46:15Z")

</div>

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