Skip to content

Commit 858b2c7

Browse files
Standardize underscore requirements in parameters (#27414)
Stardardize underscore requirements in parameters across different type of requests: _index, _type, _source, _id keep their underscores params like version and retry_on_conflict will be without underscores Throw an error if older versions of parameters are used BulkRequest, MultiGetRequest, TermVectorcRequest, MoreLikeThisQuery were changed Closes #26886
1 parent a5df2ef commit 858b2c7

File tree

15 files changed

+329
-145
lines changed

15 files changed

+329
-145
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,23 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
191191
metadata.field("_id", request.id());
192192
}
193193
if (Strings.hasLength(request.routing())) {
194-
metadata.field("_routing", request.routing());
194+
metadata.field("routing", request.routing());
195195
}
196196
if (Strings.hasLength(request.parent())) {
197-
metadata.field("_parent", request.parent());
197+
metadata.field("parent", request.parent());
198198
}
199199
if (request.version() != Versions.MATCH_ANY) {
200-
metadata.field("_version", request.version());
200+
metadata.field("version", request.version());
201201
}
202202

203203
VersionType versionType = request.versionType();
204204
if (versionType != VersionType.INTERNAL) {
205205
if (versionType == VersionType.EXTERNAL) {
206-
metadata.field("_version_type", "external");
206+
metadata.field("version_type", "external");
207207
} else if (versionType == VersionType.EXTERNAL_GTE) {
208-
metadata.field("_version_type", "external_gte");
208+
metadata.field("version_type", "external_gte");
209209
} else if (versionType == VersionType.FORCE) {
210-
metadata.field("_version_type", "force");
210+
metadata.field("version_type", "force");
211211
}
212212
}
213213

