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..75869b54850d4 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,38 @@ private ImmutableOpenMap> findAliases(String[] origi return ImmutableOpenMap.of(); } - boolean matchAllAliases = matchAllAliases(aliases); + String[] patterns = new String[aliases.length]; + boolean[] include = new boolean[aliases.length]; + for (int i = 0; i < aliases.length; i++) { + String alias = aliases[i]; + if (alias.charAt(0) == '-') { + patterns[i] = alias.substring(1); + include[i] = false; + } else { + patterns[i] = alias; + include[i] = true; + } + } + boolean matchAllAliases = patterns.length == 0; 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())) { + boolean matched = matchAllAliases; + String alias = value.alias(); + for (int i = 0; i < patterns.length; i++) { + if (include[i]) { + if (matched == false) { + String pattern = patterns[i]; + matched = ALL.equals(pattern) || Regex.simpleMatch(pattern, alias); + } + } else if (matched) { + matched = Regex.simpleMatch(patterns[i], alias) == false; + } + } + if (matched) { filteredValues.add(value); } } @@ -317,15 +341,6 @@ private ImmutableOpenMap> findAliases(String[] origi return mapBuilder.build(); } - private static boolean matchAllAliases(final String[] aliases) { - for (String alias : aliases) { - if (alias.equals(ALL)) { - return true; - } - } - return aliases.length == 0; - } - /** * Checks if at least one of the specified aliases exists in the specified concrete indices. Wildcards are supported in the * alias names for partial matches. 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..da50e99705dfb 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,38 @@ public void testFindAliases() { } } + public void testFindAliasWithExclusion() { + 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(); + List aliases = + metaData.findAliases(new GetAliasesRequest().aliases("*", "-alias1"), new String[] {"index"}).get("index"); + assertThat(aliases.size(), equalTo(1)); + assertThat(aliases.get(0).alias(), equalTo("alias2")); + } + + public void testFindAliasWithExclusionAndOverride() { + 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("aa").build()) + .putAlias(AliasMetaData.builder("ab").build()) + .putAlias(AliasMetaData.builder("bb").build()) + ).build(); + List aliases = + metaData.findAliases(new GetAliasesRequest().aliases("a*", "-*b", "b*"), new String[] {"index"}).get("index"); + assertThat(aliases.size(), equalTo(2)); + assertThat(aliases.get(0).alias(), equalTo("aa")); + assertThat(aliases.get(1).alias(), equalTo("bb")); + } + public void testIndexAndAliasWithSameName() { IndexMetaData.Builder builder = IndexMetaData.builder("index") .settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT))