# Script transform gives an internal server error in watcher

**URL:** <https://discuss.elastic.co/t/script-transform-gives-an-internal-server-error-in-watcher/107808>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [November 15, 2017, 7:05pm UTC](https://discuss.elastic.co/t/script-transform-gives-an-internal-server-error-in-watcher/107808 "2017-11-15T19:05:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarra\_Bouhenni](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@Sarra\_Bouhenni](https://discuss.elastic.co/u/Sarra_Bouhenni)\
**Post date:** [November 15, 2017, 7:05pm UTC](https://discuss.elastic.co/t/script-transform-gives-an-internal-server-error-in-watcher/107808/1 "2017-11-15T19:05:11Z")

</div>

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

```auto
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:

```auto
Watcher: An internal server error occurred

```

Any help would be really appreciated 🙂 Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Sarra\_Bouhenni](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@Sarra\_Bouhenni](https://discuss.elastic.co/u/Sarra_Bouhenni)\
**Post date:** [November 19, 2017, 10:34am UTC](https://discuss.elastic.co/t/script-transform-gives-an-internal-server-error-in-watcher/107808/2 "2017-11-19T10:34:19Z")

</div>

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:

```auto
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]);
       }
    }
  }
}

```

---

<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:** [December 17, 2017, 10:34am UTC](https://discuss.elastic.co/t/script-transform-gives-an-internal-server-error-in-watcher/107808/3 "2017-12-17T10:34:24Z")

</div>

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