# 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:** 5\
**Page:** 1

<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, 6:58pm UTC](https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503/1 "2024-01-21T18:58:11Z")

</div>

I have an index and I am using a watcher to monitor it.  
When a condition is met, I want to _copy_ each document to a different index.

I'm following the [example here](https://www.elastic.co/guide/en/elasticsearch/reference/current/actions-index.html):

```auto
  "index_payload": {
    "transform": {
      "script": """
      def documents = ctx.payload.hits.hits.stream()
        .map(hit -> [
          "_index": "my-index-000001", 
          "_id": hit._id, 
          "severity": "Sev: " + hit._source.severity 
        ])
        .collect(Collectors.toList());
      return ["_doc" : documents]; 
      """
    },
    "index": {} 
  }
}

```

But it does not copy other fields from the original document.

Is there a way to copy all the fields AND add a new field to each document?

Thank you

---

<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": {}
    }
  }

```

---

<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:17pm UTC](https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503/3 "2024-01-21T19:17:09Z")

</div>

And if you want a new document to be created each time, get rid of the "\_id" field. A new one will be generated each time the script runs

---

<div class="post-metadata">

**Author:** ![jessgarson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessgarson/32/129841_2.png) [@jessgarson](https://discuss.elastic.co/u/jessgarson)\
**Post date:** [January 22, 2024, 6:35pm UTC](https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503/4 "2024-01-22T18:35:54Z")

</div>

Thanks for sharing your solution @lizozom

---

<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:** [February 19, 2024, 6:36pm UTC](https://discuss.elastic.co/t/use-watcher-index-action-with-multiple-documents/351503/5 "2024-02-19T18:36:03Z")

</div>

Closed on February 19, 2024, 6:36pm UTC

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