Iterate over payload in Index action

Hi,
I am trying to iterate over payload result and index multiple rows using watcher index action.
Using my watcher I trying to get the list of host for which CPU crossing the threshold limit. I want to index multiple row in my index for different host.
Can anyone please suggest any solution?
Thanks

the index action allow you to index several documents in one action, see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/actions-index.html#anatomy-actions-index-multi-doc-support

What you need to do is to get the data in a proper format. This can be done by using a script transform within your index action.

If you need some examples, check out the examples repo, that contains a few example watches that can help you get up and running.

--Alex

Hi,
Thanks for your help..
I have tried to implement the same but getting error "could not execute action [index_payload] of watch [inlined]. [ctx.payload._index] or [ctx.payload._doc._index] were set together with action [index] field. Only set one of them"
According to other post I have found needs to remove _index field from the payload. I have tried a transform
def hits = ctx.payload.hits.hits.map(hit -> hit.remove('_index')).collect(Collectors.toList());
return ['_doc' : hits ]

But getting below error

"script": "ctx.payload.hits.hits.map(hit -> hit.remove('_index')).collect(Collectors.toList());",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "dynamic method [java.util.ArrayList, map/1] not found"
}

Can you please help to fix the issue?

ctx.payload.hits.hits is an arraylist, if you want to use a stream on that, you need to call ctx.payload.hits.hits.stream() first.

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