# Painless terms script aggregations

**URL:** <https://discuss.elastic.co/t/painless-terms-script-aggregations/103686>\
**Category:** Elasticsearch\
**Created:** [October 12, 2017, 9:19am UTC](https://discuss.elastic.co/t/painless-terms-script-aggregations/103686 "2017-10-12T09:19:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knights](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@knights](https://discuss.elastic.co/u/knights)\
**Post date:** [October 12, 2017, 9:19am UTC](https://discuss.elastic.co/t/painless-terms-script-aggregations/103686/1 "2017-10-12T09:19:10Z")

</div>

Hello, everyone.

I have a problem regarding a scripted terms aggregation that no longer works since ES 5.X.

I need a terms aggregation on two multivalued fields.

In groovy, I used to do this :

> doc['field1'].values + doc['field2'].values

But in Painless, when executing that script, I get

> Cannot apply[+]operation to types[org.elasticsearch.index.fielddata.ScriptDocValues.Strings]and[org.elasticsearch.index.fielddata.ScriptDocValues.Strings]

This is pretty self explanatory. The problem is that I cannot find anywhere in the doc how I sould proceed.  
Concerning that use case, the documentation simply state to "use a script to get the infos from several fields" but do not go on to explain how.

How can it be done?  
Thanks.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 12, 2017, 7:17pm UTC](https://discuss.elastic.co/t/painless-terms-script-aggregations/103686/2 "2017-10-12T19:17:13Z")

</div>

`.values` gives you a list of values. You need to decide what you want to do with those values (I'm not sure what groovy may have done before, but my guess is merging the lists, then doing a toString later). My guess is you just want them concatenated together, so probably something like this:

```auto
return Stream.concat(doc['field1'].values.stream(), doc['field2'].values.stream()).collect(Collectors.joining(", "));

```

---

<div class="post-metadata">

**Author:** ![knights](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@knights](https://discuss.elastic.co/u/knights)\
**Post date:** [October 13, 2017, 9:11am UTC](https://discuss.elastic.co/t/painless-terms-script-aggregations/103686/3 "2017-10-13T09:11:52Z")

</div>

Hello, rjernst, and thanks for your answer.

I indeed used a similar technique, merging the two list in another with addAll.

Thanks.

---

<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:** [November 10, 2017, 9:11am UTC](https://discuss.elastic.co/t/painless-terms-script-aggregations/103686/4 "2017-11-10T09:11:52Z")

</div>

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