0

An Elastic document has a structure like this:

...
"somefield": "somevalue",
"orders": [{
        "version": 1,
        "statusCode": 1
    }, {
        "version": 1,
        "statusCode": 1
    }, {
        "version": 2,
        "statusCode": 2
    }, {
        "version": 3,
        "statusCode": 5
    }, {
        "version": 3,
        "statusCode": 6
    }
]
...

"orders" is a nested list. I use inline sctipting to calculate the size of the list like this:

"params._source.orders.size() < 4"

I have to add a condition to calculate the number of orders whose statusCode is not 5 and not 6. Linq would look like this:

...orders.Where(o=>!(new[] {5, 6}).Contains(o.statusCode)).size()...

How to write the script in Painless or Groovy?

This results an exeption:

params._source.orders.count { it -> it.statusCode == 1 } < 4
FireShock
  • 1,082
  • 1
  • 15
  • 25

1 Answers1

0

I have found one of the solutions in Painless:

def count = 0;
for (item in params._source.orders){
if (item.statusCode != 5 && item.statusCode != 6) {
count++; }}
return count < 4;
FireShock
  • 1,082
  • 1
  • 15
  • 25