【问题标题】:Cannot Transform Correctly with Elasticsearch Watcher {{ctx.payload.hits.hits}}无法使用 Elasticsearch Watcher {{ctx.payload.hits.hits}} 正确转换
【发布时间】:2016-09-23 07:28:36
【问题描述】:

我有一个watcher配置如下:

{
  "trigger": {
    "schedule": {
      "interval": "5s"
    }
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : [ "my_index" ],
        "types" : [ "my_type" ],
        "body" : {
          "query" : {
            "match_all" : {}
           }
        }
      }
    }
  },
  "transform" : {
      "script" : "return [ body: groovy.json.JsonOutput.toJson(ctx.payload.hits.hits)]"
  },
  "actions" : {
    "hbase_webhook" : {
        "webhook" : {
            "method" : "POST",
            "host" : "<some_ip>",
            "port" : <some_port>,
            "path": "/v0.1/_events",
            "body" : "data: {{ctx.payload.body}}"
        }
    }
  }
}

正文中发布的数据不是有效的 JSON:类似于:

{ 'data: ': { '{"_index":"my_index","_type":"my_type","_source":{"key":"val"}},"_id":"<some_id>","_score":1.0}': '' } }

我不知道如何解析这个输出,因为 Node.js 中的 JSON.parse 无论如何都不会正确解析它。

【问题讨论】:

  • 我并不是说它不起作用。当我用 {{ctx.payload.body}} 原样返回它时(没有数据:部分,它仍然无法解析。
  • 当我用 {{ctx.payload.body}} 原样返回它时(没有数据:部分,它仍然无法解析。你得到什么解析错误?
  • @AndreiStefan 对不起,我的错。事实上,我得到了一个奇怪的 JSON,比如{ '{"_index":"&lt;some_index&gt;","_type":"&lt;some_type&gt;","_source":{"&lt;some_key&gt;":"&lt;some_value&gt;", ...},"_id":"&lt;doc_id&gt;","_score":1.0}': '' },其中数据是键,值是''。如果没有匹配的文档,则为{ '0': '' }。有什么想法吗?我想我已经接近了:)

标签: json elasticsearch elasticsearch-plugin elasticsearch-watcher


【解决方案1】:

从来没有。忘记。标题。

我忘记了:

"headers" {
    "Content-type": "application/json"
}

所以任何工具都无法解析。

【讨论】:

    【解决方案2】:

    在为端点创建警报时遇到此问题,我们只想发送符合特定条件的实际记录。请参阅下面的示例:

    "actions": {
        "my_webhook": {
          "webhook": {
            "scheme": "https",
            "host": "webhook.site",
            "port": 443,
            "method": "post",
            "path": "/webhooksiteguidwouldbehere",
            "params": {},
            "headers": {
              "Content-type": "application/json"
            },
            "body": "{{#toJson}}ctx.payload.hits.hits{{/toJson}}"
          }
        }
      }
    

    第二个注意事项: 如果主体大小设置为 0,您的点击数将返回为 null。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 2023-02-07
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多