# 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:** 1\
**Showing post:** 3

<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;
    }
}
```

---

_[View the full topic](https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552)._
