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: Add support for scripted upserts. #7144

Closed
wants to merge 4 commits into from

Conversation

Projects
None yet
5 participants
@markharwood
Copy link
Contributor

markharwood commented Aug 4, 2014

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.
Associated issue: #7143

markharwood added some commits Aug 4, 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.
//Allow the script to abort the create by setting "op" to "none"
String scriptOpChoice = (String) ctx.get("op");
if (scriptOpChoice != null) {
if ("none".equals(scriptOpChoice)) {

This comment has been minimized.

Copy link
@martijnvg

martijnvg Aug 4, 2014

Member

Maybe these if statements can be combined? Something like this:

if (!"create".equals(scriptOpChoice)) {
   if ("none".equals(scriptOpChoice)) {
      logger.warn("Used upsert operation [{}] for script [{}], doing nothing...", scriptOpChoice, request.script);
   }
   UpdateResponse update = new UpdateResponse(getResult.getIndex(), getResult.getType(), getResult.getId(),
   getResult.getVersion(), false);
   update.setGetResult(getResult);
   return new Result(update, Operation.NONE, upsertDoc, XContentType.JSON);
}

This comment has been minimized.

Copy link
@markharwood

markharwood Aug 4, 2014

Author Contributor

Thanks, Martijn. Looks good but the log msg should only be output if not none i.e.

if (!"none".equals(scriptOpChoice)) {
    logger.warn("Used upsert operation [{}] for script [{}], doing nothing...", scriptOpChoice, request.script);
}
IndexRequest indexRequest = request.docAsUpsert() ? request.doc() : request.upsertRequest();
if(request.scriptedUpsert()&&(request.script() != null)){

This comment has been minimized.

Copy link
@martijnvg

martijnvg Aug 4, 2014

Member

maybe add some whitespaces in this if statement (between if and ( and &&)? looks very compact now.

@martijnvg

This comment has been minimized.

Copy link
Member

martijnvg commented Aug 4, 2014

[source,js]
--------------------------------------------------
curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
"script_id" : "myWebSessionSummariser",

This comment has been minimized.

Copy link
@jpountz

jpountz Aug 4, 2014

Contributor

use underscore_case for consistency with the rest of the docs?

@markharwood

This comment has been minimized.

Copy link
Contributor Author

markharwood commented Aug 5, 2014

Will go ahead and push if no objections

@jpountz

This comment has been minimized.

Copy link
Contributor

jpountz commented Aug 5, 2014

LGTM

@markharwood

This comment has been minimized.

Copy link
Contributor Author

markharwood commented Aug 5, 2014

Committed in e6b459c

@markharwood markharwood closed this Aug 5, 2014

@markharwood markharwood removed the review label Aug 5, 2014

@clintongormley clintongormley changed the title Update API enhancement - add support for scripted upserts. Update API: Add support for scripted upserts. Sep 10, 2014

return this.scriptedUpsert;
}

public void scriptedUpsert(boolean scriptedUpsert) {

This comment has been minimized.

Copy link
@dadoonet

dadoonet Nov 30, 2014

Member

This should return this;

@@ -621,6 +624,15 @@ public boolean docAsUpsert() {
public void docAsUpsert(boolean shouldUpsertDoc) {

This comment has been minimized.

Copy link
@dadoonet

dadoonet Nov 30, 2014

Member

This should return this; (though it's not part of the same PR) :)

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.