@@ -219,7 +219,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
219219
} else if (opType == DocWriteRequest.OpType.UPDATE) {
220220
UpdateRequest updateRequest = (UpdateRequest) request;
221221
if (updateRequest.retryOnConflict() > 0) {
222-
metadata.field("_retry_on_conflict", updateRequest.retryOnConflict());
222+
metadata.field("retry_on_conflict", updateRequest.retryOnConflict());
223223
}
224224
if (updateRequest.fetchSource() != null) {
225225
metadata.field("_source", updateRequest.fetchSource());

core/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

+28-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.action.support.replication.ReplicationRequest;
3131
import org.elasticsearch.action.update.UpdateRequest;
3232
import org.elasticsearch.common.Nullable;
33+
import org.elasticsearch.common.ParseField;
3334
import org.elasticsearch.common.Strings;
3435
import org.elasticsearch.common.bytes.BytesArray;
3536
import org.elasticsearch.common.bytes.BytesReference;
@@ -68,6 +69,19 @@ public class BulkRequest extends ActionRequest implements CompositeIndicesReques
6869

6970
private static final int REQUEST_OVERHEAD = 50;
7071

72+
private static final ParseField INDEX = new ParseField("_index");
73+
private static final ParseField TYPE = new ParseField("_type");
74+
private static final ParseField ID = new ParseField("_id");
75+
private static final ParseField ROUTING = new ParseField("routing");
76+
private static final ParseField PARENT = new ParseField("parent");
77+
private static final ParseField OP_TYPE = new ParseField("op_type");
78+
private static final ParseField VERSION = new ParseField("version");
79+
private static final ParseField VERSION_TYPE = new ParseField("version_type");
80+
private static final ParseField RETRY_ON_CONFLICT = new ParseField("retry_on_conflict");
81+
private static final ParseField PIPELINE = new ParseField("pipeline");
82+
private static final ParseField FIELDS = new ParseField("fields");
83+
private static final ParseField SOURCE = new ParseField("_source");
84+
7185
/**
7286
* Requests that are part of this request. It is only possible to add things that are both {@link ActionRequest}s and
7387
* {@link WriteRequest}s to this but java doesn't support syntax to declare that everything in the array has both types so we declare
@@ -334,45 +348,45 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
334348
if (token == XContentParser.Token.FIELD_NAME) {
335349
currentFieldName = parser.currentName();
336350
} else if (token.isValue()) {
337-
if ("_index".equals(currentFieldName)) {
351+
if (INDEX.match(currentFieldName)){
338352
if (!allowExplicitIndex) {
339353
throw new IllegalArgumentException("explicit index in bulk is not allowed");
340354
}
341355
index = parser.text();
342-
} else if ("_type".equals(currentFieldName)) {
356+
} else if (TYPE.match(currentFieldName)) {
343357
type = parser.text();
344-
} else if ("_id".equals(currentFieldName)) {
358+
} else if (ID.match(currentFieldName)) {
345359
id = parser.text();
346-
} else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
360+
} else if (ROUTING.match(currentFieldName)) {
347361
routing = parser.text();
348-
} else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
362+
} else if (PARENT.match(currentFieldName)) {
349363
parent = parser.text();
350-
} else if ("op_type".equals(currentFieldName) || "opType".equals(currentFieldName)) {
364+
} else if (OP_TYPE.match(currentFieldName)) {
351365
opType = parser.text();
352-
} else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
366+
} else if (VERSION.match(currentFieldName)) {
353367
version = parser.longValue();
354-
} else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
368+
} else if (VERSION_TYPE.match(currentFieldName)) {
355369
versionType = VersionType.fromString(parser.text());
356-
} else if ("_retry_on_conflict".equals(currentFieldName) || "_retryOnConflict".equals(currentFieldName)) {
370+
} else if (RETRY_ON_CONFLICT.match(currentFieldName)) {
357371
retryOnConflict = parser.intValue();
358-
} else if ("pipeline".equals(currentFieldName)) {
372+
} else if (PIPELINE.match(currentFieldName)) {
359373
pipeline = parser.text();
360-
} else if ("fields".equals(currentFieldName)) {
374+
} else if (FIELDS.match(currentFieldName)) {
361375
throw new IllegalArgumentException("Action/metadata line [" + line + "] contains a simple value for parameter [fields] while a list is expected");
362-
} else if ("_source".equals(currentFieldName)) {
376+
} else if (SOURCE.match(currentFieldName)) {
363377
fetchSourceContext = FetchSourceContext.fromXContent(parser);
364378
} else {
365379
throw new IllegalArgumentException("Action/metadata line [" + line + "] contains an unknown parameter [" + currentFieldName + "]");
366380
}
367381
} else if (token == XContentParser.Token.START_ARRAY) {
368-
if ("fields".equals(currentFieldName)) {
382+
if (FIELDS.match(currentFieldName)) {
369383
DEPRECATION_LOGGER.deprecated("Deprecated field [fields] used, expected [_source] instead");
370384
List<Object> values = parser.list();
371385
fields = values.toArray(new String[values.size()]);
372386
} else {
373387
throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected a simple value for field [" + currentFieldName + "] but found [" + token + "]");
374388
}
375-
} else if (token == XContentParser.Token.START_OBJECT && "_source".equals(currentFieldName)) {
389+
} else if (token == XContentParser.Token.START_OBJECT && SOURCE.match(currentFieldName)) {
376390
fetchSourceContext = FetchSourceContext.fromXContent(parser);
377391
} else if (token != XContentParser.Token.VALUE_NULL) {
378392
throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected a simple value for field [" + currentFieldName + "] but found [" + token + "]");

core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java

+28-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.action.ValidateActions;
2929
import org.elasticsearch.action.support.IndicesOptions;
3030
import org.elasticsearch.common.Nullable;
31+
import org.elasticsearch.common.ParseField;
3132
import org.elasticsearch.common.ParsingException;
3233
import org.elasticsearch.common.Strings;
3334
import org.elasticsearch.common.io.stream.StreamInput;
@@ -48,6 +49,17 @@
4849

4950
public class MultiGetRequest extends ActionRequest implements Iterable<MultiGetRequest.Item>, CompositeIndicesRequest, RealtimeRequest {
5051

52+
private static final ParseField INDEX = new ParseField("_index");
53+
private static final ParseField TYPE = new ParseField("_type");
54+
private static final ParseField ID = new ParseField("_id");
55+
private static final ParseField ROUTING = new ParseField("routing");
56+
private static final ParseField PARENT = new ParseField("parent");
57+
private static final ParseField VERSION = new ParseField("version");
58+
private static final ParseField VERSION_TYPE = new ParseField("version_type");
59+
private static final ParseField FIELDS = new ParseField("fields");
60+
private static final ParseField STORED_FIELDS = new ParseField("stored_fields");
61+
private static final ParseField SOURCE = new ParseField("_source");
62+
5163
/**
5264
* A single get item.
5365
*/
@@ -379,30 +391,30 @@ public static void parseDocuments(XContentParser parser, List<Item> items, @Null
379391
if (token == XContentParser.Token.FIELD_NAME) {
380392
currentFieldName = parser.currentName();
381393
} else if (token.isValue()) {
382-
if ("_index".equals(currentFieldName)) {
394+
if (INDEX.match(currentFieldName)) {
383395
if (!allowExplicitIndex) {
384396
throw new IllegalArgumentException("explicit index in multi get is not allowed");
385397
}
386398
index = parser.text();
387-
} else if ("_type".equals(currentFieldName)) {
399+
} else if (TYPE.match(currentFieldName)) {
388400
type = parser.text();
389-
} else if ("_id".equals(currentFieldName)) {
401+
} else if (ID.match(currentFieldName)) {
390402
id = parser.text();
391-
} else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
403+
} else if (ROUTING.match(currentFieldName)) {
392404
routing = parser.text();
393-
} else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
405+
} else if (PARENT.match(currentFieldName)) {
394406
parent = parser.text();
395-
} else if ("fields".equals(currentFieldName)) {
407+
} else if (FIELDS.match(currentFieldName)) {
396408
throw new ParsingException(parser.getTokenLocation(),
397409
"Unsupported field [fields] used, expected [stored_fields] instead");
398-
} else if ("stored_fields".equals(currentFieldName)) {
410+
} else if (STORED_FIELDS.match(currentFieldName)) {
399411
storedFields = new ArrayList<>();
400412
storedFields.add(parser.text());
401-
} else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
413+
} else if (VERSION.match(currentFieldName)) {
402414
version = parser.longValue();
403-
} else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
415+
} else if (VERSION_TYPE.match(currentFieldName)) {
404416
versionType = VersionType.fromString(parser.text());
405-
} else if ("_source".equals(currentFieldName)) {
417+
} else if (SOURCE.match(currentFieldName)) {
406418
// check lenient to avoid interpreting the value as string but parse strict in order to provoke an error early on.
407419
if (parser.isBooleanValueLenient()) {
408420
fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(),
@@ -413,17 +425,19 @@ public static void parseDocuments(XContentParser parser, List<Item> items, @Null
413425
} else {
414426
throw new ElasticsearchParseException("illegal type for _source: [{}]", token);
415427
}
428+
} else {
429+
throw new ElasticsearchParseException("failed to parse multi get request. unknown field [{}]", currentFieldName);
416430
}
417431
} else if (token == XContentParser.Token.START_ARRAY) {
418-
if ("fields".equals(currentFieldName)) {
432+
if (FIELDS.match(currentFieldName)) {
419433
throw new ParsingException(parser.getTokenLocation(),
420434
"Unsupported field [fields] used, expected [stored_fields] instead");
421-
} else if ("stored_fields".equals(currentFieldName)) {
435+
} else if (STORED_FIELDS.match(currentFieldName)) {
422436
storedFields = new ArrayList<>();
423437
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
424438
storedFields.add(parser.text());
425439
}
426-
} else if ("_source".equals(currentFieldName)) {
440+
} else if (SOURCE.match(currentFieldName)) {
427441
ArrayList<String> includes = new ArrayList<>();
428442
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
429443
includes.add(parser.text());
@@ -433,7 +447,7 @@ public static void parseDocuments(XContentParser parser, List<Item> items, @Null
433447
}
434448

435449
} else if (token == XContentParser.Token.START_OBJECT) {
436-
if ("_source".equals(currentFieldName)) {
450+
if (SOURCE.match(currentFieldName)) {
437451
List<String> currentList = null, includes = null, excludes = null;
438452

439453
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {

0 commit comments

Comments
 (0)