I have documents that it might have a variable number of fields, I know that it sill contain a customID, and I would like to replace all the fields except my customID by using the _update_by_query API
Initial Document
{
customID : “123”,
field1 : ”a”,
field2 : “b”,
field3 : “c”,
….
}
I want my target to be:
{
customID : “123”,
replaced: true
}
Regardless of the number of extra fields available in the document.
CodePudding user response:
You can do it like this:
POST test/_update_by_query
{
"script": {
"source": "def customID = ctx._source.customID; ctx._source.clear(); ctx._source = ['customID': customID, 'replaced': true]",
"lang": "painless"
}
}