Skip to content

Commit 9eddd2b

Browse files
authored
[7.x] Add prefer_v2_templates flag and index setting (#55411) (#55476)
This commit adds a new querystring parameter on the following APIs: - Index - Update - Bulk - Create Index - Rollover These APIs now support a `?prefer_v2_templates=true|false` flag. This flag changes the preference creation to use either V2 index templates or V1 templates. This flag defaults to `false` and will be changed to `true` for 8.0+ in subsequent work. Additionally, setting this flag internally sets the `index.prefer_v2_templates` index-level setting. This setting is used so that actions that automatically create a new index (things like rollover initiated by ILM) will inherit the preference from the original index. This setting is dynamic so that a transition from v1 to v2 templates can occur for long-running indices grouped by an alias performing periodic rollover. This also adds support for sending this parameter to the High Level Rest Client. Relates to #53101
1 parent a0763d9 commit 9eddd2b

37 files changed

+530
-50
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep
116116
parameters.withTimeout(createIndexRequest.timeout());
117117
parameters.withMasterTimeout(createIndexRequest.masterNodeTimeout());
118118
parameters.withWaitForActiveShards(createIndexRequest.waitForActiveShards());
119+
if (createIndexRequest.preferV2Templates() != null) {
120+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(createIndexRequest.preferV2Templates()));
121+
}
119122
request.addParameters(parameters.asMap());
120123
request.setEntity(RequestConverters.createEntity(createIndexRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
121124
return request;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.elasticsearch.client.security.RefreshPolicy;
5757
import org.elasticsearch.client.tasks.TaskId;
5858
import org.elasticsearch.cluster.health.ClusterHealthStatus;
59+
import org.elasticsearch.cluster.metadata.IndexMetadata;
5960
import org.elasticsearch.common.Nullable;
6061
import org.elasticsearch.common.Priority;
6162
import org.elasticsearch.common.Strings;
@@ -132,6 +133,9 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
132133
parameters.withRefreshPolicy(bulkRequest.getRefreshPolicy());
133134
parameters.withPipeline(bulkRequest.pipeline());
134135
parameters.withRouting(bulkRequest.routing());
136+
if (bulkRequest.preferV2Templates() != null) {
137+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(bulkRequest.preferV2Templates()));
138+
}
135139
// Bulk API only supports newline delimited JSON or Smile. Before executing
136140
// the bulk, we need to check that all requests have the same content-type
137141
// and this content-type is supported by the Bulk API.
@@ -345,6 +349,9 @@ static Request index(IndexRequest indexRequest) {
345349
parameters.withPipeline(indexRequest.getPipeline());
346350
parameters.withRefreshPolicy(indexRequest.getRefreshPolicy());
347351
parameters.withWaitForActiveShards(indexRequest.waitForActiveShards());
352+
if (indexRequest.preferV2Templates() != null) {
353+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(indexRequest.preferV2Templates()));
354+
}
348355

349356
BytesRef source = indexRequest.source().toBytesRef();
350357
ContentType contentType = createContentType(indexRequest.getContentType());
@@ -373,6 +380,9 @@ static Request update(UpdateRequest updateRequest) throws IOException {
373380
parameters.withRetryOnConflict(updateRequest.retryOnConflict());
374381
parameters.withVersion(updateRequest.version());
375382
parameters.withVersionType(updateRequest.versionType());
383+
if (updateRequest.preferV2Templates() != null) {
384+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(updateRequest.preferV2Templates()));
385+
}
376386

377387
// The Java API allows update requests with different content types
378388
// set for the partial document and the upsert document. This client

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.action.support.ActiveShardCount;
2626
import org.elasticsearch.client.TimedRequest;
2727
import org.elasticsearch.client.Validatable;
28+
import org.elasticsearch.common.Nullable;
2829
import org.elasticsearch.common.ParseField;
2930
import org.elasticsearch.common.Strings;
3031
import org.elasticsearch.common.bytes.BytesArray;
@@ -62,6 +63,7 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, ToX
6263

6364
private BytesReference mappings;
6465
private XContentType mappingsXContentType;
66+
private Boolean preferV2Templates;
6567

6668
private final Set<Alias> aliases = new HashSet<>();
6769

@@ -265,6 +267,16 @@ public CreateIndexRequest aliases(Collection<Alias> aliases) {
265267
return this;
266268
}
267269

270+
public CreateIndexRequest preferV2Templates(Boolean preferV2Templates) {
271+
this.preferV2Templates = preferV2Templates;
272+
return this;
273+
}
274+
275+
@Nullable
276+
public Boolean preferV2Templates() {
277+
return this.preferV2Templates;
278+
}
279+
268280
/**
269281
* Sets the settings and mappings as a single source.
270282
*

docs/reference/api-conventions.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ Returns:
385385
"settings": {
386386
"index.number_of_replicas": "1",
387387
"index.number_of_shards": "1",
388+
"index.prefer_v2_templates": "false",
388389
"index.creation_date": "1474389951325",
389390
"index.uuid": "n6gzFZTgS664GUfx0Xrpjw",
390391
"index.version.created": ...,
@@ -421,7 +422,8 @@ Returns:
421422
"version": {
422423
"created": ...
423424
},
424-
"provided_name" : "twitter"
425+
"provided_name" : "twitter",
426+
"prefer_v2_templates": "false"
425427
}
426428
}
427429
}

rest-api-spec/src/main/resources/rest-api-spec/api/bulk.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
"pipeline":{
8888
"type":"string",
8989
"description":"The pipeline id to preprocess incoming documents with"
90+
},
91+
"prefer_v2_templates": {
92+
"type": "boolean",
93+
"description": "favor V2 templates instead of V1 templates during automatic index creation"
9094
}
9195
},
9296
"body":{

rest-api-spec/src/main/resources/rest-api-spec/api/create.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
"pipeline":{
9191
"type":"string",
9292
"description":"The pipeline id to preprocess incoming documents with"
93+
},
94+
"prefer_v2_templates": {
95+
"type": "boolean",
96+
"description": "favor V2 templates instead of V1 templates during automatic index creation"
9397
}
9498
},
9599
"body":{

rest-api-spec/src/main/resources/rest-api-spec/api/index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
"pipeline":{
140140
"type":"string",
141141
"description":"The pipeline id to preprocess incoming documents with"
142+
},
143+
"prefer_v2_templates": {
144+
"type": "boolean",
145+
"description": "favor V2 templates instead of V1 templates during automatic index creation"
142146
}
143147
},
144148
"body":{

rest-api-spec/src/main/resources/rest-api-spec/api/indices.create.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
"master_timeout":{
3838
"type":"time",
3939
"description":"Specify timeout for connection to master"
40+
},
41+
"prefer_v2_templates": {
42+
"type": "boolean",
43+
"description": "favor V2 templates instead of V1 templates during index creation"
4044
}
4145
},
4246
"body":{

rest-api-spec/src/main/resources/rest-api-spec/api/indices.rollover.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
"wait_for_active_shards":{
5858
"type":"string",
5959
"description":"Set the number of active shards to wait for on the newly created rollover index before the operation returns."
60+
},
61+
"prefer_v2_templates": {
62+
"type": "boolean",
63+
"description": "favor V2 templates instead of V1 templates during automatic index creation"
6064
}
6165
},
6266
"body":{

rest-api-spec/src/main/resources/rest-api-spec/api/update.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
9999
"if_primary_term":{
100100
"type":"number",
101101
"description":"only perform the update operation if the last operation that has changed the document has the specified primary term"
102+
},
103+
"prefer_v2_templates": {
104+
"type": "boolean",
105+
"description": "favor V2 templates instead of V1 templates during automatic index creation"
102106
}
103107
},
104108
"body":{

0 commit comments

Comments
 (0)