Skip to content

Commit 0352d88

Browse files
authored
Get Mapping API to honour allow_no_indices and ignore_unavailable (#31507)
Get Mapping currently throws index not found exception (and returns 404 status code) from the REST layer whenever an index was specified and no indices have been returned. We should not have this logic in the REST layer though as only our index resolver should decide whether we need to throw exceptions or not based on provided indices and corresponding indices options. Closes #31485
1 parent 009ae48 commit 0352d88

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/30_missing_index.yml

+21
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,24 @@
1313
indices.get_mapping:
1414
index: test_index
1515

16+
---
17+
"Index missing, ignore_unavailable=true":
18+
- skip:
19+
version: " - 6.99.99"
20+
reason: ignore_unavailable was ignored in previous versions
21+
- do:
22+
indices.get_mapping:
23+
index: test_index
24+
ignore_unavailable: true
25+
26+
- match: { '': {} }
27+
28+
---
29+
"Index missing, ignore_unavailable=true, allow_no_indices=false":
30+
- do:
31+
catch: missing
32+
indices.get_mapping:
33+
index: test_index
34+
ignore_unavailable: true
35+
allow_no_indices: false
36+

rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_mapping/50_wildcard_expansion.yml

+15-1
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,26 @@ setup:
9494

9595
---
9696
"Get test-* with wildcard_expansion=none":
97+
- skip:
98+
version: " - 6.99.99"
99+
reason: allow_no_indices (defaults to true) was ignored in previous versions
97100
- do:
98-
catch: missing
99101
indices.get_mapping:
100102
index: test-x*
101103
expand_wildcards: none
102104

105+
- match: { '': {} }
106+
---
107+
"Get test-* with wildcard_expansion=none allow_no_indices=false":
108+
- skip:
109+
version: " - 6.99.99"
110+
reason: allow_no_indices was ignored in previous versions
111+
- do:
112+
catch: missing
113+
indices.get_mapping:
114+
index: test-x*
115+
expand_wildcards: none
116+
allow_no_indices: false
103117
---
104118
"Get test-* with wildcard_expansion=open,closed":
105119

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingAction.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.elasticsearch.common.util.set.Sets;
3333
import org.elasticsearch.common.xcontent.ToXContent;
3434
import org.elasticsearch.common.xcontent.XContentBuilder;
35-
import org.elasticsearch.index.IndexNotFoundException;
3635
import org.elasticsearch.indices.TypeMissingException;
3736
import org.elasticsearch.rest.BaseRestHandler;
3837
import org.elasticsearch.rest.BytesRestResponse;
@@ -89,14 +88,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
8988
@Override
9089
public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {
9190
final ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
92-
if (mappingsByIndex.isEmpty() && (indices.length != 0 || types.length != 0)) {
93-
if (indices.length != 0 && types.length == 0) {
94-
builder.close();
95-
return new BytesRestResponse(channel, new IndexNotFoundException(String.join(",", indices)));
96-
} else {
97-
builder.close();
98-
return new BytesRestResponse(channel, new TypeMissingException("_all", String.join(",", types)));
99-
}
91+
if (mappingsByIndex.isEmpty() && types.length != 0) {
92+
builder.close();
93+
return new BytesRestResponse(channel, new TypeMissingException("_all", String.join(",", types)));
10094
}
10195

10296
final Set<String> typeNames = new HashSet<>();

0 commit comments

Comments
 (0)