在ElasticSearch Watcher Transform Script中无法识别Groovy属性,从而获得正确的JSON

时间:2016-06-08 17:43:59

标签: json elasticsearch groovy

我正在使用:

  • Elasticsearch 2.3
  • 观察
  • Topbeat

目标是创建一个手表,每隔x个时间进行一次查询并检索一些命中,然后将其发布到Web服务器。这很好用。但是,{{ctx.payload.hits.hits}}中的Json响应不是Json,所以我无法处理它。同样的问题似乎出现在某些主题中,这与mine最相似:

所以,这是我的手表(输入正常,问题出在动作的脚本中):

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

错误:

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)

有什么想法让groovy.json在watcher动作脚本中可用吗?或者从ctx.hits.hits返回正确的json的任何其他想法?

1 个答案:

答案 0 :(得分:0)

所以,我在Elasticsearch repo中打开了一个issue。经过一番讨论后,本机toJson函数将在胡子模板引擎中实现,因此它默认呈现json。

Here是拉取请求。

希望在下一个版本中准备就绪。