Skip to content

Commit a23d2e4

Browse files
committed
only apply _timestamp meta field if index is created as part of a data stream or data stream rollover,
this fixes a docs test, where a regular index creation matches (logs-*) with a template with a data stream definition.
1 parent 95e5b2c commit a23d2e4

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

docs/reference/indices/rollover-index.asciidoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,14 @@ PUT _index_template/template
233233
"template": {
234234
"mappings": {
235235
"properties": {
236-
"date": {
236+
"@timestamp": {
237237
"type": "date"
238238
}
239239
}
240240
}
241241
},
242242
"data_stream": {
243-
"timestamp_field": "date"
243+
"timestamp_field": "@timestamp"
244244
}
245245
}
246246
-----------------------------------
@@ -255,15 +255,15 @@ PUT /_data_stream/my-data-stream <1>
255255
POST /my-data-stream/_rollover <2>
256256
{
257257
"conditions" : {
258-
"max_age": "7d",
259-
"max_docs": 1000,
260-
"max_size": "5gb"
258+
"max_age": "7d",
259+
"max_docs": 1000,
260+
"max_size": "5gb"
261261
}
262262
}
263263
--------------------------------------------------
264264
// TEST[continued]
265265
// TEST[setup:huge_twitter]
266-
// TEST[s/# Add > 1000 documents to my-data-stream/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"my-data-stream","op_type":"create"}}/]
266+
// TEST[s/# Add > 1000 documents to my-data-stream/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"my-data-stream","op_type":"create"},"script":{"source":"ctx._source.put('@timestamp',ctx._source.remove('date'))"}}/]
267267
<1> Creates a data stream called `my-data-stream` with one initial backing index
268268
named `my-data-stream-000001`.
269269
<2> This request creates a new backing index, `my-data-stream-000002`, and adds
@@ -305,7 +305,7 @@ The API returns the following response:
305305
[source,console]
306306
-----------------------------------
307307
DELETE /_data_stream/my-data-stream
308-
DELETE /_index_template/*
308+
DELETE /_index_template/template
309309
-----------------------------------
310310
// TEST[continued]
311311
////

server/src/main/java/org/elasticsearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public static Template resolveTemplate(final String matchingTemplate, final Stri
200200

201201
// empty request mapping as the user can't specify any explicit mappings via the simulate api
202202
List<Map<String, Object>> mappings = MetadataCreateIndexService.collectV2Mappings(
203-
"{}", simulatedState, matchingTemplate, xContentRegistry);
203+
"{}", simulatedState, matchingTemplate, xContentRegistry, true);
204204

205205
CompressedXContent mergedMapping = indicesService.<CompressedXContent, Exception>withTempIndexService(indexMetadata,
206206
tempIndexService -> {

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ private ClusterState applyCreateIndexRequestWithV2Template(final ClusterState cu
489489
throws Exception {
490490
logger.debug("applying create index request using composable template [{}]", templateName);
491491

492-
final List<Map<String, Object>> mappings = collectV2Mappings(request.mappings(), currentState, templateName, xContentRegistry);
492+
final List<Map<String, Object>> mappings =
493+
collectV2Mappings(request.mappings(), currentState, templateName, xContentRegistry, request.dataStreamName() != null);
493494
final Settings aggregatedIndexSettings =
494495
aggregateIndexSettings(currentState, request,
495496
MetadataIndexTemplateService.resolveSettings(currentState.metadata(), templateName),
@@ -509,10 +510,11 @@ private ClusterState applyCreateIndexRequestWithV2Template(final ClusterState cu
509510
public static List<Map<String, Object>> collectV2Mappings(final String requestMappings,
510511
final ClusterState currentState,
511512
final String templateName,
512-
final NamedXContentRegistry xContentRegistry) throws Exception {
513+
final NamedXContentRegistry xContentRegistry,
514+
final boolean createDataStream) throws Exception {
513515
List<Map<String, Object>> result = new ArrayList<>();
514516

515-
List<CompressedXContent> templateMappings = MetadataIndexTemplateService.collectMappings(currentState, templateName);
517+
List<CompressedXContent> templateMappings = MetadataIndexTemplateService.collectMappings(currentState, templateName, createDataStream);
516518
for (CompressedXContent templateMapping : templateMappings) {
517519
Map<String, Object> parsedTemplateMapping = MapperService.parseMapping(xContentRegistry, templateMapping.string());
518520
result.add(parsedTemplateMapping);

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,9 @@ public static String findV2Template(Metadata metadata, String indexName, boolean
887887
/**
888888
* Collect the given v2 template into an ordered list of mappings.
889889
*/
890-
public static List<CompressedXContent> collectMappings(final ClusterState state, final String templateName) {
890+
public static List<CompressedXContent> collectMappings(final ClusterState state,
891+
final String templateName,
892+
final boolean createDataStream) {
891893
final ComposableIndexTemplate template = state.metadata().templatesV2().get(templateName);
892894
assert template != null : "attempted to resolve mappings for a template [" + templateName +
893895
"] that did not exist in the cluster state";
@@ -909,17 +911,19 @@ public static List<CompressedXContent> collectMappings(final ClusterState state,
909911
.ifPresent(mappings::add);
910912

911913
// Add the mapping of a data stream's timestamp field if available, since it has the highest precedence:
912-
Optional.ofNullable(template.getDataStreamTemplate())
913-
.map(ComposableIndexTemplate.DataStreamTemplate::getDataSteamMappingSnippet)
914-
.map(mapping -> {
915-
try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
916-
builder.value(mapping);
917-
return new CompressedXContent(BytesReference.bytes(builder));
918-
} catch (IOException e) {
919-
throw new UncheckedIOException(e);
920-
}
921-
})
922-
.ifPresent(mappings::add);
914+
if (createDataStream) {
915+
Optional.ofNullable(template.getDataStreamTemplate())
916+
.map(ComposableIndexTemplate.DataStreamTemplate::getDataSteamMappingSnippet)
917+
.map(mapping -> {
918+
try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
919+
builder.value(mapping);
920+
return new CompressedXContent(BytesReference.bytes(builder));
921+
} catch (IOException e) {
922+
throw new UncheckedIOException(e);
923+
}
924+
})
925+
.ifPresent(mappings::add);
926+
}
923927
return Collections.unmodifiableList(mappings);
924928
}
925929

