diff --git a/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/EsqlRestValidationTestCase.java b/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/EsqlRestValidationTestCase.java index 9ec4f60f4c843..450ef59994349 100644 --- a/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/EsqlRestValidationTestCase.java +++ b/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/EsqlRestValidationTestCase.java @@ -73,21 +73,23 @@ public void wipeTestData() throws IOException { } } - private String getInexistentIndexErrorMessage() { - return "\"reason\" : \"Found 1 problem\\nline 1:1: Unknown index "; - } - public void testInexistentIndexNameWithWildcard() throws IOException { - assertErrorMessages(inexistentIndexNameWithWildcard, getInexistentIndexErrorMessage(), 400); + for (String indexName : inexistentIndexNameWithWildcard) { + assertErrorMessage(indexName, "Unknown index " + "[" + clusterSpecificIndexName(indexName) + "]", 400); + } } + @AwaitsFix(bugUrl = "TBD") + // field caps report only first missing index public void testInexistentIndexNameWithoutWildcard() throws IOException { - assertErrorMessages(inexistentIndexNameWithoutWildcard, getInexistentIndexErrorMessage(), 400); + for (String indexName : inexistentIndexNameWithoutWildcard) { + assertErrorMessage(indexName, "Unknown index " + "[" + clusterSpecificIndexName(indexName) + "]", 400); + } } public void testExistentIndexWithoutWildcard() throws IOException { for (String indexName : existentIndexWithoutWildcard) { - assertErrorMessage(indexName, "\"reason\" : \"no such index [inexistent]\"", 404); + assertErrorMessage(indexName, "Unknown index [inexistent]", 400); } } @@ -99,19 +101,13 @@ public void testAlias() throws IOException { createAlias(); for (String indexName : existentAliasWithoutWildcard) { - assertErrorMessage(indexName, "\"reason\" : \"no such index [inexistent]\"", 404); + assertErrorMessage(indexName, "Unknown index [inexistent]", 400); } assertValidRequestOnIndices(existentAliasWithWildcard); deleteAlias(); } - private void assertErrorMessages(String[] indices, String errorMessage, int statusCode) throws IOException { - for (String indexName : indices) { - assertErrorMessage(indexName, errorMessage + "[" + clusterSpecificIndexName(indexName) + "]", statusCode); - } - } - protected String clusterSpecificIndexName(String indexName) { return indexName; } diff --git a/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/RequestIndexFilteringTestCase.java b/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/RequestIndexFilteringTestCase.java index 712f1d874e663..034c702e47587 100644 --- a/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/RequestIndexFilteringTestCase.java +++ b/x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/RequestIndexFilteringTestCase.java @@ -212,9 +212,9 @@ public void testIndicesDontExist() throws IOException { assertThat(e.getMessage(), anyOf(containsString("Unknown index [foo*]"), containsString("Unknown index [remote_cluster:foo*]"))); e = expectThrows(ResponseException.class, () -> runEsql(timestampFilter("gte", "2020-01-01").query("FROM foo, test1"))); - assertEquals(404, e.getResponse().getStatusLine().getStatusCode()); - assertThat(e.getMessage(), containsString("index_not_found_exception")); - assertThat(e.getMessage(), containsString("no such index [foo]")); + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + assertThat(e.getMessage(), containsString("verification_exception")); + assertThat(e.getMessage(), containsString("Unknown index [foo]")); // Don't test remote patterns here, we'll test them in the multi-cluster tests if (EsqlCapabilities.Cap.JOIN_LOOKUP_V12.isEnabled()) { diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/IndexResolutionIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/IndexResolutionIT.java index c711a99344102..c08481b262e5c 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/IndexResolutionIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/IndexResolutionIT.java @@ -17,7 +17,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.datastreams.DataStreamsPlugin; -import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.xpack.esql.VerificationException; import org.elasticsearch.xpack.esql.action.AbstractEsqlIntegTestCase; @@ -200,22 +199,18 @@ public void testUnavailableIndex() { } public void testPartialResolution() { - assertAcked(client().admin().indices().prepareCreate("index-1")); - assertAcked(client().admin().indices().prepareCreate("index-2")); - indexRandom(true, "index-2", 10); + assertAcked(client().admin().indices().prepareCreate("data")); + indexRandom(true, "data", 10); - try (var response = run(syncEsqlQueryRequest().query("FROM index-1,nonexisting-1"))) { - assertOk(response); // okay when present index is empty - } expectThrows( - IndexNotFoundException.class, - equalTo("no such index [nonexisting-1]"), // fails when present index is non-empty - () -> run(syncEsqlQueryRequest().query("FROM index-2,nonexisting-1")) + VerificationException.class, + containsString("Unknown index [nonexisting-1]"), + () -> run(syncEsqlQueryRequest().query("FROM data,nonexisting-1")) ); expectThrows( - IndexNotFoundException.class, - equalTo("no such index [nonexisting-1]"), // only the first missing index is reported - () -> run(syncEsqlQueryRequest().query("FROM index-2,nonexisting-1,nonexisting-2")) + VerificationException.class, + containsString("Unknown index [nonexisting-1]"), // only the first missing index is reported + () -> run(syncEsqlQueryRequest().query("FROM data,nonexisting-1,nonexisting-2")) ); } diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/IndexResolver.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/IndexResolver.java index ff042c5e7d870..a5017d33746a3 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/IndexResolver.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/IndexResolver.java @@ -6,6 +6,7 @@ */ package org.elasticsearch.xpack.esql.session; +import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesIndexResponse; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; @@ -16,6 +17,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.Maps; import org.elasticsearch.index.IndexMode; +import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.mapper.TimeSeriesParams; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.threadpool.ThreadPool; @@ -56,7 +58,7 @@ public class IndexResolver { public static final String UNMAPPED = "unmapped"; public static final IndicesOptions FIELD_CAPS_INDICES_OPTIONS = IndicesOptions.builder() - .concreteTargetOptions(IndicesOptions.ConcreteTargetOptions.ALLOW_UNAVAILABLE_TARGETS) + .concreteTargetOptions(IndicesOptions.ConcreteTargetOptions.ERROR_WHEN_UNAVAILABLE_TARGETS) .wildcardOptions( IndicesOptions.WildcardOptions.builder() .matchOpen(true) @@ -91,10 +93,19 @@ public void resolveAsMergedMapping( client.execute( EsqlResolveFieldsAction.TYPE, createFieldCapsRequest(indexWildcard, fieldNames, requestFilter, includeAllDimensions), - listener.delegateFailureAndWrap( - (l, response) -> l.onResponse( - mergedMappings(indexWildcard, new FieldsInfo(response, supportsAggregateMetricDouble, supportsDenseVector)) - ) + ActionListener.wrap( + r -> listener.onResponse( + mergedMappings(indexWildcard, new FieldsInfo(r, supportsAggregateMetricDouble, supportsDenseVector)) + ), + f -> { + if (f instanceof IndexNotFoundException e) { + listener.onResponse(IndexResolution.notFound(e.getIndex().getName())); + } else if (f instanceof ElasticsearchSecurityException e) { + listener.onResponse(IndexResolution.notFound(indexWildcard)); + } else { + listener.onFailure(f); + } + } ) ); }