@@ -569,9 +569,7 @@ static Map<String, Object> parseV2Mappings(String mappingsJson, List<CompressedX
569569 Map <String , Object > innerTemplateNonProperties = new HashMap <>(innerTemplateMapping );
570570 Map <String , Object > maybeProperties = (Map <String , Object >) innerTemplateNonProperties .remove ("properties" );
571571
572- nonProperties = removeDuplicatedDynamicTemplates (nonProperties , innerTemplateNonProperties );
573- XContentHelper .mergeDefaults (innerTemplateNonProperties , nonProperties );
574- nonProperties = innerTemplateNonProperties ;
572+ nonProperties = mergeFailingOnReplacement (nonProperties , innerTemplateNonProperties );
575573
576574 if (maybeProperties != null ) {
577575 properties = mergeFailingOnReplacement (properties , maybeProperties );
@@ -584,9 +582,7 @@ static Map<String, Object> parseV2Mappings(String mappingsJson, List<CompressedX
584582 Map <String , Object > innerRequestNonProperties = new HashMap <>(innerRequestMappings );
585583 Map <String , Object > maybeRequestProperties = (Map <String , Object >) innerRequestNonProperties .remove ("properties" );
586584
587- nonProperties = removeDuplicatedDynamicTemplates (nonProperties , innerRequestMappings );
588- XContentHelper .mergeDefaults (innerRequestNonProperties , nonProperties );
589- nonProperties = innerRequestNonProperties ;
585+ nonProperties = mergeFailingOnReplacement (nonProperties , innerRequestNonProperties );
590586
591587 if (maybeRequestProperties != null ) {
592588 properties = mergeFailingOnReplacement (properties , maybeRequestProperties );
@@ -598,71 +594,6 @@ static Map<String, Object> parseV2Mappings(String mappingsJson, List<CompressedX
598594 return Collections .singletonMap (MapperService .SINGLE_MAPPING_NAME , finalMappings );
599595 }
600596
601- /**
602- * Removes the already seen/processed dynamic templates from the previouslySeenMapping if they are defined (we're
603- * identifying the dynamic templates based on the name only, *not* on the full definition) in the newMapping we are about to
604- * process (and merge)
605- */
606- @ SuppressWarnings ("unchecked" )
607- private static Map <String , Object > removeDuplicatedDynamicTemplates (Map <String , Object > previouslySeenMapping ,
608- Map <String , Object > newMapping ) {
609- Map <String , Object > result = new HashMap <>(previouslySeenMapping );
610- List <Map <String , Object >> newDynamicTemplates = (List <Map <String , Object >>) newMapping .get ("dynamic_templates" );
611- List <Map <String , Object >> previouslySeenDynamicTemplates =
612- (List <Map <String , Object >>) previouslySeenMapping .get ("dynamic_templates" );
613-
614- List <Map <String , Object >> filteredDynamicTemplates = removeOverlapping (previouslySeenDynamicTemplates , newDynamicTemplates );
615-
616- // if we removed any mappings from the previously seen ones, we'll re-add them on merge time, see
617- // {@link XContentHelper#mergeDefaults}, so update the result to contain the filtered ones
618- if (filteredDynamicTemplates != previouslySeenDynamicTemplates ) {
619- result .put ("dynamic_templates" , filteredDynamicTemplates );
620- }
621- return result ;
622- }
623-
624- /**
625- * Removes all the items from the first list that are already present in the second list
626- *
627- * Similar to {@link List#removeAll(Collection)} but the list parameters are not modified.
628- *
629- * This expects both list values to be Maps of size one and the "contains" operation that will determine if a value
630- * from the second list is present in the first list (and be removed from the first list) is based on key name.
631- *
632- * eg.
633- * removeAll([ {"key1" : {}}, {"key2" : {}} ], [ {"key1" : {}}, {"key3" : {}} ])
634- * Returns:
635- * [ {"key2" : {}} ]
636- */
637- private static List <Map <String , Object >> removeOverlapping (List <Map <String , Object >> first , List <Map <String , Object >> second ) {
638- if (first == null ) {
639- return first ;
640- } else {
641- validateValuesAreMapsOfSizeOne (first );
642- }
643-
644- if (second == null ) {
645- return first ;
646- } else {
647- validateValuesAreMapsOfSizeOne (second );
648- }
649-
650- Set <String > keys = second .stream ()
651- .map (value -> value .keySet ().iterator ().next ())
652- .collect (Collectors .toSet ());
653-
654- return first .stream ().filter (value -> keys .contains (value .keySet ().iterator ().next ()) == false ).collect (toList ());
655- }
656-
657- private static void validateValuesAreMapsOfSizeOne (List <Map <String , Object >> second ) {
658- for (Map <String , Object > map : second ) {
659- // all are in the form of [ {"key1" : {}}, {"key2" : {}} ]
660- if (map .size () != 1 ) {
661- throw new IllegalArgumentException ("unexpected argument, expected maps with one key, but got " + map );
662- }
663- }
664- }
665-
666597 /**
667598 * Parses the `dynamic_templates` from the provided mappings, if any are configured, and returns a mappings map containing dynamic
668599 * templates with unique names.
0 commit comments