Script transform gives an internal server error in watcher

Hello,
I wanted to save aggregations results into a new indice to reduce real time calculations on Kibana, so I created a watcher that fetches the result of my pipelined aggregations (4 nested bucket arrays) into one list and indexes it into the new indice, the pipelined buckets are described like this:

ctx.payload.aggregations.agg1.buckets
ctx.payload.aggregations.agg1.buckets.agg2.buckets
ctx.payload.aggregations.agg1.buckets.agg2.buckets.agg3.buckets
ctx.payload.aggregations.agg1.buckets.agg2.buckets.agg3.buckets.agg4.buckets

Here is my transform script

def trips= ArrayList();
for (agg1 in ctx.payload.aggregations.agg1.buckets){ 
  for (agg2 in agg1.agg2.buckets){ 
    for (agg3 in agg2.agg3.buckets){ 
       for (agg4 in agg3.agg4.buckets){
          trips.add(['rider_full_name':agg1.key, 'rider_email':agg2.key, 'rider_phone':agg3.key,'rider_id':agg4.key, 'nb_requests':agg4.doc_count,'nb_finished':agg4.nb_finished, 'first_request':agg4.first_request,'last_request':agg4.last_request, 'first_trip':agg4.first_trip, 'last_trip':agg4.last_trip, 'sum_cost':agg4.sum_cost, 'sum_distance':agg4.sum_distance, '@timestamp':agg4.timestamp]);
       }
    }
  }
}

The execution of this script gives me the following error:

Watcher: An internal server error occurred

Any help would be really appreciated :slight_smile: Thanks in advance.

For those who got a similar issue, it was actually a syntax error in my loop code, solved my problem by using the following script:

def trips= ArrayList();
for (def agg1 : ctx.payload.aggregations.agg1.buckets){ 
  for (def agg2 : agg1.agg2.buckets){ 
    for (def agg3 : agg2.agg3.buckets){ 
       for (def agg4 : agg3.agg4.buckets){
          trips.add(['rider_full_name':agg1.key, 'rider_email':agg2.key, 'rider_phone':agg3.key,'rider_id':agg4.key, 'nb_requests':agg4.doc_count,'nb_finished':agg4['nb_finished'].value, 'first_request':agg4['first_request'].value_as_string,'last_request':agg4['last_request'].value_as_string, 'first_trip':agg4['first_trip'].value_as_string, 'last_trip':agg4['last_trip'].value_as_string, 'sum_cost':agg4['sum_cost'].value, 'sum_distance':agg4['sum_distance'].value, '@timestamp':agg4['timestamp'].value_as_string]);
       }
    }
  }
}

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