Skip to content

Commit 444b4c4

Browse files
Eunovojavanna
authored andcommitted
Improve error message for absence of indices (#39789)
"no indices exist" has been added to the error message for absence of indices
1 parent f2ca45c commit 444b4c4

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,19 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
157157
for (ExpressionResolver expressionResolver : expressionResolvers) {
158158
expressions = expressionResolver.resolve(context, expressions);
159159
}
160-
160+
161161
if (expressions.isEmpty()) {
162162
if (!options.allowNoIndices()) {
163-
IndexNotFoundException infe = new IndexNotFoundException((String)null);
163+
IndexNotFoundException infe;
164+
if (indexExpressions.length == 1) {
165+
if (indexExpressions[0].equals(MetaData.ALL)) {
166+
infe = new IndexNotFoundException("no indices exist", (String)null);
167+
} else {
168+
infe = new IndexNotFoundException((String)null);
169+
}
170+
} else {
171+
infe = new IndexNotFoundException((String)null);
172+
}
164173
infe.setResources("index_expression", indexExpressions);
165174
throw infe;
166175
} else {
@@ -173,7 +182,12 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
173182
AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression);
174183
if (aliasOrIndex == null ) {
175184
if (failNoIndices) {
176-
IndexNotFoundException infe = new IndexNotFoundException(expression);
185+
IndexNotFoundException infe;
186+
if (expression.equals(MetaData.ALL)) {
187+
infe = new IndexNotFoundException("no indices exist", expression);
188+
} else {
189+
infe = new IndexNotFoundException(expression);
190+
}
177191
infe.setResources("index_expression", expression);
178192
throw infe;
179193
} else {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,26 @@ public void testConcreteIndicesIgnoreIndicesEmptyRequest() {
580580
assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, new String[]{})),
581581
equalTo(newHashSet("kuku", "testXXX")));
582582
}
583+
public void testConcreteIndicesNoIndicesErrorMessage() {
584+
MetaData.Builder mdBuilder = MetaData.builder();
585+
ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
586+
IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state,
587+
IndicesOptions.fromOptions(false, false, true, true));
588+
IndexNotFoundException infe = expectThrows(IndexNotFoundException.class,
589+
() -> indexNameExpressionResolver.concreteIndices(context, new String[]{}));
590+
assertThat(infe.getMessage(), is("no such index [null] and no indices exist"));
591+
}
583592

593+
public void testConcreteIndicesNoIndicesErrorMessageNoExpand() {
594+
MetaData.Builder mdBuilder = MetaData.builder();
595+
ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
596+
IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state,
597+
IndicesOptions.fromOptions(false, false, false, false));
598+
IndexNotFoundException infe = expectThrows(IndexNotFoundException.class,
599+
() -> indexNameExpressionResolver.concreteIndices(context, new String[]{}));
600+
assertThat(infe.getMessage(), is("no such index [_all] and no indices exist"));
601+
}
602+
584603
public void testConcreteIndicesWildcardExpansion() {
585604
MetaData.Builder mdBuilder = MetaData.builder()
586605
.put(indexBuilder("testXXX").state(State.OPEN))

server/src/test/java/org/elasticsearch/validate/SimpleValidateQueryIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void testValidateEmptyCluster() {
160160
client().admin().indices().prepareValidateQuery().get();
161161
fail("Expected IndexNotFoundException");
162162
} catch (IndexNotFoundException e) {
163-
assertThat(e.getMessage(), is("no such index [null]"));
163+
assertThat(e.getMessage(), is("no such index [null] and no indices exist"));
164164
}
165165
}
166166

0 commit comments

Comments
 (0)