Skip to content

Commit a514aad

Browse files
authored
Fix/meta fields bad request (#117229)
400 rather a 5xx error is returned when _source / _seq_no / _feature / _nested_path / _field_names is requested, via fields
1 parent cbb08ba commit a514aad

File tree

10 files changed

+92
-12
lines changed

10 files changed

+92
-12
lines changed

docs/changelog/117229.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 117229
2+
summary: "In this pr, a 400 error is returned when _source / _seq_no / _feature /\
3+
\ _nested_path / _field_names is requested, rather a 5xx"
4+
area: Search
5+
type: bug
6+
issues: []

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureMetaFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String typeName() {
4848

4949
@Override
5050
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
51-
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + typeName() + "].");
51+
throw new IllegalArgumentException("Cannot fetch values for internal field [" + typeName() + "].");
5252
}
5353

5454
@Override

rest-api-spec/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ tasks.named("yamlRestCompatTestTransform").configure ({ task ->
6666
task.skipTest("logsdb/20_source_mapping/stored _source mode is supported", "no longer serialize source_mode")
6767
task.skipTest("logsdb/20_source_mapping/include/exclude is supported with stored _source", "no longer serialize source_mode")
6868
task.skipTest("logsdb/20_source_mapping/synthetic _source is default", "no longer serialize source_mode")
69+
task.skipTest("search/520_fetch_fields/fetch _seq_no via fields", "error code is changed from 5xx to 400 in 9.0")
6970
})

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search/520_fetch_fields.yml

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,88 @@ fetch _seq_no via stored_fields:
128128

129129
---
130130
fetch _seq_no via fields:
131+
- requires:
132+
cluster_features: ["meta_fetch_fields_error_code_changed"]
133+
reason: The fields_api returns a 400 instead a 5xx when _seq_no is requested via fields
131134

132135
- do:
133-
catch: "request"
136+
catch: bad_request
134137
search:
135138
index: test
136139
body:
137140
fields: [ _seq_no ]
138141

139-
# This should be `unauthorized` (401) or `forbidden` (403) or at least `bad request` (400)
140-
# while instead it is mapped to an `internal_server_error (500)`
141-
- match: { status: 500 }
142-
- match: { error.root_cause.0.type: unsupported_operation_exception }
142+
- match: { status: 400 }
143+
- match: { error.root_cause.0.type: illegal_argument_exception }
144+
- match: { error.root_cause.0.reason: "error fetching [_seq_no]: Cannot fetch values for internal field [_seq_no]." }
145+
146+
---
147+
fetch _source via fields:
148+
- requires:
149+
cluster_features: ["meta_fetch_fields_error_code_changed"]
150+
reason: The fields_api returns a 400 instead a 5xx when _seq_no is requested via fields
151+
152+
- do:
153+
catch: bad_request
154+
search:
155+
index: test
156+
body:
157+
fields: [ _source ]
158+
159+
- match: { status: 400 }
160+
- match: { error.root_cause.0.type: illegal_argument_exception }
161+
- match: { error.root_cause.0.reason: "error fetching [_source]: Cannot fetch values for internal field [_source]." }
162+
163+
---
164+
fetch _feature via fields:
165+
- requires:
166+
cluster_features: ["meta_fetch_fields_error_code_changed"]
167+
reason: The fields_api returns a 400 instead a 5xx when _seq_no is requested via fields
168+
169+
- do:
170+
catch: bad_request
171+
search:
172+
index: test
173+
body:
174+
fields: [ _feature ]
175+
176+
- match: { status: 400 }
177+
- match: { error.root_cause.0.type: illegal_argument_exception }
178+
- match: { error.root_cause.0.reason: "error fetching [_feature]: Cannot fetch values for internal field [_feature]." }
179+
180+
---
181+
fetch _nested_path via fields:
182+
- requires:
183+
cluster_features: ["meta_fetch_fields_error_code_changed"]
184+
reason: The fields_api returns a 400 instead a 5xx when _seq_no is requested via fields
185+
186+
- do:
187+
catch: bad_request
188+
search:
189+
index: test
190+
body:
191+
fields: [ _nested_path ]
192+
193+
- match: { status: 400 }
194+
- match: { error.root_cause.0.type: illegal_argument_exception }
195+
- match: { error.root_cause.0.reason: "error fetching [_nested_path]: Cannot fetch values for internal field [_nested_path]." }
196+
197+
---
198+
fetch _field_names via fields:
199+
- requires:
200+
cluster_features: ["meta_fetch_fields_error_code_changed"]
201+
reason: The fields_api returns a 400 instead a 5xx when _seq_no is requested via fields
202+
203+
- do:
204+
catch: bad_request
205+
search:
206+
index: test
207+
body:
208+
fields: [ _field_names ]
209+
210+
- match: { status: 400 }
211+
- match: { error.root_cause.0.type: illegal_argument_exception }
212+
- match: { error.root_cause.0.reason: "error fetching [_field_names]: Cannot fetch values for internal field [_field_names]." }
143213

