Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API - allow scripted upserts #7143

Closed
markharwood opened this Issue Aug 4, 2014 · 0 comments

Comments

Projects
None yet
2 participants
@markharwood
Copy link
Contributor

markharwood commented Aug 4, 2014

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:

  1. The example upsert document passed by the client (which now could be {} ) is presented in the script context as the initial state of the document held in ctx._source.
  2. The ctx.op field in the script context is set to create to indicate that this is an insert as opposed to update operation
  3. The script (or stored script) used to perform updates is called with the params values passed from the client (in the same way updates are invoked). It mutates the map of values held in ctx._source appropriately.
  4. The script can set ctx.op to none if it wants to ignore the insert, otherwise the document held in ctx._source after the script completes is stored

markharwood added a commit that referenced this issue Aug 5, 2014

Update API enhancement - add support for scripted upserts.
In the case of inserts the UpdateHelper class will now allow the script used to apply updates to run on the upsert doc provided by clients. This allows the logic for managing the internal state of the data item to be managed by the script and is not reliant on clients performing the initialisation of data structures managed by the script.

Closes #7143

@markharwood markharwood added v1.3.2 and removed v1.3.2 labels Aug 13, 2014

markharwood added a commit that referenced this issue Sep 8, 2014

Update API enhancement - add support for scripted upserts.
In the case of inserts the UpdateHelper class will now allow the script used to apply updates to run on the upsert doc provided by clients. This allows the logic for managing the internal state of the data item to be managed by the script and is not reliant on clients performing the initialisation of data structures managed by the script.

Closes #7143
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.