Skip to content

Commit 626ff21

Browse files
committed
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 77c5526 commit 626ff21

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,25 @@ setup:
9595
---
9696
"Get test-* with wildcard_expansion=none":
9797
- skip:
98-
version: " - 5.99.99"
99-
reason: this was a breaking change in 6.0
98+
version: " - 6.3.99"
99+
reason: allow_no_indices (defaults to true) was ignored in previous versions
100+
- do:
101+
indices.get_mapping:
102+
index: test-x*
103+
expand_wildcards: none
100104

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
101111
- do:
102112
catch: missing
103113
indices.get_mapping:
104114
index: test-x*
105115
expand_wildcards: none
106-
116+
allow_no_indices: false
107117
---
108118
"Get test-* with wildcard_expansion=open,closed":
109119

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

Lines changed: 3 additions & 9 deletions
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;
@@ -88,14 +87,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
8887
@Override
8988
public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {
9089
final ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
91-
if (mappingsByIndex.isEmpty() && (indices.length != 0 || types.length != 0)) {
92-
if (indices.length != 0 && types.length == 0) {
93-
builder.close();
94-
return new BytesRestResponse(channel, new IndexNotFoundException(String.join(",", indices)));
95-
} else {
96-
builder.close();
97-
return new BytesRestResponse(channel, new TypeMissingException("_all", String.join(",", types)));
98-
}
90+
if (mappingsByIndex.isEmpty() && types.length != 0) {
91+
builder.close();
92+
return new BytesRestResponse(channel, new TypeMissingException("_all", String.join(",", types)));
9993
}
10094

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

0 commit comments

Comments
 (0)