diff --git a/docs/reference/docs/update.asciidoc b/docs/reference/docs/update.asciidoc index 347ec657073..4236e06754a 100644 --- a/docs/reference/docs/update.asciidoc +++ b/docs/reference/docs/update.asciidoc @@ -197,10 +197,15 @@ The update operation supports similar parameters as the index API, including: [horizontal] -`routing`:: Sets the routing that will be used to route the - document to the relevant shard. +`routing`:: Routing is used to route the update request to the right shard + and sets the routing for the upsert request if the document being + updated doesn't exist. Can't be used to update the routing of an + existing document. -`parent`:: Simply sets the routing. +`parent`:: Parent is used to route the update request to the right shard + and sets the parent for the upsert request if the document being + updated doesn't exist. Can't be used to update the `parent` of an + existing document. `timeout`:: Timeout waiting for a shard to become available. diff --git a/rest-api-spec/api/update.json b/rest-api-spec/api/update.json index 5d3ed8f06bf..20fc3524283 100644 --- a/rest-api-spec/api/update.json +++ b/rest-api-spec/api/update.json @@ -38,7 +38,7 @@ }, "parent": { "type": "string", - "description": "ID of the parent document" + "description": "ID of the parent document. Is is only used for routing and when for the upsert request" }, "refresh": { "type": "boolean", diff --git a/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java b/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java index b16370f3712..a1b3a09508c 100644 --- a/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java +++ b/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java @@ -353,12 +353,12 @@ public class BulkRequest extends ActionRequest implements Composite } else if ("update".equals(action)) { UpdateRequest updateRequest = new UpdateRequest(index, type, id).routing(routing).parent(parent).retryOnConflict(retryOnConflict) .version(version).versionType(versionType) + .routing(routing) + .parent(parent) .source(data.slice(from, nextMarker - from)); IndexRequest upsertRequest = updateRequest.upsertRequest(); if (upsertRequest != null) { - upsertRequest.routing(routing); - upsertRequest.parent(parent); // order is important, set it after routing, so it will set the routing upsertRequest.timestamp(timestamp); upsertRequest.ttl(ttl); upsertRequest.version(version); @@ -366,8 +366,6 @@ public class BulkRequest extends ActionRequest implements Composite } IndexRequest doc = updateRequest.doc(); if (doc != null) { - doc.routing(routing); - doc.parent(parent); // order is important, set it after routing, so it will set the routing doc.timestamp(timestamp); doc.ttl(ttl); doc.version(version); diff --git a/src/main/java/org/elasticsearch/action/update/UpdateHelper.java b/src/main/java/org/elasticsearch/action/update/UpdateHelper.java index 01ba10c566c..a8e1de53374 100644 --- a/src/main/java/org/elasticsearch/action/update/UpdateHelper.java +++ b/src/main/java/org/elasticsearch/action/update/UpdateHelper.java @@ -118,15 +118,16 @@ public class UpdateHelper extends AbstractComponent { update.setGetResult(getResult); return new Result(update, Operation.NONE, upsertDoc, XContentType.JSON); } - indexRequest.source((Map)ctx.get("_source")); + indexRequest.source((Map) ctx.get("_source")); } indexRequest.index(request.index()).type(request.type()).id(request.id()) // it has to be a "create!" .create(true) - .routing(request.routing()) .ttl(ttl) .refresh(request.refresh()) + .routing(request.routing()) + .parent(request.parent()) .consistencyLevel(request.consistencyLevel()); indexRequest.operationThreaded(false); if (request.versionType() != VersionType.INTERNAL) { diff --git a/src/main/java/org/elasticsearch/action/update/UpdateRequest.java b/src/main/java/org/elasticsearch/action/update/UpdateRequest.java index 62bb23b826e..c3f03db7944 100644 --- a/src/main/java/org/elasticsearch/action/update/UpdateRequest.java +++ b/src/main/java/org/elasticsearch/action/update/UpdateRequest.java @@ -55,6 +55,9 @@ public class UpdateRequest extends InstanceShardOperationRequest @Nullable private String routing; + @Nullable + private String parent; + @Nullable String script; @Nullable @@ -174,17 +177,6 @@ public class UpdateRequest extends InstanceShardOperationRequest return this; } - /** - * Sets the parent id of this document. Will simply set the routing to this value, as it is only - * used for routing with delete requests. - */ - public UpdateRequest parent(String parent) { - if (routing == null) { - routing = parent; - } - return this; - } - /** * Controls the shard routing of the request. Using this value to hash the shard * and not the id. @@ -194,6 +186,21 @@ public class UpdateRequest extends InstanceShardOperationRequest return this.routing; } + /** + * The parent id is used for the upsert request and also implicitely sets the routing if not already set. + */ + public UpdateRequest parent(String parent) { + this.parent = parent; + if (routing == null) { + routing = parent; + } + return this; + } + + public String parent() { + return parent; + } + int shardId() { return this.shardId; } @@ -631,6 +638,7 @@ public class UpdateRequest extends InstanceShardOperationRequest type = in.readString(); id = in.readString(); routing = in.readOptionalString(); + parent = in.readOptionalString(); script = in.readOptionalString(); if(Strings.hasLength(script)) { scriptType = ScriptService.ScriptType.readFrom(in); @@ -668,6 +676,7 @@ public class UpdateRequest extends InstanceShardOperationRequest out.writeString(type); out.writeString(id); out.writeOptionalString(routing); + out.writeOptionalString(parent); out.writeOptionalString(script); if (Strings.hasLength(script)) { ScriptService.ScriptType.writeTo(scriptType, out); diff --git a/src/main/java/org/elasticsearch/rest/action/update/RestUpdateAction.java b/src/main/java/org/elasticsearch/rest/action/update/RestUpdateAction.java index 1b36f338a13..c884fe4267e 100644 --- a/src/main/java/org/elasticsearch/rest/action/update/RestUpdateAction.java +++ b/src/main/java/org/elasticsearch/rest/action/update/RestUpdateAction.java @@ -57,7 +57,7 @@ public class RestUpdateAction extends BaseRestHandler { UpdateRequest updateRequest = new UpdateRequest(request.param("index"), request.param("type"), request.param("id")); updateRequest.listenerThreaded(false); updateRequest.routing(request.param("routing")); - updateRequest.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing + updateRequest.parent(request.param("parent")); updateRequest.timeout(request.paramAsTime("timeout", updateRequest.timeout())); updateRequest.refresh(request.paramAsBoolean("refresh", updateRequest.refresh())); String consistencyLevel = request.param("consistency");