diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java index 13b7ee92435..1da26627469 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java @@ -78,19 +78,19 @@ public class IndicesAliasesRequest extends AcknowledgedRequest filter = null; String routing = null; boolean routingSet = false; @@ -90,9 +92,9 @@ public class RestIndicesAliasesAction extends BaseRestHandler { currentFieldName = parser.currentName(); } else if (token.isValue()) { if ("index".equals(currentFieldName)) { - index = parser.text(); + indices = new String[] { parser.text() }; } else if ("alias".equals(currentFieldName)) { - alias = parser.text(); + aliases = new String[] { parser.text() }; } else if ("routing".equals(currentFieldName)) { routing = parser.textOrNull(); routingSet = true; @@ -103,6 +105,23 @@ public class RestIndicesAliasesAction extends BaseRestHandler { searchRouting = parser.textOrNull(); searchRoutingSet = true; } + } else if (token == XContentParser.Token.START_ARRAY) { + if ("indices".equals(currentFieldName)) { + List indexNames = new ArrayList<>(); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + String index = parser.text(); + indexNames.add(index); + } + indices = indexNames.toArray(new String[indexNames.size()]); + } + if ("aliases".equals(currentFieldName)) { + List aliasNames = new ArrayList<>(); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + String alias = parser.text(); + aliasNames.add(alias); + } + aliases = aliasNames.toArray(new String[aliasNames.size()]); + } } else if (token == XContentParser.Token.START_OBJECT) { if ("filter".equals(currentFieldName)) { filter = parser.mapOrdered(); @@ -111,19 +130,19 @@ public class RestIndicesAliasesAction extends BaseRestHandler { } if (type == AliasAction.Type.ADD) { - AliasAction aliasAction = newAddAliasAction(index, alias).filter(filter); + AliasActions aliasActions = new AliasActions(type, indices, aliases); if (routingSet) { - aliasAction.routing(routing); + aliasActions.routing(routing); } if (indexRoutingSet) { - aliasAction.indexRouting(indexRouting); + aliasActions.indexRouting(indexRouting); } if (searchRoutingSet) { - aliasAction.searchRouting(searchRouting); + aliasActions.searchRouting(searchRouting); } - indicesAliasesRequest.addAliasAction(aliasAction); + indicesAliasesRequest.addAliasAction(aliasActions); } else if (type == AliasAction.Type.REMOVE) { - indicesAliasesRequest.removeAlias(index, alias); + indicesAliasesRequest.removeAlias(indices, aliases); } } } diff --git a/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java b/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java index 81b09c8c8e2..3a3876b60b1 100644 --- a/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java +++ b/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java @@ -759,7 +759,7 @@ public class IndexAliasesIT extends ESIntegTestCase { admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction("index1", null)).get(); fail("Expected ActionRequestValidationException"); } catch (ActionRequestValidationException e) { - assertThat(e.getMessage(), containsString("requires an [alias] to be set")); + assertThat(e.getMessage(), containsString("[alias] may not be empty string")); } } @@ -768,7 +768,7 @@ public class IndexAliasesIT extends ESIntegTestCase { admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction("index1", "")).get(); fail("Expected ActionRequestValidationException"); } catch (ActionRequestValidationException e) { - assertThat(e.getMessage(), containsString("requires an [alias] to be set")); + assertThat(e.getMessage(), containsString("[alias] may not be empty string")); } } diff --git a/docs/reference/indices/aliases.asciidoc b/docs/reference/indices/aliases.asciidoc index 9a65c89837d..57faa9718f9 100644 --- a/docs/reference/indices/aliases.asciidoc +++ b/docs/reference/indices/aliases.asciidoc @@ -63,7 +63,22 @@ curl -XPOST 'http://localhost:9200/_aliases' -d ' }' -------------------------------------------------- -Alternatively, you can use a glob pattern to associate an alias to +Multiple indices can be specified for an action with the `indices` array syntax: + +[source,js] +-------------------------------------------------- +curl -XPOST 'http://localhost:9200/_aliases' -d ' +{ + "actions" : [ + { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } } + ] +}' +-------------------------------------------------- + +To specify multiple aliases in one action, the corresponding `aliases` array +syntax exists as well. + +For the example above, a glob pattern can also be used to associate an alias to more than one index that share a common name: [source,js] diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.update_aliases/10_basic.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.update_aliases/10_basic.yaml index 5b45f740e44..041f6bb3cc0 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.update_aliases/10_basic.yaml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.update_aliases/10_basic.yaml @@ -32,3 +32,50 @@ name: test_alias - match: {test_index.aliases.test_alias: {'index_routing': 'routing_value', 'search_routing': 'routing_value'}} + +--- +"Basic test for multiple aliases": + + - do: + indices.create: + index: test_index + + - do: + indices.exists_alias: + name: test_alias1 + + - is_false: '' + + - do: + indices.exists_alias: + name: test_alias2 + + - is_false: '' + + - do: + indices.update_aliases: + body: + actions: + - add: + indices: [test_index] + aliases: [test_alias1, test_alias2] + routing: routing_value + + - do: + indices.exists_alias: + name: test_alias1 + + - is_true: '' + + - do: + indices.exists_alias: + name: test_alias2 + + - is_true: '' + + - do: + indices.get_alias: + index: test_index + + - match: {test_index.aliases.test_alias1: {'index_routing': 'routing_value', 'search_routing': 'routing_value'}} + - match: {test_index.aliases.test_alias2: {'index_routing': 'routing_value', 'search_routing': 'routing_value'}}