Update API - allow scripted upserts #7143
Labels
:Distributed Indexing/CRUD
A catch all label for issues around indexing, updating and getting a doc by id. Not search.
>enhancement
>feature
v1.4.0.Beta1
v2.0.0-beta1
In some scenarios it is useful if the update script used to add new content to an existing document is also capable of being called when the document does not already exist (aka Upsert requests).
Now that we have stored scripts they can be non-trivial chunks of code and may have a lot of business logic to manage the state of an item. Object orientation teaches us the value of encapsulation and this enhancement allows the scripts to encapsulate the logic used in the construction of new documents as well as the current logic for modifying existing documents.
The suggested change is that a new
scripted_upsert
parameter can be passed to upsert requests to indicate if the script should be called to perform inserts. The default value is "false" to reflect the current behaviour. If set to "true" and an insert is being formed the following steps occur:upsert
document passed by the client (which now could be {} ) is presented in the script context as the initial state of the document held inctx._source
.ctx.op
field in the script context is set tocreate
to indicate that this is an insert as opposed to update operationparams
values passed from the client (in the same way updates are invoked). It mutates the map of values held inctx._source
appropriately.ctx.op
tonone
if it wants to ignore the insert, otherwise the document held inctx._source
after the script completes is storedThe text was updated successfully, but these errors were encountered: