Skip to content

Commit c787332

Browse files
committed
only include template with _timestamp field mapping if creation a backing index
1 parent 3bd6764 commit c787332

4 files changed

Lines changed: 14 additions & 10 deletions

File tree

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, true);
203+
"{}", simulatedState, matchingTemplate, xContentRegistry, indexName);
204204

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

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

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

492492
final List<Map<String, Object>> mappings =
493-
collectV2Mappings(request.mappings(), currentState, templateName, xContentRegistry, request.dataStreamName() != null);
493+
collectV2Mappings(request.mappings(), currentState, templateName, xContentRegistry, request.index());
494494
final Settings aggregatedIndexSettings =
495495
aggregateIndexSettings(currentState, request,
496496
MetadataIndexTemplateService.resolveSettings(currentState.metadata(), templateName),
@@ -511,11 +511,11 @@ public static List<Map<String, Object>> collectV2Mappings(final String requestMa
511511
final ClusterState currentState,
512512
final String templateName,
513513
final NamedXContentRegistry xContentRegistry,
514-
final boolean createDataStream) throws Exception {
514+
final String indexName) throws Exception {
515515
List<Map<String, Object>> result = new ArrayList<>();
516516

517517
List<CompressedXContent> templateMappings =
518-
MetadataIndexTemplateService.collectMappings(currentState, templateName, createDataStream);
518+
MetadataIndexTemplateService.collectMappings(currentState, templateName, indexName);
519519
for (CompressedXContent templateMapping : templateMappings) {
520520
Map<String, Object> parsedTemplateMapping = MapperService.parseMapping(xContentRegistry, templateMapping.string());
521521
result.add(parsedTemplateMapping);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ public static String findV2Template(Metadata metadata, String indexName, boolean
889889
*/
890890
public static List<CompressedXContent> collectMappings(final ClusterState state,
891891
final String templateName,
892-
final boolean createDataStream) {
892+
final String indexName) {
893893
final ComposableIndexTemplate template = state.metadata().templatesV2().get(templateName);
894894
assert template != null : "attempted to resolve mappings for a template [" + templateName +
895895
"] that did not exist in the cluster state";
@@ -910,8 +910,10 @@ public static List<CompressedXContent> collectMappings(final ClusterState state,
910910
.map(Template::mappings)
911911
.ifPresent(mappings::add);
912912

913-
// Add the mapping of a data stream's timestamp field if available, since it has the highest precedence:
914-
if (createDataStream) {
913+
// Only include _timestamp mapping snippet if creating backing index.
914+
if (indexName.startsWith(DataStream.BACKING_INDEX_PREFIX)) {
915+
// Only if template has data stream definition this should be added and
916+
// adding this template last, since _timestamp field should have highest precedence:
915917
Optional.ofNullable(template.getDataStreamTemplate())
916918
.map(ComposableIndexTemplate.DataStreamTemplate::getDataSteamMappingSnippet)
917919
.map(mapping -> {
@@ -1069,8 +1071,10 @@ private static void validateCompositeTemplate(final ClusterState state,
10691071
// shard id and the current timestamp
10701072
xContentRegistry, tempIndexService.newQueryShardContext(0, null, () -> 0L, null));
10711073

1074+
// triggers inclusion of _timestamp field and its validation:
1075+
String indexName = DataStream.BACKING_INDEX_PREFIX + temporaryIndexName;
10721076
// Parse mappings to ensure they are valid after being composed
1073-
List<CompressedXContent> mappings = collectMappings(stateWithIndex, templateName, true);
1077+
List<CompressedXContent> mappings = collectMappings(stateWithIndex, templateName, indexName );
10741078
try {
10751079
MapperService mapperService = tempIndexService.mapperService();
10761080
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", false);
709+
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "my-template", "my-index");
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", false);
772+
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "my-template", "my-index");
773773

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

0 commit comments

Comments
 (0)