# Use watcher index action with multiple documents

**URL:** <https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503>\
**Category:** Kibana\
**Created:** [January 21, 2024, 6:58pm UTC](https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503 "2024-01-21T18:58:11Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![lizozom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lizozom/32/114932_2.png) [@lizozom](https://discuss.elastic.co/u/lizozom)\
**Post date:** [January 21, 2024, 7:14pm UTC](https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503/2 "2024-01-21T19:14:38Z")

</div>

Figured it out myself, posting here for posterity.

This code will copy each document from the `input` to the target index, adding a `inventory_time` with the time the task ran at.

```auto
"actions": {
    "index_payload": {
      "transform": {
        "script": {
          "source": """
          def documents = ctx.payload.hits.hits.stream()
            .map(hit -> [
              "_index": "daily-inventory", 
              "_id": hit._id, 
              "inventory_time": ctx['trigger']['scheduled_time'],
              "source": hit._source.entrySet().stream()
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
            ])
            .collect(Collectors.toList());
          return ["_doc" : documents]; 
          """,
          "lang": "painless"
        }
      },
      "index": {}
    }
  }

```

---

_[View the full topic](https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503)._
