# Compile Errors - Using Painless

**URL:** https://discuss.elastic.co/t/compile-errors-using-painless/279265
**Category:** Elasticsearch
**Tags:** painless
**Created:** [July 21, 2021, 12:56pm UTC](https://discuss.elastic.co/t/compile-errors-using-painless/279265 "2021-07-21T12:56:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dshonubi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dshonubi/32/91924_2.png) [@dshonubi](https://discuss.elastic.co/u/dshonubi)
#### Post date: [July 21, 2021, 12:56pm UTC](https://discuss.elastic.co/t/compile-errors-using-painless/279265/1 "2021-07-21T12:56:00Z")

</div>

I keep getting this error of a script exception, but I'm not sure what the root cause is.

[script\_exception] compile error, with { script\_stack={ 0="... tx.payload.aggregations.4XXerror.buckets.size(); i ..." & 1=" ^---- HERE" } & script="if (ctx.payload.hits.total \< 1) return false; for (int i=0; i \< ctx.payload.aggregations.4XXerror.buckets.size(); i++) { if (ctx.payload.aggregations.4XXerror.buckets[i].doc\_count \> 10) { return true; }}" & lang="painless" & position={ offset=90 & start=65 & end=115 } }

Any suggestions?

```auto
"condition": {
    "script": {
      "source": "if (ctx.payload.hits.total < 1) return false; for (int i=0; i < ctx.payload.aggregations.4XXerror.buckets.size(); i++) { if (ctx.payload.aggregations.4XXerror.buckets[i].doc_count > 10) { return true; }}",
      "lang": "painless"
    }
  }

```

---

<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: [July 21, 2021, 2:00pm UTC](https://discuss.elastic.co/t/compile-errors-using-painless/279265/2 "2021-07-21T14:00:48Z")

</div>

I suppose you need to check for the existence of the `buckets` array, as it might be empty - this is just a guess however.

First, please always format your exceptions as well, makes it much more readbale.

Second, if you add `min_doc_count: 10` to your aggregation, the bucket will only be returned if it contains more than ten documents. With this change in your query in place, you just need to check if the size of the `buckets` is greater than 0.

If it still does not work, please share the **full** output of the execute watch API.

Thanks!

---

<div class="post-metadata">

### Author: ![dshonubi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dshonubi/32/91924_2.png) [@dshonubi](https://discuss.elastic.co/u/dshonubi)
#### Post date: [July 21, 2021, 3:03pm UTC](https://discuss.elastic.co/t/compile-errors-using-painless/279265/3 "2021-07-21T15:03:18Z")

</div>

Thanks for the help.

Turns out the issue was naming.  
You cannot use numbers to name the fields you're calling as it causes an error later on.  
In my case: `4XXerror`.  
I renamed it to `errors`, and it works perfectly fine.

---

<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: [August 18, 2021, 3:03pm UTC](https://discuss.elastic.co/t/compile-errors-using-painless/279265/4 "2021-08-18T15:03:28Z")

</div>

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