@@ -1066,7 +1070,7 @@ private static void validateCompositeTemplate(final ClusterState state,
10661070
xContentRegistry, tempIndexService.newQueryShardContext(0, null, () -> 0L, null));
10671071

10681072
// Parse mappings to ensure they are valid after being composed
1069-
List<CompressedXContent> mappings = collectMappings(stateWithIndex, templateName);
1073+
List<CompressedXContent> mappings = collectMappings(stateWithIndex, templateName, true);
10701074
try {
10711075
MapperService mapperService = tempIndexService.mapperService();
10721076
for (CompressedXContent mapping : mappings) {

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ public void testResolveConflictingMappings() throws Exception {
706706
List.of("ct_low", "ct_high"), 0L, 1L, null, null);
707707
state = service.addIndexTemplateV2(state, true, "my-template", it);
708708

709-
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "my-template");
709+
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "my-template", false);
710710

711711
assertNotNull(mappings);
712712
assertThat(mappings.size(), equalTo(3));
@@ -769,7 +769,7 @@ public void testResolveMappings() throws Exception {
769769
List.of("ct_low", "ct_high"), 0L, 1L, null, null);
770770
state = service.addIndexTemplateV2(state, true, "my-template", it);
771771

772-
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "my-template");
772+
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "my-template", false);
773773

774774
assertNotNull(mappings);
775775
assertThat(mappings.size(), equalTo(3));

0 commit comments

Comments
 (0)