简体   繁体   中英

Groovy property not recognized in ElasticSearch Watcher Transform Script getting proper JSON

I'm using:

  • Elasticsearch 2.3
  • Watcher
  • Topbeat

The goal is to create a watch that every x amount of time does a query and retrieves some hits, and post it to a web server. This works fine. However, the Json response in {{ctx.payload.hits.hits}} isn't Json, so I can't proccess it. The same issue seems to appear in some threads, this being the most similar to mine :

So, this is my watch (the input works fine, the issue is in the script of the action):

PUT _watcher/watch/running_process_watch
{
  "trigger" : {
      "schedule" : {
        "interval" : "10s"
    }
  },
  "input" : {... },
  "actions" : {
    "ping_webhook": {
        "transform":{
        "script": "return [ body: groovy.json.JsonOutput.toJson(ctx.payload.hits.hits)]"
          },
          "webhook": {
            "method": "POST",
            "host":   "localhost",
            "port":   4567,
            "path":   "/register_data",
            "headers": {
             "Content-Type" : "application/json"
            },
            "body" : "data: {{ctx.payload.body}}"
          }
        }
  }
}

The error:

failed to execute [script] transform for [running_process_watch_0-2016-06-08T17:25:14.162Z]
ScriptException[failed to run inline script [return [ body: groovy.json.JsonOutput.toJson(ctx.payload.hits.hits)]] using lang [groovy]]; nested: MissingPropertyException[No such property: groovy for class: 1605d064acb49c10c464b655dacc9193f4e2e484];
    at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript.run(GroovyScriptEngineService.java:320)
    at org.elasticsearch.watcher.transform.script.ExecutableScriptTransform.doExecute(ExecutableScriptTransform.java:74)
    at org.elasticsearch.watcher.transform.script.ExecutableScriptTransform.execute(ExecutableScriptTransform.java:60)
    at org.elasticsearch.watcher.transform.script.ExecutableScriptTransform.execute(ExecutableScriptTransform.java:41)
    at org.elasticsearch.watcher.actions.ActionWrapper.execute(ActionWrapper.java:94)
    at org.elasticsearch.watcher.execution.ExecutionService.executeInner(ExecutionService.java:388)
    at org.elasticsearch.watcher.execution.ExecutionService.execute(ExecutionService.java:273)
    at org.elasticsearch.watcher.execution.ExecutionService$WatchExecutionTask.run(ExecutionService.java:438)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Any idea to make groovy.json usable inside the watcher action script? Or any other idea to return proper json from the ctx.hits.hits?

So, I opened an issue in Elasticsearch repo. After some discussion, a native toJson function is going to be implemented in the mustache templating engine so it renders json by default.

Here is the pull request.

Hopefully in the next release will be ready.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM