【问题标题】:Problems accessing _source fields with a dot in the name when creating Slack action for Elasticsearch Watcher为 Elasticsearch Watcher 创建 Slack 操作时,访问名称中带有点的 _source 字段时出现问题
【发布时间】:2018-04-28 01:35:27
【问题描述】:

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

【问题讨论】:

    标签: elasticsearch elasticsearch-watcher


    【解决方案1】:

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

    更新:

    我能够通过使用转换来构建一个新对象来完成这项工作。 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}}"
        }
    }
    

    旧答案:

    所以根据thisWatcher 使用小胡子。

    根据this mustache 无法访问名称中带有点的字段。

    【讨论】:

      猜你喜欢
      • 2020-12-27
      • 2019-09-27
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      相关资源
      最近更新 更多