Skip to content

Commit

Permalink
CORE: Make Pattern Exclusion Work with Aliases
Browse files Browse the repository at this point in the history
* Adds the pattern exclusion logic to finding aliases
* Closes elastic#33395
  • Loading branch information
original-brownbear committed Sep 7, 2018
1 parent 42469a9 commit 576dd2d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,28 @@ private ImmutableOpenMap<String, List<AliasMetaData>> findAliases(String[] origi
return ImmutableOpenMap.of();
}

boolean matchAllAliases = matchAllAliases(aliases);
List<String> includeAliases = new ArrayList<>();
List<String> 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<String, List<AliasMetaData>> mapBuilder = ImmutableOpenMap.builder();
for (String index : concreteIndices) {
IndexMetaData indexMetaData = indices.get(index);
List<AliasMetaData> filteredValues = new ArrayList<>();
for (ObjectCursor<AliasMetaData> 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);
}
}
Expand All @@ -317,13 +331,13 @@ private ImmutableOpenMap<String, List<AliasMetaData>> findAliases(String[] origi
return mapBuilder.build();
}

private static boolean matchAllAliases(final String[] aliases) {
private static boolean matchAllAliases(final List<String> aliases) {
for (String alias : aliases) {
if (alias.equals(ALL)) {
return true;
}
}
return aliases.length == 0;
return aliases.isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<AliasMetaData>> 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))
Expand Down

0 comments on commit 576dd2d

Please sign in to comment.