lizozom
(Liza Katz)
January 21, 2024, 6:58pm
1
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 :
"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
lizozom
(Liza Katz)
January 21, 2024, 7:14pm
2
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.
"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": {}
}
}
1 Like
lizozom
(Liza Katz)
January 21, 2024, 7:17pm
3
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
1 Like
jessgarson
(Jess Garson)
January 22, 2024, 6:35pm
4
Thanks for sharing your solution @lizozom
system
(system)
Closed
February 19, 2024, 6:36pm
5
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.