Skip to content

Commit 0202e1a

Browse files
authored
Add prefer_v2_templates flag and index setting (#55411)
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 f56b0f1 commit 0202e1a

37 files changed

+517
-37
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
@@ -113,6 +113,9 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep
113113
parameters.withTimeout(createIndexRequest.timeout());
114114
parameters.withMasterTimeout(createIndexRequest.masterNodeTimeout());
115115
parameters.withWaitForActiveShards(createIndexRequest.waitForActiveShards());
116+
if (createIndexRequest.preferV2Templates() != null) {
117+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(createIndexRequest.preferV2Templates()));
118+
}
116119
request.addParameters(parameters.asMap());
117120
request.setEntity(RequestConverters.createEntity(createIndexRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
118121
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;
@@ -131,6 +132,9 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
131132
parameters.withRefreshPolicy(bulkRequest.getRefreshPolicy());
132133
parameters.withPipeline(bulkRequest.pipeline());
133134
parameters.withRouting(bulkRequest.routing());
135+
if (bulkRequest.preferV2Templates() != null) {
136+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(bulkRequest.preferV2Templates()));
137+
}
134138
// Bulk API only supports newline delimited JSON or Smile. Before executing
135139
// the bulk, we need to check that all requests have the same content-type
136140
// and this content-type is supported by the Bulk API.
@@ -331,6 +335,9 @@ static Request index(IndexRequest indexRequest) {
331335
parameters.withPipeline(indexRequest.getPipeline());
332336
parameters.withRefreshPolicy(indexRequest.getRefreshPolicy());
333337
parameters.withWaitForActiveShards(indexRequest.waitForActiveShards());
338+
if (indexRequest.preferV2Templates() != null) {
339+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(indexRequest.preferV2Templates()));
340+
}
334341

335342
BytesRef source = indexRequest.source().toBytesRef();
336343
ContentType contentType = createContentType(indexRequest.getContentType());
@@ -357,6 +364,9 @@ static Request update(UpdateRequest updateRequest) throws IOException {
357364
parameters.withRetryOnConflict(updateRequest.retryOnConflict());
358365
parameters.withVersion(updateRequest.version());
359366
parameters.withVersionType(updateRequest.versionType());
367+
if (updateRequest.preferV2Templates() != null) {
368+
parameters.putParam(IndexMetadata.PREFER_V2_TEMPLATES_FLAG, Boolean.toString(updateRequest.preferV2Templates()));
369+
}
360370

361371
// The Java API allows update requests with different content types
362372
// 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
@@ -92,6 +92,10 @@
9292
"pipeline":{
9393
"type":"string",
9494
"description":"The pipeline id to preprocess incoming documents with"
95+
},
96+
"prefer_v2_templates": {
97+
"type": "boolean",
98+
"description": "favor V2 templates instead of V1 templates during automatic index creation"
9599
}
96100
},
97101
"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
@@ -33,6 +33,10 @@
3333
"master_timeout":{
3434
"type":"time",
3535
"description":"Specify timeout for connection to master"
36+
},
37+
"prefer_v2_templates": {
38+
"type": "boolean",
39+
"description": "favor V2 templates instead of V1 templates during index creation"
3640
}
3741
},
3842
"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
@@ -53,6 +53,10 @@
5353
"wait_for_active_shards":{
5454
"type":"string",
5555
"description":"Set the number of active shards to wait for on the newly created rollover index before the operation returns."
56+
},
57+
"prefer_v2_templates": {
58+
"type": "boolean",
59+
"description": "favor V2 templates instead of V1 templates during automatic index creation"
5660
}
5761
},
5862
"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)