ソースを参照

Make sure that the parent option on the update request only is delgated to upsert index request.

Closes #4538
Martijn van Groningen 10 年 前
コミット
6d1a1b328b

+ 9 - 4
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.
-
-`parent`::      Simply sets the routing.
+`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`::      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.
 

+ 1 - 1
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",

+ 2 - 4
src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

@@ -353,12 +353,12 @@ public class BulkRequest extends ActionRequest<BulkRequest> 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<BulkRequest> 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);

+ 3 - 2
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) {

+ 18 - 9
src/main/java/org/elasticsearch/action/update/UpdateRequest.java

@@ -55,6 +55,9 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
     @Nullable
     private String routing;
 
+    @Nullable
+    private String parent;
+
     @Nullable
     String script;
     @Nullable
@@ -175,23 +178,27 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
     }
 
     /**
-     * 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.
+     * Controls the shard routing of the request. Using this value to hash the shard
+     * and not the id.
+     */
+    @Override
+    public String routing() {
+        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;
     }
 
-    /**
-     * Controls the shard routing of the request. Using this value to hash the shard
-     * and not the id.
-     */
-    @Override
-    public String routing() {
-        return this.routing;
+    public String parent() {
+        return parent;
     }
 
     int shardId() {
@@ -631,6 +638,7 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
         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<UpdateRequest>
         out.writeString(type);
         out.writeString(id);
         out.writeOptionalString(routing);
+        out.writeOptionalString(parent);
         out.writeOptionalString(script);
         if (Strings.hasLength(script)) {
             ScriptService.ScriptType.writeTo(scriptType, out);

+ 1 - 1
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");