Painless terms script aggregations

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?

.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:

return Stream.concat(doc['field1'], doc['field2']", "));

Hello, rjernst, and thanks for your answer.

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


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