Nested Aggregation in a Transforms Script and collect value

What is the syntax for loop through all the buckets inside the map statement

Below is my aggregation code:

    "aggs": {  
 "account_agg" : { 
    "terms": { 
                "field": "client"},
                "aggs" : {
                "account_month" : {
                     "terms" : {
                         "field" : "month",
                         "size" : 7
                    } ,
                       "aggs":{
                         "min_txncount":{
              "min":{
                "field":"txn_count"
              }
            },
            "max_txncount":{
              "max":{
                "field":"txn_count"
              }   } } } }
                     
                 }
     
   }

Below is my transform script:

 "transform" : {
      "script":
      """
        return ['txn_diff': ctx.payload.aggregations.account_agg.buckets.stream().map( t -> {                                                   
                return ['account': t.key]}).collect(Collectors.toList())]
      """
    }

But, I want to collect the key and min_txncount & max_txncount from account_month buckets.

How to place the for loop:

My for loop construct:

for (int i = 0; i < ctx.payload.aggregations.account_agg.buckets.size(); i++)
	{for (int j = 0; j < ctx.payload.aggregations.account_agg.buckets[i].account_agg.buckets.size(); j++){ if ( ctx.payload.aggregations.account_agg.buckets[i].account_agg.buckets[j].min_txncount.value  != ctx.payload.aggregations.account_agg.buckets[i].account_agg.buckets[j].max_txncount.value)

Thanks & Regards

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