CORE: Make Pattern Exclusion Work with Aliases (#33518)

* CORE: Make Pattern Exclusion Work with Aliases

* Adds the pattern exclusion logic to finding aliases
* Closes #33395
This commit is contained in:
Armin Braun 2018-09-09 17:31:02 +02:00 committed by GitHub
parent 6eca627409
commit d4b212c4c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 11 deletions

View File

@ -296,14 +296,38 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, To
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<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())) {
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 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, To
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.

View File

@ -109,6 +109,38 @@ public class MetaDataTests extends ESTestCase {
}
}
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<AliasMetaData> 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<AliasMetaData> 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))