# Compile error when using script with Terms aggregation

**URL:** <https://discuss.elastic.co/t/compile-error-when-using-script-with-terms-aggregation/91715>\
**Category:** Elasticsearch\
**Created:** [July 4, 2017, 6:22am UTC](https://discuss.elastic.co/t/compile-error-when-using-script-with-terms-aggregation/91715 "2017-07-04T06:22:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsameer](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@iamsameer](https://discuss.elastic.co/u/iamsameer)\
**Post date:** [July 4, 2017, 6:22am UTC](https://discuss.elastic.co/t/compile-error-when-using-script-with-terms-aggregation/91715/1 "2017-07-04T06:22:56Z")

</div>

Hi,

After upgrading to elastic search 5.4, script with terms aggregation giving compile error which were working properly in previous version 2.3.  
I am getting compile error for "list.location" aggregation.  
Below is the code snippet for your reference.

GET myindex/mytype/\_search  
{  
"\_source": ["id","title","location","country"],  
"query": {  
something...  
},  
"aggs":{  
"list.location":{  
"terms":{  
"script": "result=; country = doc['country.untouched'].value + '##'; if (doc['location.untouched'].empty) {country + 'null';} else {for(term in doc['location.untouched'].values){result.add(country + term)}; return result}"  
}  
}  
}  
}

Below is attached error screenshot.  
 ![](https://us1.discourse-cdn.com/elastic/original/3X/a/b/abc76edb824c2ddf4d06442d7af0ec75a078623a.JPG)

Can you please help me to find solution for this..

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![iamsameer](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@iamsameer](https://discuss.elastic.co/u/iamsameer)\
**Post date:** [July 5, 2017, 12:11pm UTC](https://discuss.elastic.co/t/compile-error-when-using-script-with-terms-aggregation/91715/2 "2017-07-05T12:11:11Z")

</div>

I got the solution by modifying script as below.

def result=[]; def country = doc['country.untouched'].value + '##'; if (doc['location.untouched'].empty) {country + 'null';} else {for(term in doc['location.untouched'].values){result.add(country + term)} return result}

Thanks (y)

---

<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:** [August 2, 2017, 12:11pm UTC](https://discuss.elastic.co/t/compile-error-when-using-script-with-terms-aggregation/91715/3 "2017-08-02T12:11:14Z")

</div>

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