diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index e47585356a0..fcc0fdebdd4 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -243,7 +243,8 @@ public class MetaData implements Iterable, Diffable, To * * @param aliases The names of the index aliases to find * @param concreteIndices The concrete indexes the index aliases must point to order to be returned. - * @return the found index aliases grouped by index + * @return a map of index to a list of alias metadata, the list corresponding to a concrete index will be empty if no aliases are + * present for that index */ public ImmutableOpenMap> findAliases(final String[] aliases, String[] concreteIndices) { assert aliases != null; @@ -273,8 +274,8 @@ public class MetaData implements Iterable, Diffable, To return o1.alias().compareTo(o2.alias()); } }); - mapBuilder.put(index, Collections.unmodifiableList(filteredValues)); } + mapBuilder.put(index, Collections.unmodifiableList(filteredValues)); } return mapBuilder.build(); } diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java index ffb34c6b939..51ff743d2d1 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java @@ -76,6 +76,7 @@ public class RestGetAliasesAction extends BaseRestHandler { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { + final boolean namesProvided = request.hasParam("name"); final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name"); final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases); final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); @@ -89,9 +90,13 @@ public class RestGetAliasesAction extends BaseRestHandler { final ImmutableOpenMap> aliasMap = response.getAliases(); final Set aliasNames = new HashSet<>(); - for (final ObjectCursor> cursor : aliasMap.values()) { + final Set indicesToDisplay = new HashSet<>(); + for (final ObjectObjectCursor> cursor : aliasMap) { for (final AliasMetaData aliasMetaData : cursor.value) { aliasNames.add(aliasMetaData.alias()); + if (namesProvided) { + indicesToDisplay.add(cursor.key); + } } } @@ -131,17 +136,19 @@ public class RestGetAliasesAction extends BaseRestHandler { } for (final ObjectObjectCursor> entry : response.getAliases()) { - builder.startObject(entry.key); - { - builder.startObject("aliases"); + if (namesProvided == false || (namesProvided && indicesToDisplay.contains(entry.key))) { + builder.startObject(entry.key); { - for (final AliasMetaData alias : entry.value) { - AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS); + builder.startObject("aliases"); + { + for (final AliasMetaData alias : entry.value) { + AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS); + } } + builder.endObject(); } builder.endObject(); } - builder.endObject(); } } builder.endObject(); diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexIT.java b/core/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexIT.java index 2bd13669fee..a34e6bcc0c4 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexIT.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.action.admin.indices.get; +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature; import org.elasticsearch.cluster.metadata.AliasMetaData; @@ -281,6 +282,8 @@ public class GetIndexIT extends ESIntegTestCase { private void assertEmptyAliases(GetIndexResponse response) { assertThat(response.aliases(), notNullValue()); - assertThat(response.aliases().isEmpty(), equalTo(true)); + for (final ObjectObjectCursor> entry : response.getAliases()) { + assertTrue(entry.value.isEmpty()); + } } } diff --git a/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java b/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java index 5de9c211a6c..9526b5b97e6 100644 --- a/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java +++ b/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.aliases; +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse; @@ -32,6 +33,7 @@ import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.AliasOrIndex; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.StopWatch; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; @@ -49,6 +51,7 @@ import org.elasticsearch.test.ESIntegTestCase; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -567,20 +570,24 @@ public class IndexAliasesIT extends ESIntegTestCase { logger.info("--> getting alias1"); GetAliasesResponse getResponse = admin().indices().prepareGetAliases("alias1").get(); assertThat(getResponse, notNullValue()); - assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().size(), equalTo(5)); assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1)); assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1")); assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + assertTrue(getResponse.getAliases().get("test").isEmpty()); + assertTrue(getResponse.getAliases().get("test123").isEmpty()); + assertTrue(getResponse.getAliases().get("foobarbaz").isEmpty()); + assertTrue(getResponse.getAliases().get("bazbar").isEmpty()); AliasesExistResponse existsResponse = admin().indices().prepareAliasesExist("alias1").get(); assertThat(existsResponse.exists(), equalTo(true)); logger.info("--> getting all aliases that start with alias*"); getResponse = admin().indices().prepareGetAliases("alias*").get(); assertThat(getResponse, notNullValue()); - assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().size(), equalTo(5)); assertThat(getResponse.getAliases().get("foobar").size(), equalTo(2)); assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1")); @@ -592,6 +599,10 @@ public class IndexAliasesIT extends ESIntegTestCase { assertThat(getResponse.getAliases().get("foobar").get(1).getFilter(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(1).getIndexRouting(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(1).getSearchRouting(), nullValue()); + assertTrue(getResponse.getAliases().get("test").isEmpty()); + assertTrue(getResponse.getAliases().get("test123").isEmpty()); + assertTrue(getResponse.getAliases().get("foobarbaz").isEmpty()); + assertTrue(getResponse.getAliases().get("bazbar").isEmpty()); existsResponse = admin().indices().prepareAliasesExist("alias*").get(); assertThat(existsResponse.exists(), equalTo(true)); @@ -676,12 +687,13 @@ public class IndexAliasesIT extends ESIntegTestCase { logger.info("--> getting f* for index *bar"); getResponse = admin().indices().prepareGetAliases("f*").addIndices("*bar").get(); assertThat(getResponse, notNullValue()); - assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().size(), equalTo(2)); assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo")); assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + assertTrue(getResponse.getAliases().get("bazbar").isEmpty()); existsResponse = admin().indices().prepareAliasesExist("f*") .addIndices("*bar").get(); assertThat(existsResponse.exists(), equalTo(true)); @@ -690,13 +702,14 @@ public class IndexAliasesIT extends ESIntegTestCase { logger.info("--> getting f* for index *bac"); getResponse = admin().indices().prepareGetAliases("foo").addIndices("*bac").get(); assertThat(getResponse, notNullValue()); - assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().size(), equalTo(2)); assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1)); assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo")); assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + assertTrue(getResponse.getAliases().get("bazbar").isEmpty()); existsResponse = admin().indices().prepareAliasesExist("foo") .addIndices("*bac").get(); assertThat(existsResponse.exists(), equalTo(true)); @@ -729,7 +742,9 @@ public class IndexAliasesIT extends ESIntegTestCase { .removeAlias("foobar", "foo")); getResponse = admin().indices().prepareGetAliases("foo").addIndices("foobar").get(); - assertThat(getResponse.getAliases().isEmpty(), equalTo(true)); + for (final ObjectObjectCursor> entry : getResponse.getAliases()) { + assertTrue(entry.value.isEmpty()); + } existsResponse = admin().indices().prepareAliasesExist("foo").addIndices("foobar").get(); assertThat(existsResponse.exists(), equalTo(false)); } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.delete_alias/all_path_options.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.delete_alias/all_path_options.yml index d1d01cbaaa7..2882250519c 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.delete_alias/all_path_options.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.delete_alias/all_path_options.yml @@ -84,6 +84,10 @@ setup: --- "check delete with index list": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + - do: indices.delete_alias: index: "test_index1,test_index2" @@ -106,6 +110,10 @@ setup: --- "check delete with prefix* index": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + - do: indices.delete_alias: index: "test_*" @@ -129,6 +137,10 @@ setup: --- "check delete with index list and * aliases": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + - do: indices.delete_alias: index: "test_index1,test_index2" @@ -152,6 +164,10 @@ setup: --- "check delete with index list and _all aliases": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + - do: indices.delete_alias: index: "test_index1,test_index2" @@ -175,6 +191,10 @@ setup: --- "check delete with index list and wildcard aliases": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + - do: indices.delete_alias: index: "test_index1,test_index2" diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_alias/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_alias/10_basic.yml index 6678cbebbd5..4e3861b059b 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_alias/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_alias/10_basic.yml @@ -40,6 +40,62 @@ setup: - match: {test_index.aliases.test_blias: {}} - is_false: test_index_2 +--- +"Get aliases via /_all/_alias/": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + + - do: + indices.create: + index: myindex + + - do: + indices.get_alias: + index: _all + + - match: {test_index.aliases.test_alias: {}} + - match: {test_index.aliases.test_blias: {}} + - match: {test_index_2.aliases.test_alias: {}} + - match: {test_index_2.aliases.test_blias: {}} + - match: {myindex.aliases: {}} + +--- +"Get aliases via /*/_alias/": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + + - do: + indices.create: + index: myindex + + - do: + indices.get_alias: + index: "*" + + - match: {test_index.aliases.test_alias: {}} + - match: {test_index.aliases.test_blias: {}} + - match: {test_index_2.aliases.test_alias: {}} + - match: {test_index_2.aliases.test_blias: {}} + - match: {myindex.aliases: {}} + +--- +"Get and index with no aliases via /{index}/_alias/": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x + + - do: + indices.create: + index: myindex + + - do: + indices.get_alias: + index: myindex + + - match: {myindex.aliases: {}} + --- "Get specific alias via /{index}/_alias/{name}": diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_alias/all_path_options.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_alias/all_path_options.yml index 05858dd176b..aa030aa5546 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_alias/all_path_options.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_alias/all_path_options.yml @@ -14,6 +14,9 @@ setup: --- "put alias per index": + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x - do: indices.put_alias: @@ -69,7 +72,9 @@ setup: --- "put alias prefix* index": - + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x - do: indices.put_alias: @@ -86,7 +91,9 @@ setup: --- "put alias in list of indices": - + - skip: + version: " - 5.99.99" + reason: only requested indices are included in 6.x - do: indices.put_alias: