I'm trying to figure out how to use the Update API to update an object field in my document. Let's say my _source field looks like this:
{
"documentID": 123,
"originalFilename": "Build a Better Post.pdf",
"modDate": "2017-11-16T18:22:54.48",
"documentType": "pdf",
"keySystem": "web",
"title": "Build a Better Post",
"createPreview": false,
"uploadedBy": "DA5208B3-2198-44C6-8256-0AEBC4DD1588",
"streamItemData": {
"itemID": 800,
"author": {
"employeeID": 9,
"authorName": {
"firstName": "Joseph",
"preferredName": "Joe",
"lastName": "Smith"
},
"title": "manager"
}
}
}
I would like to update the "streamItemData" field in its entirety. Using the Update API, partial document updates don't work because I want to replace the entire object, not select fields:
POST test/type1/123/_update
{
"doc" : {
"streamItemData": {
"itemID": 340,
"author": {
"employeeID": 32,
"authorName": {
"firstName": "Bill",
"lastName": "Johnson"
}
}
}
}
}
You'll notice that Bill Johnson doesn't have a "preferredName" or "title", but the old values from Joe Smith are still left in there after the update. So that doesn't work for me.
Is it possible to do what I want using a script?
I know that this works:
POST test/type1/123/_update
{
"script" : "Map a = new HashMap(); a.put('employeeID', 32); a.put('firstName', 'Bill'); a.put('lastName', 'Johnson'); Map si = new HashMap(); si.put('itemID', 340); si.put('author', a); ctx._source.streamItemData = si;"
}
But it seems like there has to be a better way. In reality, my "streamItemData" object is much larger and has many more nested objects. Is there a way to pass in the whole object as json to the script instead?