# Looping through nested aggregations error

**URL:** <https://discuss.elastic.co/t/looping-through-nested-aggregations-error/266408>\
**Category:** Elasticsearch\
**Created:** [March 5, 2021, 7:56pm UTC](https://discuss.elastic.co/t/looping-through-nested-aggregations-error/266408 "2021-03-05T19:56:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Faisal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_faisal/32/79213_2.png) [@Muhammad\_Faisal](https://discuss.elastic.co/u/Muhammad_Faisal)\
**Post date:** [March 5, 2021, 7:56pm UTC](https://discuss.elastic.co/t/looping-through-nested-aggregations-error/266408/1 "2021-03-05T19:56:48Z")

</div>

hi all,

i have serial difference metric aggregation, and need to filter those documents, whose delta or value is greater than zero ...need to loop through all aggregations in condition and check for value \>0 ( where Value is serial difference )

`{`  
`"trigger": {`  
`"schedule": {`  
`"hourly": {`  
`"minute": [`  
`8,`  
`23,`  
`38,`  
`53`  
`]`  
`}`  
`}`  
`},`  
`"input": {`  
`"search": {`  
`"request": {`  
`"search_type": "query_then_fetch",`  
`"indices": [`  
`"...."`  
`],`  
`"rest_total_hits_as_int": true,`

```
      "aggs": {
        "3": {
          `"terms": {`
            `"field": "host",`
            
          `},`
          `"aggs": {`
            `"4": {`
              `"terms": {`
                `"field": "Error",`
         
              `"aggs": {`
                `"2": {`
                  `"date_histogram": {
                    "field": "@timestamp",
                    "fixed_interval": "5m",
                   
                  },
                  "aggs": {
                    "1": {
                      "serial_diff": {
                        "buckets_path": "1-metric"
                      }
                    },
                    "1-metric": {
                      "sum": {
                        "field": "Value"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "script_fields": {},
      "docvalue_fields": [
        {
          "field": "@timestamp",
          "format": "date_time"
        }
      ],
      "_source": {
        "excludes": []
      },

```

` "condition": {`  
`"script": {`  
`"lang": "painless",`  
`"source": "return ctx.payload.aggregations.3.buckets.stream().anyMatch(cluster -> cluster.4.buckets.stream().anyMatch(xx -> xx.2.buckets.stream().anyMatch(node -> node.1.value > 0)));"`  
}` `}`

---

<div class="post-metadata">

**Author:** ![Muhammad\_Faisal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_faisal/32/79213_2.png) [@Muhammad\_Faisal](https://discuss.elastic.co/u/Muhammad_Faisal)\
**Post date:** [March 7, 2021, 8:08pm UTC](https://discuss.elastic.co/t/looping-through-nested-aggregations-error/266408/2 "2021-03-07T20:08:37Z")

</div>

hi guys ,  
anyone there

---

<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:** [April 4, 2021, 8:09pm UTC](https://discuss.elastic.co/t/looping-through-nested-aggregations-error/266408/3 "2021-04-04T20:09:29Z")

</div>

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