# Iterate watcher hits and perform action on each hit

**URL:** https://discuss.elastic.co/t/iterate-watcher-hits-and-perform-action-on-each-hit/236702
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [June 11, 2020, 12:32pm UTC](https://discuss.elastic.co/t/iterate-watcher-hits-and-perform-action-on-each-hit/236702 "2020-06-11T12:32:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jeroen2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeroen2/32/55694_2.png) [@Jeroen2](https://discuss.elastic.co/u/Jeroen2)
#### Post date: [June 11, 2020, 12:32pm UTC](https://discuss.elastic.co/t/iterate-watcher-hits-and-perform-action-on-each-hit/236702/1 "2020-06-11T12:32:23Z")

</div>

Hi, in the example below I have a condition where I send an alert if the first hit contains the query 'some\_query'. Is it possible to iterate all hits and sent/stack all hits in an action?

```
 "condition": {
    "script": {
      "source": "return ctx.payload.hits.hits.0._source.message.contains('some_query')",
      "lang": "painless"
    }
  },
  "actions": {
    "opsgenie": {
      "webhook": {
        "scheme": "https",
        "host": "api.opsgenie.com",
        "port": 443,
        "method": "post",
        "path": "/v1/json/eswatcher",
        "params": {
          "apiKey": "some key"
        },
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{{#toJson}}ctx.payload.hits.hits.0._source.correlation_id{{/toJson}}"
      }
    }
  }
```

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [June 30, 2020, 8:13am UTC](https://discuss.elastic.co/t/iterate-watcher-hits-and-perform-action-on-each-hit/236702/2 "2020-06-30T08:13:35Z")

</div>

try something like

```auto
{{#ctx.payload.hits.hits}}{{_source.whatever.field}}{{/ctx.payload.hits.hits}}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 28, 2020, 8:13am UTC](https://discuss.elastic.co/t/iterate-watcher-hits-and-perform-action-on-each-hit/236702/3 "2020-07-28T08:13:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
