在为Elasticsearch Watcher创建Slack操作时,访问带有名称中的点的_source字段时出现问题

时间:2018-04-28 01:35:27

标签: elasticsearch elasticsearch-watcher

我正在尝试使用动态附件创建一个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']}}"

1 个答案:

答案 0 :(得分:0)

  

我的问题的答案是,您无法直接使用小胡子访问带有点的_source个键,您必须先转换数据。

<强>更新

我能够通过使用转换来构建新对象来实现这一目标。胡子可能无法访问名称中带点的字段,但无痛!我将这个变换添加到我的松弛对象中:

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

旧答案:

所以根据this Watcher使用小胡子。

并且根据this胡须无法访问名称中包含点的字段。