Skip to content

Commit f2db121

Browse files
authored
No index found with given index pattern should throw IndexNotFoundException (opensearch-project#4369)
* No index found with given index pattern should throw IndexNotFoundException Signed-off-by: Lantao Jin <ltjin@amazon.com> * Add UT Signed-off-by: Lantao Jin <ltjin@amazon.com> --------- Signed-off-by: Lantao Jin <ltjin@amazon.com>
1 parent 8c417f4 commit f2db121

5 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
setup:
2+
- do:
3+
query.settings:
4+
body:
5+
transient:
6+
plugins.calcite.enabled : true
7+
8+
---
9+
teardown:
10+
- do:
11+
query.settings:
12+
body:
13+
transient:
14+
plugins.calcite.enabled : false
15+
16+
---
17+
"No index found with given index pattern should throw IndexNotFoundException":
18+
- skip:
19+
features:
20+
- headers
21+
- allowed_warnings
22+
- do:
23+
indices.create:
24+
index: log-test
25+
body:
26+
mappings:
27+
properties:
28+
"@timestamp":
29+
type: date
30+
31+
- do:
32+
bulk:
33+
index: log-test
34+
refresh: true
35+
body:
36+
- '{"index": {}}'
37+
- '{ "@timestamp" : "2025-09-04T16:15:00.000Z" }'
38+
39+
- do:
40+
headers:
41+
Content-Type: 'application/json'
42+
ppl:
43+
body:
44+
query: source=log-* | fields @timestamp
45+
46+
- match: { total: 1 }
47+
- length: { datarows: 1 }
48+
49+
- do:
50+
catch: missing # without the patch #4342, it throws 'bad_request' rather 'missing'
51+
headers:
52+
Content-Type: 'application/json'
53+
ppl:
54+
body:
55+
query: source=log-abc* | fields @timestamp

opensearch/src/main/java/org/opensearch/sql/opensearch/client/OpenSearchNodeClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {
8989
try {
9090
GetMappingsResponse mappingsResponse =
9191
client.admin().indices().prepareGetMappings(indexExpression).setLocal(true).get();
92+
if (mappingsResponse.mappings().isEmpty()) {
93+
throw new IndexNotFoundException(indexExpression[0]);
94+
}
9295
return mappingsResponse.mappings().entrySet().stream()
9396
.collect(
9497
Collectors.toUnmodifiableMap(

opensearch/src/main/java/org/opensearch/sql/opensearch/client/OpenSearchRestClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.opensearch.client.indices.GetMappingsResponse;
3030
import org.opensearch.cluster.metadata.AliasMetadata;
3131
import org.opensearch.common.settings.Settings;
32+
import org.opensearch.index.IndexNotFoundException;
3233
import org.opensearch.sql.opensearch.mapping.IndexMapping;
3334
import org.opensearch.sql.opensearch.request.OpenSearchRequest;
3435
import org.opensearch.sql.opensearch.request.OpenSearchScrollRequest;
@@ -71,6 +72,9 @@ public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {
7172
GetMappingsRequest request = new GetMappingsRequest().indices(indexExpression);
7273
try {
7374
GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT);
75+
if (response.mappings().isEmpty()) {
76+
throw new IndexNotFoundException(indexExpression[0]);
77+
}
7478
return response.mappings().entrySet().stream()
7579
.collect(Collectors.toMap(Map.Entry::getKey, e -> new IndexMapping(e.getValue())));
7680
} catch (IOException e) {

opensearch/src/test/java/org/opensearch/sql/opensearch/client/OpenSearchNodeClientTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ void get_index_mappings_with_IOException() {
241241
assertThrows(IllegalStateException.class, () -> client.getIndexMappings(indexName));
242242
}
243243

244+
@Test
245+
void get_index_mappings_with_index_patterns() {
246+
mockNodeClientIndicesMappings("", null);
247+
assertThrows(IndexNotFoundException.class, () -> client.getIndexMappings("test*"));
248+
}
249+
244250
@Test
245251
void get_index_mappings_with_non_exist_index() {
246252
when(nodeClient.admin().indices().prepareGetMappings(any()).setLocal(anyBoolean()).get())
@@ -494,7 +500,9 @@ public void mockNodeClientIndicesMappings(String indexName, String mappings) {
494500
.thenReturn(mockResponse);
495501
try {
496502
Map<String, MappingMetadata> metadata;
497-
if (mappings.isEmpty()) {
503+
if (mappings == null) {
504+
metadata = Map.of();
505+
} else if (mappings.isEmpty()) {
498506
when(emptyMapping.getSourceAsMap()).thenReturn(Map.of());
499507
metadata = Map.of(indexName, emptyMapping);
500508
} else {

opensearch/src/test/java/org/opensearch/sql/opensearch/client/OpenSearchRestClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.opensearch.core.xcontent.DeprecationHandler;
6363
import org.opensearch.core.xcontent.NamedXContentRegistry;
6464
import org.opensearch.core.xcontent.XContentParser;
65+
import org.opensearch.index.IndexNotFoundException;
6566
import org.opensearch.search.SearchHit;
6667
import org.opensearch.search.SearchHits;
6768
import org.opensearch.search.builder.SearchSourceBuilder;
@@ -225,6 +226,15 @@ void get_index_mappings() throws IOException {
225226
OpenSearchTextType.of(MappingType.Long), parsedTypes.get("manager.salary")));
226227
}
227228

229+
@Test
230+
void get_index_mappings_with_index_patterns() throws IOException {
231+
GetMappingsResponse response = mock(GetMappingsResponse.class);
232+
when(response.mappings()).thenReturn(Map.of());
233+
when(restClient.indices().getMapping(any(GetMappingsRequest.class), any()))
234+
.thenReturn(response);
235+
assertThrows(IndexNotFoundException.class, () -> client.getIndexMappings("test*"));
236+
}
237+
228238
@Test
229239
void get_index_mappings_with_IOException() throws IOException {
230240
when(restClient.indices().getMapping(any(GetMappingsRequest.class), any()))

0 commit comments

Comments
 (0)