Possible to combine/flatten arrays using Painless?


(Loren Siebert) #1

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


(Loren Siebert) #2

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?


(Ryan Ernst) #3

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


(Loren Siebert) #4

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!


(system) #5

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