Update plus With Script in Bulk

I believe the upsert element is still valid when doing a scripted upsert, it is just merged in with the doc after the script is run. The scripted upsert example in the docs I posted before looks pretty close to what you want.

The script would contain the code to add your tags to ctx._source. The upsert would contain your initial document. The script will run, adding the tags, and then if the doc doesn't exist, it will be merged with the upsert element. If you have changes, then instead of passing upsert, pass the doc into your script via params. You would then need to do your own merging with a potentially already existing doc.

2 Likes