Browse Source

Return the index name on a create index response

This commit modifies the create index response so that it includes the
index name.

Relates #25139
Sergey Novikov 8 years ago
parent
commit
7c8657df0e

+ 15 - 1
core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java

@@ -19,6 +19,7 @@
 
 package org.elasticsearch.action.admin.indices.create;
 
+import org.elasticsearch.Version;
 import org.elasticsearch.action.support.master.AcknowledgedResponse;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
@@ -32,14 +33,16 @@ import java.io.IOException;
 public class CreateIndexResponse extends AcknowledgedResponse {
 
     private boolean shardsAcked;
+    private String index;
 
     protected CreateIndexResponse() {
     }
 
-    protected CreateIndexResponse(boolean acknowledged, boolean shardsAcked) {
+    protected CreateIndexResponse(boolean acknowledged, boolean shardsAcked, String index) {
         super(acknowledged);
         assert acknowledged || shardsAcked == false; // if its not acknowledged, then shards acked should be false too
         this.shardsAcked = shardsAcked;
+        this.index = index;
     }
 
     @Override
@@ -47,6 +50,9 @@ public class CreateIndexResponse extends AcknowledgedResponse {
         super.readFrom(in);
         readAcknowledged(in);
         shardsAcked = in.readBoolean();
+        if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha3)) {
+            index = in.readString();
+        }
     }
 
     @Override
