# Loop through "source": "\['message': ctx.payload.hits.hits.0.\_source \]",

**URL:** <https://discuss.elastic.co/t/loop-through-source-message-ctx-payload-hits-hits-0-source/285317>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 28, 2021, 7:02am UTC](https://discuss.elastic.co/t/loop-through-source-message-ctx-payload-hits-hits-0-source/285317 "2021-09-28T07:02:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilshanviso](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dilshanviso](https://discuss.elastic.co/u/dilshanviso)\
**Post date:** [September 28, 2021, 7:02am UTC](https://discuss.elastic.co/t/loop-through-source-message-ctx-payload-hits-hits-0-source/285317/1 "2021-09-28T07:02:00Z")

</div>

Hi,

Attached herewith is my watcher script to ingest all hits into an index and email action every hit.  
Currently I am able to do that to only for the first result/first hit using the following piece of code  
""transform": {  
"script": {  
**"source": "['message': ctx.payload.hits.hits.0.\_source]",**  
"lang": "painless"  
}  
}"  
But what i want is to perform the action on all hits instead of just the first.

Following in my watcher script:

```auto
{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "index-*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "message": "match1"
                  }
                },
                {
                  "match": {
                    "message": "match2"
                  }
                }
              ],
              "filter": {
                "range": {
                  "@timestamp": {
                    "from": "now-600m",
                    "to": "now"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gt": 0
      }
    }
  },
  "actions": {
    "index_payload": {
      "transform": {
        "script": {
          **"source": "['message': ctx.payload.hits.hits.0._source]",**
          "lang": "painless"
        }
      },
      "index": {
        "index": "access_testing_index",
        "doc_type": "_doc",
        "execution_time_field": "timestamp"
      }
    },
    "email_admin": {
      "email": {
        "profile": "standard",
        "from": "blahhh",
        "priority": "high",
        "to": [
          "blahhhhh"
        ],
        "subject": "Siren Alert ALARM {{ payload._id }}",
        "body": {
          "text": "Series Alarm {{ payload._id}}: {{ payload.myvar }}"
        }
      }
    }
  }
}

```

---

<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:** [October 26, 2021, 7:02am UTC](https://discuss.elastic.co/t/loop-through-source-message-ctx-payload-hits-hits-0-source/285317/2 "2021-10-26T07:02:30Z")

</div>

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