Skip to content

Commit 3f23340

Browse files
dakronejavanna
authored andcommitted
Only negate index expression on all indices with preceding wildcard
There is currently a very confusing behavior in Elasticsearch for the following: Given the indices: `[test1, test2, -foo1, -foo2]` ``` DELETE /-foo* ``` Will cause the `test1` and `test2` indices to be deleted, when what is usually intended is to delete the `-foo1` and `-foo2` indices. Previously we added a change in elastic#20033 to disallow creating indices starting with `-` or `+`, which will help with this situation. However, users may have existing indices starting with these characters. This changes the negation to only take effect in a wildcard (`*`) has been seen somewhere in the expression, so in order to delete `-foo1` and `-foo2` the following now works: ``` DELETE /-foo* ``` As well as: ``` DELETE /-foo1,-foo2 ``` so in order to actually delete everything except for the "foo" indices (ie, `test1` and `test2`) a user would now issue: ``` DELETE /*,--foo* ``` Relates to elastic#19800
1 parent 289a69b commit 3f23340

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

core/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ public List<String> resolve(Context context, List<String> expressions) {
579579

580580
private Set<String> innerResolve(Context context, List<String> expressions, IndicesOptions options, MetaData metaData) {
581581
Set<String> result = null;
582+
boolean wildcardSeen = false;
582583
for (int i = 0; i < expressions.size(); i++) {
583584
String expression = expressions.get(i);
584585
if (aliasOrIndexExists(metaData, expression)) {
@@ -598,13 +599,14 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
598599
}
599600
expression = expression.substring(1);
600601
} else if (expression.charAt(0) == '-') {
601-
// if its the first, fill it with all the indices...
602-
if (i == 0) {
603-
List<String> concreteIndices = resolveEmptyOrTrivialWildcard(options, metaData, false);
604-
result = new HashSet<>(concreteIndices);
602+
// if there is a negation without a wildcard being previously seen, add it verbatim,
603+
// otherwise return the expression
604+
if (wildcardSeen) {
605+
add = false;
606+
expression = expression.substring(1);
607+
} else {
608+
add = true;
605609
}
606-
add = false;
607-
expression = expression.substring(1);
608610
}
609611
if (result == null) {
610612
// add all the previous ones...
@@ -634,6 +636,10 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
634636
if (!noIndicesAllowedOrMatches(options, matches)) {
635637
throw infe(expression);
636638
}
639+
640+
if (expression.contains("*")) {
641+
wildcardSeen = true;
642+
}
637643
}
638644
return result;
639645
}

core/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void testIndexOptionsWildcardExpansion() {
305305
assertEquals(1, results.length);
306306
assertEquals("bar", results[0]);
307307

308-
results = indexNameExpressionResolver.concreteIndexNames(context, "-foo*");
308+
results = indexNameExpressionResolver.concreteIndexNames(context, "*", "-foo*");
309309
assertEquals(1, results.length);
310310
assertEquals("bar", results[0]);
311311

@@ -585,6 +585,35 @@ public void testConcreteIndicesWildcardExpansion() {
585585
assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, "testX*")), equalTo(newHashSet("testXXX", "testXXY", "testXYY")));
586586
}
587587

588+
public void testConcreteIndicesWildcardWithNegation() {
589+
MetaData.Builder mdBuilder = MetaData.builder()
590+
.put(indexBuilder("testXXX").state(State.OPEN))
591+
.put(indexBuilder("testXXY").state(State.OPEN))
592+
.put(indexBuilder("testXYY").state(State.OPEN))
593+
.put(indexBuilder("-testXYZ").state(State.OPEN))
594+
.put(indexBuilder("-testXZZ").state(State.OPEN))
595+
.put(indexBuilder("testYYY").state(State.OPEN))
596+
.put(indexBuilder("testYYX").state(State.OPEN));
597+
ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
598+
599+
IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state,
600+
IndicesOptions.fromOptions(true, true, true, true));
601+
assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, "testX*")),
602+
equalTo(newHashSet("testXXX", "testXXY", "testXYY")));
603+
604+
assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, "test*", "-testX*")),
605+
equalTo(newHashSet("testYYY", "testYYX")));
606+
607+
assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, "-testX*")),
608+
equalTo(newHashSet("-testXYZ", "-testXZZ")));
609+
610+
assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, "testXXY", "-testX*")),
611+
equalTo(newHashSet("testXXY", "-testXYZ", "-testXZZ")));
612+
613+
assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, "*", "--testX*")),
614+
equalTo(newHashSet("testXXX", "testXXY", "testXYY", "testYYX", "testYYY")));
615+
}
616+
588617
/**
589618
* test resolving _all pattern (null, empty array or "_all") for random IndicesOptions
590619
*/

0 commit comments

Comments
 (0)