Possible to combine/flatten arrays using Painless?

Is it possible to flatten an array using Painless? I'm essentially looking for this Groovy flatten method in Painless.

Here's what I'm doing in Groovy in 5.6:

      {
        "script": {
          "lang": "groovy",
          "source": "ctx.foo=ctx.foo.flatten().unique().sort()"
        }
      }

It works, but I'm admonished: #! Deprecation: [groovy] scripts are deprecated, use [painless] scripts instead.
Well, how can I?

You should look at java streams (which are exposed in painless), and specifically flatMap().

1 Like

Thank you Ryan! I wasn't aware of how much Java was exposed via Painless.

      {
        "script": {
          "lang": "painless",
          "source": "ctx.foo=ctx.foo.stream().flatMap(l -> l.stream()).distinct().sorted().collect(Collectors.toList())"
        }
      }

That was Painless!

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