Skip to content

Commit eb9de19

Browse files
authored
[DOCS] Add watcher multi-doc index ex (#52040)
Adds an example snippet for creating a `_doc` payload field with the Watcher `index` action.
1 parent 9a49075 commit eb9de19

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

x-pack/docs/en/watcher/actions/index.asciidoc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,34 @@ When a `_doc` field exists, if the field holds an object, it is extracted and in
7373
as a single document. If the field holds an array of objects, each object is treated as
7474
a document and the index action indexes all of them in a bulk.
7575

76-
An `_index`, or `_id` value can be added per document to dynamically set the ID
76+
An `_index`, or `_id` value can be added per document to dynamically set the index and ID
7777
of the indexed document.
78+
79+
The following snippet shows a multi-document `index` action definition:
80+
81+
[source,js]
82+
--------------------------------------------------
83+
"actions": {
84+
"index_payload": {
85+
"transform": {
86+
"script": """
87+
def documents = ctx.payload.hits.hits.stream()
88+
.map(hit -> [
89+
"_index": "my-index", <1>
90+
"_id": hit._id, <2>
91+
"severity": "Sev: " + hit._source.severity <3>
92+
])
93+
.collect(Collectors.toList());
94+
return [ "_doc" : documents]; <4>
95+
"""
96+
},
97+
"index": {} <5>
98+
}
99+
}
100+
--------------------------------------------------
101+
// NOTCONSOLE
102+
<1> The document's index
103+
<2> An optional `_id` for the document
104+
<3> A new `severity` field derived from the original document
105+
<4> The payload `_doc` field which is an array of documents
106+
<5> Since the `_index` was informed per document this should be empty

0 commit comments

Comments
 (0)