Sorting in painless

Hi,

I had the following line in a groovy script that I'm attempting to rewrite into painless -

def buckets = ctx.payload.aggregations.metrics.buckets.sort(a,b -> a.largest_surprise.value == b.largest_surprise.value ? 0 : a.largest_surprise.value < b.largest_surprise.value ? -1 : 1);

The compile error I'm getting is "Variable [a] is not defined."

It looks like painless supports the Collections.sort method in java so I tried -

Collections.sort(ctx.payload.aggregations.metrics.buckets, a,b -> a.largest_surprise.value == b.largest_surprise.value ? 0 : a.largest_surprise.value < b.largest_surprise.value ? -1 : 1);

But that also has a compile error - "Unknown call [sort] with [3] arguments on type [Collections]."

Is it possible to sort in painless with a lambda? If so, then what is the syntax? Also, are there any better references for painless than https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference.html?

Thanks,
Nick

Found a page describing lambdas in painless - https://www.elastic.co/guide/en/elasticsearch/reference/5.4/modules-scripting-painless-syntax.html#painless-lambda-expressions.

I think I was missing enclosing parantheses around "a,b" as

def buckets = ctx.payload.aggregations.metrics.buckets.sort((a,b) -> a.largest_surprise.value == b.largest_surprise.value ? 0 : a.largest_surprise.value < b.largest_surprise.value ? -1 : 1);

compiles.

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