Painless script break out of inner for loop using labels

I would like to have a watch with script condition which returns true based on condition in my inner most for loop.
I have logic created with labeled for loop, as it is part of the Java control flow mentioned in doc here

def status = false;
top_loop:for (item_a in ctx.payload.aggregations.by_A.buckets) {
   def a = item_a.key;
   for (item_b in item_a.by_B.buckets) {
      def b = item_b.key;
      for (item_c in item_b.by_C.buckets) {
         def c = item_c.key;
         def alert_count = item_c.D.value;
         if (alert_count > 1)  {
            status = true;
            break top_loop;
         }
      }
   }
}
return status;

This script is returning compilation errors:

"type" : "script_exception",
  "reason" : "compile error",
  "script_stack" : [
    "...\ntop_loop:for (item_a in ctx. ...",
    "            ^---- HERE"
  ],
"lang" : "painless",
  "caused_by" : {
    "type" : "illegal_argument_exception",
    "reason" : "unexpected token [':'] was expecting one of [{<EOF>, ';'}]."
  }

Need suggestion to resolve this.
Thanks

Update: Using ES, x-pack 5.4

painless does not support labels.

An alternative here would be to use the anyMatch with a predicate in streams, something like this

return ctx.payload.aggregations.foo.buckets.stream().anyMatch(i -> { return i.bar.buckets.stream().anyMatch(x -> { return x.doc_count > 0 })})

--Alex

1 Like

Thanks, I will check this out. Much appreciated

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