@@ -97,7 +97,7 @@ public class MappingBuilder {
97
97
private static final String DYNAMIC_DATE_FORMATS = "dynamic_date_formats" ;
98
98
private static final String RUNTIME = "runtime" ;
99
99
100
- private final ElasticsearchConverter elasticsearchConverter ;
100
+ protected final ElasticsearchConverter elasticsearchConverter ;
101
101
102
102
private boolean writeTypeHints = true ;
103
103
@@ -113,9 +113,16 @@ public MappingBuilder(ElasticsearchConverter elasticsearchConverter) {
113
113
*/
114
114
public String buildPropertyMapping (Class <?> clazz ) throws MappingException {
115
115
116
+ ElasticsearchPersistentEntity <?> entity = elasticsearchConverter .getMappingContext ()
117
+ .getRequiredPersistentEntity (clazz );
118
+
119
+ return buildPropertyMapping (entity , getRuntimeFields (entity ));
120
+ }
121
+
122
+ protected String buildPropertyMapping (ElasticsearchPersistentEntity <?> entity ,
123
+ @ Nullable org .springframework .data .elasticsearch .core .document .Document runtimeFields ) {
124
+
116
125
try {
117
- ElasticsearchPersistentEntity <?> entity = elasticsearchConverter .getMappingContext ()
118
- .getRequiredPersistentEntity (clazz );
119
126
120
127
writeTypeHints = entity .writeTypeHints ();
121
128
@@ -124,7 +131,8 @@ public String buildPropertyMapping(Class<?> clazz) throws MappingException {
124
131
// Dynamic templates
125
132
addDynamicTemplatesMapping (builder , entity );
126
133
127
- mapEntity (builder , entity , true , "" , false , FieldType .Auto , null , entity .findAnnotation (DynamicMapping .class ));
134
+ mapEntity (builder , entity , true , "" , false , FieldType .Auto , null , entity .findAnnotation (DynamicMapping .class ),
135
+ runtimeFields );
128
136
129
137
builder .endObject () // root object
130
138
.close ();
@@ -148,7 +156,8 @@ private void writeTypeHintMapping(XContentBuilder builder) throws IOException {
148
156
149
157
private void mapEntity (XContentBuilder builder , @ Nullable ElasticsearchPersistentEntity <?> entity ,
150
158
boolean isRootObject , String nestedObjectFieldName , boolean nestedOrObjectField , FieldType fieldType ,
151
- @ Nullable Field parentFieldAnnotation , @ Nullable DynamicMapping dynamicMapping ) throws IOException {
159
+ @ Nullable Field parentFieldAnnotation , @ Nullable DynamicMapping dynamicMapping ,
160
+ @ Nullable org .springframework .data .elasticsearch .core .document .Document runtimeFields ) throws IOException {
152
161
153
162
if (entity != null && entity .isAnnotationPresent (Mapping .class )) {
154
163
Mapping mappingAnnotation = entity .getRequiredAnnotation (Mapping .class );
@@ -170,8 +179,8 @@ private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersisten
170
179
builder .field (DYNAMIC_DATE_FORMATS , mappingAnnotation .dynamicDateFormats ());
171
180
}
172
181
173
- if (StringUtils . hasText ( mappingAnnotation . runtimeFieldsPath ()) ) {
174
- addRuntimeFields ( builder , mappingAnnotation . runtimeFieldsPath () );
182
+ if (runtimeFields != null ) {
183
+ builder . field ( RUNTIME , runtimeFields );
175
184
}
176
185
}
177
186
@@ -227,13 +236,22 @@ private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersisten
227
236
228
237
}
229
238
230
- private void addRuntimeFields (XContentBuilder builder , String runtimeFieldsPath ) throws IOException {
239
+ @ Nullable
240
+ private org .springframework .data .elasticsearch .core .document .Document getRuntimeFields (
241
+ @ Nullable ElasticsearchPersistentEntity <?> entity ) {
231
242
232
- ClassPathResource runtimeFields = new ClassPathResource (runtimeFieldsPath );
243
+ if (entity != null ) {
244
+ Mapping mappingAnnotation = entity .findAnnotation (Mapping .class );
245
+ if (mappingAnnotation != null ) {
246
+ String runtimeFieldsPath = mappingAnnotation .runtimeFieldsPath ();
233
247
234
- if (runtimeFields .exists ()) {
235
- builder .rawField (RUNTIME , runtimeFields .getInputStream (), XContentType .JSON );
248
+ if (hasText (runtimeFieldsPath )) {
249
+ String jsonString = ResourceUtil .readFileFromClasspath (runtimeFieldsPath );
250
+ return org .springframework .data .elasticsearch .core .document .Document .parse (jsonString );
251
+ }
252
+ }
236
253
}
254
+ return null ;
237
255
}
238
256
239
257
private void buildPropertyMapping (XContentBuilder builder , boolean isRootObject ,
@@ -291,7 +309,7 @@ private void buildPropertyMapping(XContentBuilder builder, boolean isRootObject,
291
309
: null ;
292
310
293
311
mapEntity (builder , persistentEntity , false , property .getFieldName (), true , fieldAnnotation .type (),
294
- fieldAnnotation , dynamicMapping );
312
+ fieldAnnotation , dynamicMapping , null );
295
313
return ;
296
314
}
297
315
}
0 commit comments