2

我正在尝试使用动态附件创建 Slack 操作。我的 _source 看起来像这样:

{
    "user.url": "https://api.github.com/users/...",
    "user.gists_url": "https://api.github.com/users/.../gists{/gist_id}",
    "user.repos_url": "https://api.github.com/users/.../repos",
    "date": "2018-04-27T14:34:10Z",
    "user.followers_url": "https://api.github.com/users/.../followers",
    "user.following_url": "https://api.github.com/users/.../following{/other_user}",
    "user.id": 123456,
    "user.avatar_url": "https://avatars0.githubusercontent.com/u/123456?v=4",
    "user.events_url": "https://api.github.com/users/.../events{/privacy}",
    "user.site_admin": false,
    "user.html_url": "https://github.com/...",
    "user.starred_url": "https://api.github.com/users/.../starred{/owner}{/repo}",
    "user.received_events_url": "https://api.github.com/users/.../received_events",
    "metric": "stars",
    "user.login": "...",
    "user.type": "User",
    "user.subscriptions_url": "https://api.github.com/users/.../subscriptions",
    "user.organizations_url": "https://api.github.com/users/.../orgs",
    "user.gravatar_id": ""
}

这是我的 Slack 动作

"actions": {
    "notify-slack": {
        "throttle_period_in_millis": 240000,
        "slack": {
            "account": "monitoring",
            "message": {
                "from": "Elasticsearch Watcher",
                "to": [
                    "#watcher"
                ],
            "text": "We have {{ctx.payload.new.hits.total}} new stars! And {{ctx.payload.old.hits.total}} in total.",
            "dynamic_attachments" : {
                "list_path" : "ctx.payload.new.hits.hits",
                "attachment_template" : {
                    "title" : "{{_source.[\"user.login\"]}}", 
                    "text" : "Users Count: {{count}}",
                    "color" : "{{color}}"
                }
            }
        }
    }
}

我似乎无法弄清楚如何访问我的 _source 字段,因为它们中有点。我试过了:

  • "{{_source.[\"user.login\"]}}"
  • "{{_source.user.login}}"
  • "{{_source.[user.login]}}"
  • "{{_source.['user.login']}}"
4

1 回答 1

3

我的问题的答案是,您不能_source直接使用 mustache 访问带有点的键,您必须首先转换数据。

更新:

我能够通过使用转换来构建一个新对象来完成这项工作。Mustache 可能无法访问名称中带有点的字段,但 painless 可以!我将此转换添加到我的 slack 对象中:

"transform" : {
    "script" : {
        "source" : "['items': ctx.payload.new.hits.hits.collect(user -> ['userName': user._source['user.login']])]",
        "lang" : "painless"
    }
}

现在在松弛动作动态附件中,我可以访问items数组:

"dynamic_attachments" : {
    "list_path" : "ctx.payload.items",
    "attachment_template" : {
        "title" : "{{userName}}", 
        "text" : "{{_source}}"
    }
}

老答案:

所以根据这个观察者使用胡子。

并且根据这个小胡子不能访问名称中带有点的字段。

于 2018-04-28T01:37:24.627 回答