# Compile Error | Watcher Condition

**URL:** https://discuss.elastic.co/t/compile-error-watcher-condition/195897
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [August 20, 2019, 11:05am UTC](https://discuss.elastic.co/t/compile-error-watcher-condition/195897 "2019-08-20T11:05:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ntran](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@ntran](https://discuss.elastic.co/u/ntran)
#### Post date: [August 20, 2019, 11:05am UTC](https://discuss.elastic.co/t/compile-error-watcher-condition/195897/1 "2019-08-20T11:05:04Z")

</div>

Hi,

I am trying to create a loop where all the arrays are searched to find any have more than one value for `"doc_count"`. Once the loop finishes, and all the fields are identified, I wish to put these in a separate array to use this for my action.

When I execute the condition, I get a compile error. Can you please help me?

// Executed Output

> <https://gist.github.com/nhtrn/e2e30e8ed74966e4512a3b8fc75ce317>

// Console thus far

> <https://gist.github.com/nhtrn/f3c22e4462c85bb483a1f3bcc42712da>

Thank you in advance 🙂

---

<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: [August 20, 2019, 11:29am UTC](https://discuss.elastic.co/t/compile-error-watcher-condition/195897/2 "2019-08-20T11:29:03Z")

</div>

hey,

`new` is a reserved keyword in painless. Use something like `new_list` and the script should compile at that stage.

--Alex

---

<div class="post-metadata">

### Author: ![ntran](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@ntran](https://discuss.elastic.co/u/ntran)
#### Post date: [August 20, 2019, 11:33am UTC](https://discuss.elastic.co/t/compile-error-watcher-condition/195897/3 "2019-08-20T11:33:10Z")

</div>

Thanks for the quick response

Unfortunately, I am still getting a `compile error`

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "... ransform.add(document) } ; return cctx.payload.agg ...",
          " ^---- HERE"
        ],
        "script": "ctx.payload.aggregations.managedobjectref_agg.transform = []; def new_list = []; for (int j=0;j<ctx.payload.aggregations.managedobjectref_agg.hits.hits;j++){ new_list = ['bob': ctx.payload.hits.hits[j].buckets.timerange_agg.buckets.0.doc_count >= 1]; ctx.payload.aggregations.managedobjectref_agg.transform.add(document) } ; return cctx.payload.aggregations.managedobjectref_agg.transform",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      "... ransform.add(document) } ; return cctx.payload.agg ...",
      " ^---- HERE"
    ],
    "script": "ctx.payload.aggregations.managedobjectref_agg.transform = []; def new_list = []; for (int j=0;j<ctx.payload.aggregations.managedobjectref_agg.hits.hits;j++){ new_list = ['bob': ctx.payload.hits.hits[j].buckets.timerange_agg.buckets.0.doc_count >= 1]; ctx.payload.aggregations.managedobjectref_agg.transform.add(document) } ; return cctx.payload.aggregations.managedobjectref_agg.transform",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "unexpected token [';'] was expecting one of [<EOF>]."
    }
  },
  "status": 400
}

```

The error is after`.add(document)`. I'm assuming this means my loop did not go into a separate document  
I would appreciate your insight on this

---

<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: [August 20, 2019, 11:38am UTC](https://discuss.elastic.co/t/compile-error-watcher-condition/195897/4 "2019-08-20T11:38:57Z")

</div>

please take the time to properly format your scripts instead of having them inside of the JSON, take this example

```auto
ctx.payload.aggregations.managedobjectref_agg.transform = []; 
def new_list = []; 
for (int j=0;j<ctx.payload.aggregations.managedobjectref_agg.hits.hits;j++){ 
  new_list = ['bob': ctx.payload.hits.hits[j].buckets.timerange_agg.buckets.0.doc_count >= 1]; 
  ctx.payload.aggregations.managedobjectref_agg.transform.add(document) 
} ; 
return cctx.payload.aggregations.managedobjectref_agg.transform",

```

Few things:

- typo at `cctx`.
- mustache style index usage `buckets.0.doc_count`, that must be `buckets[0].doc_count`.
- `new_list` is used as a map but initialize it as a list.
- missing semicolon at the last line of the for loop.

debugging tip: When using kibana console, instead of using `"script" : "your...script"` use

```auto
"script": """
def foo...
return ...;
"""

```

those triple ticks allow you to write scripts in multiple lines, make them more readable and the console is doing all the escaping and using single ticks when sending to ES.

---

<div class="post-metadata">

### Author: ![ntran](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@ntran](https://discuss.elastic.co/u/ntran)
#### Post date: [August 20, 2019, 1:15pm UTC](https://discuss.elastic.co/t/compile-error-watcher-condition/195897/5 "2019-08-20T13:15:48Z")

</div>

Thanks as always for your help @spinscale

---

<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: [September 17, 2019, 1:27pm UTC](https://discuss.elastic.co/t/compile-error-watcher-condition/195897/6 "2019-09-17T13:27:21Z")

</div>

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