Invalid sequence of tokens near

Hi All,

I am working on a watcher where I am getting the following error:

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_222]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_222]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
Caused by: java.lang.IllegalArgumentException: invalid sequence of tokens near ['i'].
	at org.elasticsearch.painless.PainlessScript$Script.compile(boolean trigger = false; def prov_list = new Map(); for (var i = 0; i < ctx.payload.hits.hits.size(); i++) { for (var r = 0; r < ctx.payload.hits.hits[i].recipient_count; r++) { var userdata = ctx.payload.hits.hits[i].recipients[r].split('@'); if (prov_lis ...:62) ~[?:?]
	at org.elasticsearch.painless.antlr.ParserErrorStrategy.recover(ParserErrorStrategy.java:61) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.rstatement(PainlessParser.java:831) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.statement(PainlessParser.java:364) ~[?:?]

The code snippet I am trying to implement is as follows:

 "condition": {
    "script": {
      "source": "boolean trigger = false;def prov_list = new Map();for (var i = 0; i < ctx.payload.hits.hits.size(); i++){for (var r = 0; r < ctx.payload.hits.hits[i].recipient_count; r++){var userdata = ctx.payload.hits.hits[i].recipients[r].split('@');if(prov_list.has(userdata[0])){var counter = prov_list.get(userdata[1]);counter = counter++;prov_list.set(userdata[1], counter);break;}prov_list.set(userdata[1], 1);}}if (prov_list.size >= 2) {var iterator = prov_list.entries();for (var i = 0; i < prov_list.size; i++){if (iterator1.next().value.match('hotmail')){trigger = true;}}}return trigger;",
      "lang": "painless",
      "params": {
        "domain": "hotmail"
      }
    }
  }

Could someone please help me?

Thanks,
Paul.

can you share the full stack response when running the execute watch API? There should be an indication in the stack trace where this refers to.

Hi,

Thanks for your response, there is the full stack trace.

[2019-09-19T12:13:30,295][WARN ][r.suppressed             ] [tb-clog-esc1.tb.iss.local] path: /_xpack/watcher/watch/_execute, params: {}
org.elasticsearch.script.ScriptException: compile error
	at org.elasticsearch.painless.PainlessScriptEngine.convertToScriptException(PainlessScriptEngine.java:507) ~[?:?]
	at org.elasticsearch.painless.PainlessScriptEngine.compile(PainlessScriptEngine.java:427) ~[?:?]
	at org.elasticsearch.painless.PainlessScriptEngine.compile(PainlessScriptEngine.java:167) ~[?:?]
	at org.elasticsearch.script.ScriptService.compile(ScriptService.java:363) ~[elasticsearch-6.8.0.jar:6.8.0]
	at org.elasticsearch.xpack.watcher.condition.ScriptCondition.<init>(ScriptCondition.java:36) ~[?:?]
	at org.elasticsearch.xpack.watcher.condition.ScriptCondition.parse(ScriptCondition.java:46) ~[?:?]
	at org.elasticsearch.xpack.watcher.Watcher.lambda$createComponents$2(Watcher.java:327) ~[?:?]
	at org.elasticsearch.xpack.core.watcher.condition.ConditionRegistry.parseExecutable(ConditionRegistry.java:56) ~[?:?]
	at org.elasticsearch.xpack.watcher.watch.WatchParser.parse(WatchParser.java:151) ~[?:?]
	at org.elasticsearch.xpack.watcher.watch.WatchParser.parse(WatchParser.java:120) ~[?:?]
	at org.elasticsearch.xpack.watcher.watch.WatchParser.parse(WatchParser.java:76) ~[?:?]
	at org.elasticsearch.xpack.watcher.transport.actions.execute.TransportExecuteWatchAction.masterOperation(TransportExecuteWatchAction.java:107) ~[?:?]
	at org.elasticsearch.xpack.watcher.transport.actions.execute.TransportExecuteWatchAction.masterOperation(TransportExecuteWatchAction.java:63) ~[?:?]
	at org.elasticsearch.action.support.master.TransportMasterNodeAction.masterOperation(TransportMasterNodeAction.java:124) ~[elasticsearch-6.8.0.jar:6.8.0]
	at org.elasticsearch.action.support.master.TransportMasterNodeAction$AsyncSingleAction$2.doRun(TransportMasterNodeAction.java:211) ~[elasticsearch-6.8.0.jar:6.8.0]
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:751) [elasticsearch-6.8.0.jar:6.8.0]
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.8.0.jar:6.8.0]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_222]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_222]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
Caused by: java.lang.IllegalArgumentException: invalid sequence of tokens near ['i'].
	at org.elasticsearch.painless.PainlessScript$Script.compile(boolean trigger = false; def prov_list = new Map(); for (var i = 0; i < ctx.payload.hits.hits.size(); i++) { for (var r = 0; r < ctx.payload.hits.hits[i].recipient_count; r++) { var userdata = ctx.payload.hits.hits[i].recipients[r].split('@'); if (prov_lis ...:62) ~[?:?]
	at org.elasticsearch.painless.antlr.ParserErrorStrategy.recover(ParserErrorStrategy.java:61) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.rstatement(PainlessParser.java:831) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.statement(PainlessParser.java:364) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.source(PainlessParser.java:178) ~[?:?]
	at org.elasticsearch.painless.antlr.Walker.buildAntlrTree(Walker.java:222) ~[?:?]
	at org.elasticsearch.painless.antlr.Walker.<init>(Walker.java:204) ~[?:?]
	at org.elasticsearch.painless.antlr.Walker.buildPainlessTree(Walker.java:179) ~[?:?]
	at org.elasticsearch.painless.Compiler.compile(Compiler.java:211) ~[?:?]
	at org.elasticsearch.painless.PainlessScriptEngine$5.run(PainlessScriptEngine.java:420) ~[?:?]
	at org.elasticsearch.painless.PainlessScriptEngine$5.run(PainlessScriptEngine.java:416) ~[?:?]
	at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_222]
	at org.elasticsearch.painless.PainlessScriptEngine.compile(PainlessScriptEngine.java:416) ~[?:?]
	... 18 more
Caused by: org.antlr.v4.runtime.NoViableAltException
	at org.antlr.v4.runtime.atn.ParserATNSimulator.noViableAlt(ParserATNSimulator.java:1884) ~[?:?]
	at org.antlr.v4.runtime.atn.ParserATNSimulator.execATN(ParserATNSimulator.java:486) ~[?:?]
	at org.antlr.v4.runtime.atn.ParserATNSimulator.adaptivePredict(ParserATNSimulator.java:412) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.rstatement(PainlessParser.java:570) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.statement(PainlessParser.java:364) ~[?:?]
	at org.elasticsearch.painless.antlr.PainlessParser.source(PainlessParser.java:178) ~[?:?]
	at org.elasticsearch.painless.antlr.Walker.buildAntlrTree(Walker.java:222) ~[?:?]
	at org.elasticsearch.painless.antlr.Walker.<init>(Walker.java:204) ~[?:?]
	at org.elasticsearch.painless.antlr.Walker.buildPainlessTree(Walker.java:179) ~[?:?]
	at org.elasticsearch.painless.Compiler.compile(Compiler.java:211) ~[?:?]
	at org.elasticsearch.painless.PainlessScriptEngine$5.run(PainlessScriptEngine.java:420) ~[?:?]
	at org.elasticsearch.painless.PainlessScriptEngine$5.run(PainlessScriptEngine.java:416) ~[?:?]
	at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_222]
	at org.elasticsearch.painless.PainlessScriptEngine.compile(PainlessScriptEngine.java:416) ~[?:?]
	... 18 more
[2019-09-19T12:44:59,519][INFO ][o.e.m.j.JvmGcMonitorService] [tb-clog-esc1.tb.iss.local] [gc][1221552] overhead, spent [417ms] collecting in the last [1s]

For completeness, [here is the watcher.json as well].(https://gist.github.com/pjanzen/59c3acee10a181202089fd0b60a7abe5)

try replacing all your var with def statements. Also, new Map will not work (it's an interface in java), just do foo = [:] to create an empty map.

One last trick, running this using the execute watch API in the console, should show you exact error messages, which part of the script is having a problem, down to the exact character.

--Alex

1 Like

Thanks, how do I run it in the console, just copy & paste and run?

Use the Execute Watch API

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