正在加载

对数组中的每个元素运行操作

Elastic Stack Serverless

您可以使用操作中的 foreach 字段来为该数组中的每个元素触发配置的操作。

为了防止长时间运行的监视,您可以使用 max_iterations 字段来限制每个监视执行的最大运行次数。如果达到此限制,执行将正常停止。如果未设置,则此字段默认为一百。

 PUT _watcher/watch/log_event_watch {
  "trigger" : {
    "schedule" : { "interval" : "5m" }
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : "log-events",
        "body" : {
          "query" : { "match" : { "status" : "error" } }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 } }
  },
  "actions" : {
    "log_hits" : {
      "foreach" : "ctx.payload.hits.hits",
      "max_iterations" : 500,
      "logging" : {
        "text" : "Found id {{ctx.payload._id}} with field {{ctx.payload._source.my_field}}"
      }
    }
  }
}
  1. 将为每个返回的搜索命中执行日志记录语句。
© . All rights reserved.