@@ -200,30 +200,15 @@ private Tuple<Collection<String>, Exception> validateMappings(
200200 try {
201201 if (indexAbstraction != null
202202 && componentTemplateSubstitutions .isEmpty ()
203- && indexTemplateSubstitutions .isEmpty ()
204- && mappingAddition .isEmpty ()) {
203+ && indexTemplateSubstitutions .isEmpty ()) {
205204 /*
206- * In this case the index exists and we don't have any component template overrides. So we can just use withTempIndexService
207- * to do the mapping validation, using all the existing logic for validation .
205+ * In this case the index exists and we don't have any template overrides. So we can just merge the mappingAddition (which
206+ * might not exist) into the existing index mapping .
208207 */
209208 IndexMetadata imd = project .getIndexSafe (indexAbstraction .getWriteIndex (request , project ));
210- indicesService .withTempIndexService (imd , indexService -> {
211- indexService .mapperService ().updateMapping (null , imd );
212- return IndexShard .prepareIndex (
213- indexService .mapperService (),
214- sourceToParse ,
215- SequenceNumbers .UNASSIGNED_SEQ_NO ,
216- -1 ,
217- -1 ,
218- VersionType .INTERNAL ,
219- Engine .Operation .Origin .PRIMARY ,
220- Long .MIN_VALUE ,
221- false ,
222- request .ifSeqNo (),
223- request .ifPrimaryTerm (),
224- 0
225- );
226- });
209+ CompressedXContent mappings = imd .mapping () == null ? null : imd .mapping ().source ();
210+ CompressedXContent mergedMappings = mappingAddition == null ? null : mergeMappings (mappings , mappingAddition );
211+ ignoredFields = validateUpdatedMappingsFromIndexMetadata (imd , mergedMappings , request , sourceToParse );
227212 } else {
228213 /*
229214 * The index did not exist, or we have component template substitutions, so we put together the mappings from existing
@@ -332,9 +317,6 @@ private Collection<String> validateUpdatedMappings(
332317 IndexRequest request ,
333318 SourceToParse sourceToParse
334319 ) throws IOException {
335- if (updatedMappings == null ) {
336- return List .of (); // no validation to do
337- }
338320 Settings dummySettings = Settings .builder ()
339321 .put (IndexMetadata .SETTING_VERSION_CREATED , IndexVersion .current ())
340322 .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
@@ -346,8 +328,19 @@ private Collection<String> validateUpdatedMappings(
346328 originalIndexMetadataBuilder .putMapping (new MappingMetadata (originalMappings ));
347329 }
348330 final IndexMetadata originalIndexMetadata = originalIndexMetadataBuilder .build ();
331+ return validateUpdatedMappingsFromIndexMetadata (originalIndexMetadata , updatedMappings , request , sourceToParse );
332+ }
333+ private Collection <String > validateUpdatedMappingsFromIndexMetadata (
334+ IndexMetadata originalIndexMetadata ,
335+ @ Nullable CompressedXContent updatedMappings ,
336+ IndexRequest request ,
337+ SourceToParse sourceToParse
338+ ) throws IOException {
339+ if (updatedMappings == null ) {
340+ return List .of (); // no validation to do
341+ }
349342 final IndexMetadata updatedIndexMetadata = IndexMetadata .builder (request .index ())
350- .settings (dummySettings )
343+ .settings (originalIndexMetadata . getSettings () )
351344 .putMapping (new MappingMetadata (updatedMappings ))
352345 .build ();
353346 Engine .Index result = indicesService .withTempIndexService (originalIndexMetadata , indexService -> {
0 commit comments