Improve error message for absence of indices (#39789)

"no indices exist" has been added to the error message for absence of indices
This commit is contained in:
Oghenovo Usiwoma 2019-03-27 08:03:09 -07:00 committed by Luca Cavanna
parent f2ca45c210
commit 444b4c4136
3 changed files with 37 additions and 4 deletions

View File

@ -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 {

View File

@ -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))

View File

@ -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"));
}
}