diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index c3da63886140a..3ee8cf4d0ca28 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -296,14 +296,28 @@ private ImmutableOpenMap> findAliases(String[] origi return ImmutableOpenMap.of(); } - boolean matchAllAliases = matchAllAliases(aliases); + List includeAliases = new ArrayList<>(); + List excludeAliases = new ArrayList<>(); + boolean wildcardSeen = false; + for (final String alias : aliases) { + if (Regex.isSimpleMatchPattern(alias)) { + wildcardSeen = true; + } + if (wildcardSeen && alias.charAt(0) == '-') { + excludeAliases.add(alias.substring(1)); + } else { + includeAliases.add(alias); + } + } + boolean matchAllAliases = matchAllAliases(includeAliases); ImmutableOpenMap.Builder> mapBuilder = ImmutableOpenMap.builder(); for (String index : concreteIndices) { IndexMetaData indexMetaData = indices.get(index); List filteredValues = new ArrayList<>(); for (ObjectCursor cursor : indexMetaData.getAliases().values()) { AliasMetaData value = cursor.value; - if (matchAllAliases || Regex.simpleMatch(aliases, value.alias())) { + if ((matchAllAliases || Regex.simpleMatch(includeAliases, value.alias())) + && Regex.simpleMatch(excludeAliases, value.alias()) == false) { filteredValues.add(value); } } @@ -317,13 +331,13 @@ private ImmutableOpenMap> findAliases(String[] origi return mapBuilder.build(); } - private static boolean matchAllAliases(final String[] aliases) { + private static boolean matchAllAliases(final List aliases) { for (String alias : aliases) { if (alias.equals(ALL)) { return true; } } - return aliases.length == 0; + return aliases.isEmpty(); } /** diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java index 9d82e9e1cdca5..0cbd9a55893b5 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java @@ -109,6 +109,21 @@ public void testFindAliases() { } } + public void testFindAliasWithMultiIndexAndExclusion() { + MetaData metaData = MetaData.builder().put( + IndexMetaData.builder("index") + .settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(AliasMetaData.builder("alias1").build()) + .putAlias(AliasMetaData.builder("alias2").build()) + ).build(); + ImmutableOpenMap> aliases = + metaData.findAliases(new GetAliasesRequest().aliases("*", "-alias1"), new String[] {"index"}); + assertThat(aliases.size(), equalTo(1)); + assertThat(aliases.get("index").get(0).alias(), equalTo("alias2")); + } + public void testIndexAndAliasWithSameName() { IndexMetaData.Builder builder = IndexMetaData.builder("index") .settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT))