Compile Errors - Using Painless

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?

"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"
    }
  }

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!

1 Like

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.