# Watcher - how to write painless script for looping aggregated buckets

**URL:** https://discuss.elastic.co/t/watcher-how-to-write-painless-script-for-looping-aggregated-buckets/119449
**Category:** Elasticsearch
**Created:** [February 12, 2018, 10:10am UTC](https://discuss.elastic.co/t/watcher-how-to-write-painless-script-for-looping-aggregated-buckets/119449 "2018-02-12T10:10:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![surya\_dadi\_dhamarake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surya_dadi_dhamarake/32/133245_2.png) [@surya\_dadi\_dhamarake](https://discuss.elastic.co/u/surya_dadi_dhamarake)
#### Post date: [February 12, 2018, 10:10am UTC](https://discuss.elastic.co/t/watcher-how-to-write-painless-script-for-looping-aggregated-buckets/119449/1 "2018-02-12T10:10:14Z")

</div>

Hi,  
I wrote a watcher to query some logs and aggregate using terms aggregations and under terms aggregations I wrote sub aggregation as follows

```
"aggs": {
        "host_wise": {
          "terms": {
            "field": "abc.keyword",
            "size": 3
          },
          "aggs": {
            "max_latency": {
              "max": {
                "field": "cde"
              }
            }
          }
        },
        "all": {
          "filter": {
            "query_string": {
              "query": "*",
              "analyze_wildcard": true
            }
          }
        }
      }

```

So I am getting buckets like

```
"buckets": [
          {
            "doc_count": 405,
            "max_latency": {
              "value": 30351
            },
            "key": "work1"
          },
          {
            "doc_count": 340,
            "max_latency": {
              "value": 15935
            },
            "key": "work2"
          }
        ]

```

How can I loop this buckets and create dynamic keys like max\_latency1 and max latency2 for both bucket values ?

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 12, 2018, 3:47pm UTC](https://discuss.elastic.co/t/watcher-how-to-write-painless-script-for-looping-aggregated-buckets/119449/2 "2018-02-12T15:47:33Z")

</div>

I am not sure I follow your use-case here. Do you want to find out the max latency or something else? Just looping through the result and collecting only the `value` fields would be something like

```auto
ctx.payload.aggregations.host_wise.buckets.stream().map(b -> b.max_latency.value).collect(Collectors.toList());

```

Hope this helps, otherwise can you please expand your use-case?

--Alex

---

<div class="post-metadata">

### Author: ![surya\_dadi\_dhamarake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surya_dadi_dhamarake/32/133245_2.png) [@surya\_dadi\_dhamarake](https://discuss.elastic.co/u/surya_dadi_dhamarake)
#### Post date: [February 12, 2018, 5:13pm UTC](https://discuss.elastic.co/t/watcher-how-to-write-painless-script-for-looping-aggregated-buckets/119449/3 "2018-02-12T17:13:09Z")

</div>

what the above code will give us? I mean what type of output it will return?  
what I need to do is get all the 'key' values when max\_latency value is greater than 10000.

---

<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 12, 2018, 5:13pm UTC](https://discuss.elastic.co/t/watcher-how-to-write-painless-script-for-looping-aggregated-buckets/119449/4 "2018-03-12T17:13:25Z")

</div>

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