@@ -136,6 +136,9 @@ protected void doInternalExecute(
136136 Map <String , ComponentTemplate > componentTemplateSubstitutions = bulkRequest .getComponentTemplateSubstitutions ();
137137 Map <String , ComposableIndexTemplate > indexTemplateSubstitutions = bulkRequest .getIndexTemplateSubstitutions ();
138138 Map <String , Object > mappingAddition = ((SimulateBulkRequest ) bulkRequest ).getMappingAddition ();
139+ MapperService .MergeReason mappingMergeReason = Optional .ofNullable (((SimulateBulkRequest ) bulkRequest ).getMappingMergeReason ())
140+ .map (mergeReason -> MapperService .MergeReason .valueOf (mergeReason .toUpperCase ()))
141+ .orElse (MapperService .MergeReason .MAPPING_UPDATE );
139142 for (int i = 0 ; i < bulkRequest .requests .size (); i ++) {
140143 DocWriteRequest <?> docRequest = bulkRequest .requests .get (i );
141144 assert docRequest instanceof IndexRequest : "TransportSimulateBulkAction should only ever be called with IndexRequests" ;
@@ -144,7 +147,8 @@ protected void doInternalExecute(
144147 componentTemplateSubstitutions ,
145148 indexTemplateSubstitutions ,
146149 mappingAddition ,
147- request
150+ request ,
151+ mappingMergeReason
148152 );
149153 Exception mappingValidationException = validationResult .v2 ();
150154 responses .set (
@@ -182,7 +186,8 @@ private Tuple<Collection<String>, Exception> validateMappings(
182186 Map <String , ComponentTemplate > componentTemplateSubstitutions ,
183187 Map <String , ComposableIndexTemplate > indexTemplateSubstitutions ,
184188 Map <String , Object > mappingAddition ,
185- IndexRequest request
189+ IndexRequest request ,
190+ MapperService .MergeReason mappingMergeReason
186191 ) {
187192 final SourceToParse sourceToParse = new SourceToParse (
188193 request .id (),
@@ -207,7 +212,7 @@ private Tuple<Collection<String>, Exception> validateMappings(
207212 IndexMetadata imd = project .getIndexSafe (indexAbstraction .getWriteIndex (request , project ));
208213 CompressedXContent mappings = Optional .ofNullable (imd .mapping ()).map (MappingMetadata ::source ).orElse (null );
209214 CompressedXContent mergedMappings = mappingAddition == null ? null : mergeMappings (mappings , mappingAddition );
210- ignoredFields = validateUpdatedMappingsFromIndexMetadata (imd , mergedMappings , request , sourceToParse );
215+ ignoredFields = validateUpdatedMappingsFromIndexMetadata (imd , mergedMappings , request , sourceToParse , mappingMergeReason );
211216 } else {
212217 /*
213218 * The index did not exist, or we have component template substitutions, so we put together the mappings from existing
@@ -265,7 +270,7 @@ private Tuple<Collection<String>, Exception> validateMappings(
265270 );
266271 CompressedXContent mappings = template .mappings ();
267272 CompressedXContent mergedMappings = mergeMappings (mappings , mappingAddition );
268- ignoredFields = validateUpdatedMappings (mappings , mergedMappings , request , sourceToParse );
273+ ignoredFields = validateUpdatedMappings (mappings , mergedMappings , request , sourceToParse , mappingMergeReason );
269274 } else {
270275 List <IndexTemplateMetadata > matchingTemplates = findV1Templates (simulatedProjectMetadata , request .index (), false );
271276 if (matchingTemplates .isEmpty () == false ) {
@@ -279,15 +284,15 @@ private Tuple<Collection<String>, Exception> validateMappings(
279284 xContentRegistry
280285 );
281286 final CompressedXContent combinedMappings = mergeMappings (new CompressedXContent (mappingsMap ), mappingAddition );
282- ignoredFields = validateUpdatedMappings (null , combinedMappings , request , sourceToParse );
287+ ignoredFields = validateUpdatedMappings (null , combinedMappings , request , sourceToParse , mappingMergeReason );
283288 } else {
284289 /*
285290 * The index matched no templates and had no mapping of its own. If there were component template substitutions
286291 * or index template substitutions, they didn't match anything. So just apply the mapping addition if it exists,
287292 * and validate.
288293 */
289294 final CompressedXContent combinedMappings = mergeMappings (null , mappingAddition );
290- ignoredFields = validateUpdatedMappings (null , combinedMappings , request , sourceToParse );
295+ ignoredFields = validateUpdatedMappings (null , combinedMappings , request , sourceToParse , mappingMergeReason );
291296 }
292297 }
293298 }
@@ -305,7 +310,8 @@ private Collection<String> validateUpdatedMappings(
305310 @ Nullable CompressedXContent originalMappings ,
306311 @ Nullable CompressedXContent updatedMappings ,
307312 IndexRequest request ,
308- SourceToParse sourceToParse
313+ SourceToParse sourceToParse ,
314+ MapperService .MergeReason mappingMergeReason
309315 ) throws IOException {
310316 Settings dummySettings = Settings .builder ()
311317 .put (IndexMetadata .SETTING_VERSION_CREATED , IndexVersion .current ())
@@ -318,14 +324,15 @@ private Collection<String> validateUpdatedMappings(
318324 originalIndexMetadataBuilder .putMapping (new MappingMetadata (originalMappings ));
319325 }
320326 final IndexMetadata originalIndexMetadata = originalIndexMetadataBuilder .build ();
321- return validateUpdatedMappingsFromIndexMetadata (originalIndexMetadata , updatedMappings , request , sourceToParse );
327+ return validateUpdatedMappingsFromIndexMetadata (originalIndexMetadata , updatedMappings , request , sourceToParse , mappingMergeReason );
322328 }
323329
324330 private Collection <String > validateUpdatedMappingsFromIndexMetadata (
325331 IndexMetadata originalIndexMetadata ,
326332 @ Nullable CompressedXContent updatedMappings ,
327333 IndexRequest request ,
328- SourceToParse sourceToParse
334+ SourceToParse sourceToParse ,
335+ MapperService .MergeReason mappingMergeReason
329336 ) throws IOException {
330337 if (updatedMappings == null ) {
331338 return List .of (); // no validation to do
@@ -335,7 +342,7 @@ private Collection<String> validateUpdatedMappingsFromIndexMetadata(
335342 .putMapping (new MappingMetadata (updatedMappings ))
336343 .build ();
337344 Engine .Index result = indicesService .withTempIndexService (originalIndexMetadata , indexService -> {
338- indexService .mapperService ().merge (updatedIndexMetadata , MapperService . MergeReason . MAPPING_UPDATE );
345+ indexService .mapperService ().merge (updatedIndexMetadata , mappingMergeReason );
339346 return IndexShard .prepareIndex (
340347 indexService .mapperService (),
341348 sourceToParse ,
0 commit comments