Issue migrating watchers to latest

I am trying to migrate my watcher to the latest elastic search Xpack

Here is my condition script

"condition": {
"script": {
"inline": "def sf = new SimpleDateFormat('yyyy-MM-dd');ctx.vars.from = sf.parse(new Date());if (ctx.payload.aggregations.minutes20.buckets.size() == 0) return false;def latest = ctx.payload.aggregations.minutes20.buckets[-1]; return latest.occurences.buckets.any{it.doc_count > 0};",
"lang": "painless"
}

},

i am having issues compiling the watcher. below are the error details

{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... st.occurences.buckets.Any{it.doc_count > 0};",
" ^---- HERE"
],
"script": "def sf = new SimpleDateFormat('yyyy-MM-dd');ctx.vars.from = sf.parse(new Date());if (ctx.payload.aggregations.minutes20.buckets.size() == 0) return false;def latest = ctx.payload.aggregations.minutes20.buckets[-1]; return latest.occurences.buckets.Any{it.doc_count > 0};",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... st.occurences.buckets.Any{it.doc_count > 0};",
" ^---- HERE"
],
"script": "def sf = new SimpleDateFormat('yyyy-MM-dd');ctx.vars.from = sf.parse(new Date());if (ctx.payload.aggregations.minutes20.buckets.size() == 0) return false;def latest = ctx.payload.aggregations.minutes20.buckets[-1]; return latest.occurences.buckets.Any{it.doc_count > 0};",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "unexpected token ['{'] was expecting one of [{, ';'}]."
}
},
"status": 500
}

you need to either install the old groovy plugin or convert your scripts to the new painless language. If you need to access the time the watch was running, you should check out ctx.trigger.triggered_time - as you cannot instantiate a new date object anymore.

you also need to replace groovy closures with java lambdas,

i.e.

ctx.payload.foo.bar.buckets.stream().anyMatch(item -> item.whateverfield > 6)

hope htis helps to get started. There is also the painless documentation https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting-painless.html

How can i install the old groovy plugin int he new env?

Sorry, my bad... groovy is still part of Elasticsearch 5.x, no need to install anything. You need to specify lang: groovy in your script part though.

Word of warning; Groovy as scripting language will be removed in Elasticsearch 6.0

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