# Painless script break out of inner for loop using labels

**URL:** https://discuss.elastic.co/t/painless-script-break-out-of-inner-for-loop-using-labels/104421
**Category:** Elasticsearch
**Created:** [October 18, 2017, 3:06pm UTC](https://discuss.elastic.co/t/painless-script-break-out-of-inner-for-loop-using-labels/104421 "2017-10-18T15:06:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![krishna\_chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krishna\_chaitanya](https://discuss.elastic.co/u/krishna_chaitanya)
#### Post date: [October 18, 2017, 3:06pm UTC](https://discuss.elastic.co/t/painless-script-break-out-of-inner-for-loop-using-labels/104421/1 "2017-10-18T15:06:41Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-syntax.html)

```
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

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [October 18, 2017, 3:53pm UTC](https://discuss.elastic.co/t/painless-script-break-out-of-inner-for-loop-using-labels/104421/2 "2017-10-18T15:53:38Z")

</div>

painless does not support labels.

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

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

```

--Alex

---

<div class="post-metadata">

### Author: ![krishna\_chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krishna\_chaitanya](https://discuss.elastic.co/u/krishna_chaitanya)
#### Post date: [October 18, 2017, 7:06pm UTC](https://discuss.elastic.co/t/painless-script-break-out-of-inner-for-loop-using-labels/104421/3 "2017-10-18T19:06:30Z")

</div>

Thanks, I will check this out. Much appreciated

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 15, 2017, 7:10pm UTC](https://discuss.elastic.co/t/painless-script-break-out-of-inner-for-loop-using-labels/104421/4 "2017-11-15T19:10:02Z")

</div>

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