@@ -396,37 +396,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
396
396
}
397
397
398
398
if (targetEntity .isAnnotationPresent (DynamicTemplates .class )) {
399
- String mappingPath = targetEntity .getRequiredAnnotation (DynamicTemplates .class ).mappingPath ();
400
- if (hasText (mappingPath )) {
401
- String jsonString = ResourceUtil .readFileFromClasspath (mappingPath );
402
- if (hasText (jsonString )) {
403
- Object templates = new DefaultStringObjectMap <>().fromJson (jsonString ).get ("dynamic_templates" );
404
- if (templates instanceof List <?> array ) {
405
- for (Object node : array ) {
406
- if (node instanceof Map <?, ?> entry ) {
407
- Entry <?, ?> templateEntry = entry .entrySet ().stream ().findFirst ().orElse (null );
408
- if (templateEntry != null ) {
409
- ElasticsearchPersistentProperty property = targetEntity
410
- .getPersistentPropertyWithFieldName ((String ) templateEntry .getKey ());
411
- if (property != null && property .isDynamicFieldMapping ()) {
412
- targetEntity .getPropertyAccessor (result ).getProperty (property );
413
- targetEntity .getPropertyAccessor (result ).setProperty (property ,
414
- document .entrySet ().stream ().filter (fieldKey -> {
415
- if (templateEntry .getValue () instanceof Map <?, ?> templateValue ) {
416
- if (templateValue .containsKey ("match" )) {
417
- return simpleMatch ((String ) templateValue .get ("match" ), fieldKey .getKey ());
418
- }
419
- }
420
-
421
- return false ;
422
- }).collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue )));
423
- }
424
- }
425
- }
426
- }
427
- }
428
- }
429
- }
399
+ populateFieldsUsingDynamicTemplates (targetEntity , result , document );
430
400
}
431
401
}
432
402
@@ -704,6 +674,40 @@ private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T
704
674
});
705
675
}
706
676
677
+ private <R > void populateFieldsUsingDynamicTemplates (ElasticsearchPersistentEntity <?> targetEntity , R result , Document document ) {
678
+ String mappingPath = targetEntity .getRequiredAnnotation (DynamicTemplates .class ).mappingPath ();
679
+ if (hasText (mappingPath )) {
680
+ String jsonString = ResourceUtil .readFileFromClasspath (mappingPath );
681
+ if (hasText (jsonString )) {
682
+ Object templates = new DefaultStringObjectMap <>().fromJson (jsonString ).get ("dynamic_templates" );
683
+ if (templates instanceof List <?> array ) {
684
+ for (Object node : array ) {
685
+ if (node instanceof Map <?, ?> entry ) {
686
+ Entry <?, ?> templateEntry = entry .entrySet ().stream ().findFirst ().orElse (null );
687
+ if (templateEntry != null ) {
688
+ ElasticsearchPersistentProperty property = targetEntity
689
+ .getPersistentPropertyWithFieldName ((String ) templateEntry .getKey ());
690
+ if (property != null && property .isDynamicFieldMapping ()) {
691
+ targetEntity .getPropertyAccessor (result ).getProperty (property );
692
+ targetEntity .getPropertyAccessor (result ).setProperty (property ,
693
+ document .entrySet ().stream ().filter (fieldKey -> {
694
+ if (templateEntry .getValue () instanceof Map <?, ?> templateValue ) {
695
+ if (templateValue .containsKey ("match" )) {
696
+ return simpleMatch ((String ) templateValue .get ("match" ), fieldKey .getKey ());
697
+ }
698
+ }
699
+
700
+ return false ;
701
+ }).collect (Collectors .toMap (Entry ::getKey , Entry ::getValue )));
702
+ }
703
+ }
704
+ }
705
+ }
706
+ }
707
+ }
708
+ }
709
+ }
710
+
707
711
/**
708
712
* Compute the type to use by checking the given entity against the store type;
709
713
*/
0 commit comments