@@ -54,6 +60,9 @@ public class CreateIndexResponse extends AcknowledgedResponse {
         super.writeTo(out);
         writeAcknowledged(out);
         out.writeBoolean(shardsAcked);
+        if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha3)) {
+            out.writeString(index);
+        }
     }
 
     /**
@@ -65,7 +74,12 @@ public class CreateIndexResponse extends AcknowledgedResponse {
         return shardsAcked;
     }
 
+    public String index() {
+        return index;
+    }
+
     public void addCustomFields(XContentBuilder builder) throws IOException {
         builder.field("shards_acknowledged", isShardsAcked());
+        builder.field("index", index());
     }
 }

+ 1 - 1
core/src/main/java/org/elasticsearch/action/admin/indices/create/TransportCreateIndexAction.java

@@ -79,7 +79,7 @@ public class TransportCreateIndexAction extends TransportMasterNodeAction<Create
                 .waitForActiveShards(request.waitForActiveShards());
 
         createIndexService.createIndex(updateRequest, ActionListener.wrap(response ->
-            listener.onResponse(new CreateIndexResponse(response.isAcknowledged(), response.isShardsAcked())),
+            listener.onResponse(new CreateIndexResponse(response.isAcknowledged(), response.isShardsAcked(), indexName)),
             listener::onFailure));
     }
 

+ 2 - 2
core/src/main/java/org/elasticsearch/action/admin/indices/shrink/ShrinkResponse.java

@@ -25,7 +25,7 @@ public final class ShrinkResponse extends CreateIndexResponse {
     ShrinkResponse() {
     }
 
-    ShrinkResponse(boolean acknowledged, boolean shardsAcked) {
-        super(acknowledged, shardsAcked);
+    ShrinkResponse(boolean acknowledged, boolean shardsAcked, String index) {
+        super(acknowledged, shardsAcked, index);
     }
 }

+ 7 - 2
core/src/main/java/org/elasticsearch/action/admin/indices/shrink/TransportShrinkAction.java

@@ -91,8 +91,13 @@ public class TransportShrinkAction extends TransportMasterNodeAction<ShrinkReque
                         IndexShardStats shard = indicesStatsResponse.getIndex(sourceIndex).getIndexShards().get(i);
                         return shard == null ? null : shard.getPrimary().getDocs();
                     }, indexNameExpressionResolver);
-                createIndexService.createIndex(updateRequest, ActionListener.wrap(response ->
-                    listener.onResponse(new ShrinkResponse(response.isAcknowledged(), response.isShardsAcked())), listener::onFailure));
+                createIndexService.createIndex(
+                    updateRequest,
+                    ActionListener.wrap(response ->
+                        listener.onResponse(new ShrinkResponse(response.isAcknowledged(), response.isShardsAcked(), updateRequest.index())),
+                        listener::onFailure
+                    )
+                );
             }
 
             @Override

+ 8 - 0
core/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java

@@ -335,4 +335,12 @@ public class CreateIndexIT extends ESIntegTestCase {
 
         assertTrue(createPartitionedIndex.apply(1, 1));
     }
+
+    public void testIndexNameInResponse() {
+        CreateIndexResponse response = prepareCreate("foo")
+            .setSettings(Settings.builder().build())
+            .get();
+
+        assertEquals("Should have index name in response", "foo", response.index());
+    }
 }

+ 65 - 0
core/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java

@@ -0,0 +1,65 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.elasticsearch.action.admin.indices.create;
+
+import org.elasticsearch.Version;
+import org.elasticsearch.common.io.stream.BytesStreamOutput;
+import org.elasticsearch.common.io.stream.StreamInput;
+import org.elasticsearch.test.ESTestCase;
+
+import java.io.IOException;
+
+public class CreateIndexResponseTests extends ESTestCase {
+
+    public void testSerialization() throws IOException {
+        CreateIndexResponse response = new CreateIndexResponse(true, true, "foo");
+
+        try (BytesStreamOutput output = new BytesStreamOutput()) {
+            response.writeTo(output);
+
+            try (StreamInput in = output.bytes().streamInput()) {
+                CreateIndexResponse serialized = new CreateIndexResponse();
+                serialized.readFrom(in);
+                assertEquals(response.isShardsAcked(), serialized.isShardsAcked());
+                assertEquals(response.isAcknowledged(), serialized.isAcknowledged());
+                assertEquals(response.index(), serialized.index());
+            }
+        }
+    }
+
+    public void testSerializationWithOldVersion() throws IOException {
+        Version oldVersion = Version.V_5_4_0;
+        CreateIndexResponse response = new CreateIndexResponse(true, true, "foo");
+
+        try (BytesStreamOutput output = new BytesStreamOutput()) {
+            output.setVersion(oldVersion);
+            response.writeTo(output);
+
+            try (StreamInput in = output.bytes().streamInput()) {
+                in.setVersion(oldVersion);
+                CreateIndexResponse serialized = new CreateIndexResponse();
+                serialized.readFrom(in);
+                assertEquals(response.isShardsAcked(), serialized.isShardsAcked());
+                assertEquals(response.isAcknowledged(), serialized.isAcknowledged());
+                assertNull(serialized.index());
+            }
+        }
+    }
+}

+ 1 - 1
core/src/test/java/org/elasticsearch/action/bulk/TransportBulkActionIndicesThatCannotBeCreatedTests.java

@@ -118,7 +118,7 @@ public class TransportBulkActionIndicesThatCannotBeCreatedTests extends ESTestCa
             @Override
             void createIndex(String index, TimeValue timeout, ActionListener<CreateIndexResponse> listener) {
                 // If we try to create an index just immediately assume it worked
-                listener.onResponse(new CreateIndexResponse(true, true) {});
+                listener.onResponse(new CreateIndexResponse(true, true, index) {});
             }
         };
         action.doExecute(null, bulkRequest, null);

+ 2 - 1
docs/reference/indices/create-index.asciidoc

@@ -129,7 +129,8 @@ what happened:
 --------------------------------------------------
 {
     "acknowledged": true,
-    "shards_acknowledged": true
+    "shards_acknowledged": true,
+    "index": "test"
 }
 --------------------------------------------------
 // TESTRESPONSE

+ 13 - 0
rest-api-spec/src/main/resources/rest-api-spec/test/indices.create/10_basic.yml

@@ -30,6 +30,19 @@
 
   - match: { test_index.settings.index.number_of_replicas: "0"}
 
+---
+"Create index":
+  - skip:
+      version: " - 5.99.99"
+      reason: create index response contains index name since 5.6.0
+
+  - do:
+      indices.create:
+        index: test_index
+
+  - match: { acknowledged: true }
+  - match: { index: "test_index"}
+
 ---
 "Create index with wait_for_active_shards set to all":