From 444b4c41364182d608226359b9780430c6de3ab8 Mon Sep 17 00:00:00 2001 From: Oghenovo Usiwoma <37949128+Eunovo@users.noreply.github.com> Date: Wed, 27 Mar 2019 08:03:09 -0700 Subject: [PATCH] Improve error message for absence of indices (#39789) "no indices exist" has been added to the error message for absence of indices --- .../metadata/IndexNameExpressionResolver.java | 20 ++++++++++++++++--- .../IndexNameExpressionResolverTests.java | 19 ++++++++++++++++++ .../validate/SimpleValidateQueryIT.java | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java index 050d97ba54c..03fa790a871 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -157,10 +157,19 @@ public class IndexNameExpressionResolver { for (ExpressionResolver expressionResolver : expressionResolvers) { expressions = expressionResolver.resolve(context, expressions); } - + if (expressions.isEmpty()) { if (!options.allowNoIndices()) { - IndexNotFoundException infe = new IndexNotFoundException((String)null); + IndexNotFoundException infe; + if (indexExpressions.length == 1) { + if (indexExpressions[0].equals(MetaData.ALL)) { + infe = new IndexNotFoundException("no indices exist", (String)null); + } else { + infe = new IndexNotFoundException((String)null); + } + } else { + infe = new IndexNotFoundException((String)null); + } infe.setResources("index_expression", indexExpressions); throw infe; } else { @@ -173,7 +182,12 @@ public class IndexNameExpressionResolver { AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression); if (aliasOrIndex == null ) { if (failNoIndices) { - IndexNotFoundException infe = new IndexNotFoundException(expression); + IndexNotFoundException infe; + if (expression.equals(MetaData.ALL)) { + infe = new IndexNotFoundException("no indices exist", expression); + } else { + infe = new IndexNotFoundException(expression); + } infe.setResources("index_expression", expression); throw infe; } else { diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java index 571843126f9..228d05c51c4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java @@ -580,7 +580,26 @@ public class IndexNameExpressionResolverTests extends ESTestCase { assertThat(newHashSet(indexNameExpressionResolver.concreteIndexNames(context, new String[]{})), equalTo(newHashSet("kuku", "testXXX"))); } + public void testConcreteIndicesNoIndicesErrorMessage() { + MetaData.Builder mdBuilder = MetaData.builder(); + ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build(); + IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, + IndicesOptions.fromOptions(false, false, true, true)); + IndexNotFoundException infe = expectThrows(IndexNotFoundException.class, + () -> indexNameExpressionResolver.concreteIndices(context, new String[]{})); + assertThat(infe.getMessage(), is("no such index [null] and no indices exist")); + } + public void testConcreteIndicesNoIndicesErrorMessageNoExpand() { + MetaData.Builder mdBuilder = MetaData.builder(); + ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build(); + IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, + IndicesOptions.fromOptions(false, false, false, false)); + IndexNotFoundException infe = expectThrows(IndexNotFoundException.class, + () -> indexNameExpressionResolver.concreteIndices(context, new String[]{})); + assertThat(infe.getMessage(), is("no such index [_all] and no indices exist")); + } + public void testConcreteIndicesWildcardExpansion() { MetaData.Builder mdBuilder = MetaData.builder() .put(indexBuilder("testXXX").state(State.OPEN)) diff --git a/server/src/test/java/org/elasticsearch/validate/SimpleValidateQueryIT.java b/server/src/test/java/org/elasticsearch/validate/SimpleValidateQueryIT.java index d9f25a369d6..54d9a015b4e 100644 --- a/server/src/test/java/org/elasticsearch/validate/SimpleValidateQueryIT.java +++ b/server/src/test/java/org/elasticsearch/validate/SimpleValidateQueryIT.java @@ -160,7 +160,7 @@ public class SimpleValidateQueryIT extends ESIntegTestCase { client().admin().indices().prepareValidateQuery().get(); fail("Expected IndexNotFoundException"); } catch (IndexNotFoundException e) { - assertThat(e.getMessage(), is("no such index [null]")); + assertThat(e.getMessage(), is("no such index [null] and no indices exist")); } }