144214
---
145215
fetch fields with none stored_fields:

server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public boolean isEnabled() {
135135

136136
@Override
137137
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
138-
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "].");
138+
throw new IllegalArgumentException("Cannot fetch values for internal field [" + name() + "].");
139139
}
140140

141141
@Override

server/src/main/java/org/elasticsearch/index/mapper/MapperFeatures.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public Set<NodeFeature> getFeatures() {
6161
"mapper.constant_keyword.synthetic_source_write_fix"
6262
);
6363

64+
public static final NodeFeature META_FETCH_FIELDS_ERROR_CODE_CHANGED = new NodeFeature("meta_fetch_fields_error_code_changed");
65+
6466
@Override
6567
public Set<NodeFeature> getTestFeatures() {
6668
return Set.of(
@@ -71,7 +73,8 @@ public Set<NodeFeature> getTestFeatures() {
7173
IgnoredSourceFieldMapper.IGNORED_SOURCE_AS_TOP_LEVEL_METADATA_ARRAY_FIELD,
7274
IgnoredSourceFieldMapper.ALWAYS_STORE_OBJECT_ARRAYS_IN_NESTED_OBJECTS,
7375
MapperService.LOGSDB_DEFAULT_IGNORE_DYNAMIC_BEYOND_LIMIT,
74-
CONSTANT_KEYWORD_SYNTHETIC_SOURCE_WRITE_FIX
76+
CONSTANT_KEYWORD_SYNTHETIC_SOURCE_WRITE_FIX,
77+
META_FETCH_FIELDS_ERROR_CODE_CHANGED
7578
);
7679
}
7780
}

server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Query existsQuery(SearchExecutionContext context) {
6767

6868
@Override
6969
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
70-
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "].");
70+
throw new IllegalArgumentException("Cannot fetch values for internal field [" + name() + "].");
7171
}
7272

7373
@Override

server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public boolean mayExistInIndex(SearchExecutionContext context) {
168168

169169
@Override
170170
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
171-
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "].");
171+
throw new IllegalArgumentException("Cannot fetch values for internal field [" + name() + "].");
172172
}
173173

174174
@Override

server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public String typeName() {
325325

326326
@Override
327327
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
328-
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "].");
328+
throw new IllegalArgumentException("Cannot fetch values for internal field [" + name() + "].");
329329
}
330330

331331
@Override

server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldFetcherTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void testMetadataFields() throws IOException {
271271
FieldNamesFieldMapper.NAME,
272272
NestedPathFieldMapper.name(IndexVersion.current())
273273
)) {
274-
expectThrows(UnsupportedOperationException.class, () -> fetchFields(mapperService, source, fieldname));
274+
expectThrows(IllegalArgumentException.class, () -> fetchFields(mapperService, source, fieldname));
275275
}
276276
}
277277

0 commit comments

Comments
 (0)