# Terms Aggregation with a Script not working

**URL:** <https://discuss.elastic.co/t/terms-aggregation-with-a-script-not-working/109154>\
**Category:** Elasticsearch\
**Created:** [November 27, 2017, 12:53am UTC](https://discuss.elastic.co/t/terms-aggregation-with-a-script-not-working/109154 "2017-11-27T00:53:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PRussell](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@PRussell](https://discuss.elastic.co/u/PRussell)\
**Post date:** [November 27, 2017, 12:53am UTC](https://discuss.elastic.co/t/terms-aggregation-with-a-script-not-working/109154/1 "2017-11-27T00:53:25Z")

</div>

In the ES doc under Terms Aggregations, it gives the following on using a script to generate terms for Aggregations:

Generating the terms using a script:

GET /\_search  
{  
"aggs" : {  
"genres" : {  
"terms" : {  
"script" : {  
"source": "doc['genre'].value",  
"lang": "painless"  
}  
}  
}  
}  
}

This is not working for me. In the Kibana Console, I see the results:

{  
"error": {  
"root\_cause": [  
{  
"type": "parsing\_exception",  
"reason": "[terms] failed to parse field [script]",  
"line": 6,  
"col": 21  
}  
],  
"type": "parsing\_exception",  
"reason": "[terms] failed to parse field [script]",  
"line": 6,  
"col": 21,  
"caused\_by": {  
"type": "illegal\_argument\_exception",  
"reason": "[script] unknown field [source], parser not found"  
}  
},  
"status": 400  
}

Of course I also adapted this query to my own specific index, type, and fields, with the same result.

I really do need this to work because I have 2 fields that need to be combined to generate the buckets I need.

I am using ES 5.3, since that is in production, but we could change if this is something fixed in a later version.

Some help would be most appreciated. Hopefully it is just a small issue or mistake on my part. I have worked with scripts in aggregations in the same production environment without problems, but haven't before tried to do this specifically.

Thank you!

Patrick

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 27, 2017, 3:29am UTC](https://discuss.elastic.co/t/terms-aggregation-with-a-script-not-working/109154/2 "2017-11-27T03:29:49Z")

</div>

This is the doc for 5.3: [https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script)

Use `inline` for this version. Or upgrade to 5.6

---

<div class="post-metadata">

**Author:** ![PRussell](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@PRussell](https://discuss.elastic.co/u/PRussell)\
**Post date:** [November 27, 2017, 2:30pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-a-script-not-working/109154/3 "2017-11-27T14:30:14Z")

</div>

Initially I thought I had set the doc version to 5.3 and tried that and got the same error. I then tried 6.0 and Master, which I thought were identical. My error.

Thanks so much, David. You saved me a lot of frustration!

---

<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 25, 2017, 2:30pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-a-script-not-working/109154/4 "2017-12-25T14:30:43Z")

</div>

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