From 37e2bb705721eb29415dba4c7635b20aaca3d29b Mon Sep 17 00:00:00 2001 From: James Rodewig Date: Wed, 20 May 2020 16:57:45 -0400 Subject: [PATCH] [DOCS] Add watcher multi-doc index ex (#52040) (#57011) Adds an example snippet for creating a `_doc` payload field with the Watcher `index` action. Co-authored-by: Luiz Guilherme Pais dos Santos --- x-pack/docs/en/watcher/actions/index.asciidoc | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/x-pack/docs/en/watcher/actions/index.asciidoc b/x-pack/docs/en/watcher/actions/index.asciidoc index 14a755712bb7a..2ac15906019c4 100644 --- a/x-pack/docs/en/watcher/actions/index.asciidoc +++ b/x-pack/docs/en/watcher/actions/index.asciidoc @@ -73,5 +73,34 @@ When a `_doc` field exists, if the field holds an object, it is extracted and in as a single document. If the field holds an array of objects, each object is treated as a document and the index action indexes all of them in a bulk. -An `_index`, or `_id` value can be added per document to dynamically set the ID +An `_index`, or `_id` value can be added per document to dynamically set the index and ID of the indexed document. + +The following snippet shows a multi-document `index` action definition: + +[source,js] +-------------------------------------------------- +"actions": { + "index_payload": { + "transform": { + "script": """ + def documents = ctx.payload.hits.hits.stream() + .map(hit -> [ + "_index": "my-index", <1> + "_id": hit._id, <2> + "severity": "Sev: " + hit._source.severity <3> + ]) + .collect(Collectors.toList()); + return [ "_doc" : documents]; <4> + """ + }, + "index": {} <5> + } +} +-------------------------------------------------- +// NOTCONSOLE +<1> The document's index +<2> An optional `_id` for the document +<3> A new `severity` field derived from the original document +<4> The payload `_doc` field which is an array of documents +<5> Since the `_index` was informed per document this should be empty