33
33
import org .springframework .data .annotation .Transient ;
34
34
import org .springframework .data .elasticsearch .annotations .CompletionContext ;
35
35
import org .springframework .data .elasticsearch .annotations .CompletionField ;
36
+ import org .springframework .data .elasticsearch .annotations .DynamicMapping ;
36
37
import org .springframework .data .elasticsearch .annotations .DynamicTemplates ;
37
38
import org .springframework .data .elasticsearch .annotations .Field ;
38
39
import org .springframework .data .elasticsearch .annotations .FieldType ;
@@ -86,6 +87,7 @@ public class MappingBuilder {
86
87
private static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length" ;
87
88
private static final String COMPLETION_CONTEXTS = "contexts" ;
88
89
90
+ private static final String TYPE_DYNAMIC = "dynamic" ;
89
91
private static final String TYPE_VALUE_KEYWORD = "keyword" ;
90
92
private static final String TYPE_VALUE_GEO_POINT = "geo_point" ;
91
93
private static final String TYPE_VALUE_COMPLETION = "completion" ;
@@ -120,13 +122,9 @@ public String buildPropertyMapping(Class<?> clazz) throws IOException {
120
122
builder .startObject (FIELD_PARENT ).field (FIELD_PARAM_TYPE , parentType ).endObject ();
121
123
}
122
124
123
- // Properties
124
- builder .startObject (FIELD_PROPERTIES );
125
-
126
- mapEntity (builder , entity , true , "" , false , FieldType .Auto , null );
125
+ mapEntity (builder , entity , true , "" , false , FieldType .Auto , null , entity .findAnnotation (DynamicMapping .class ));
127
126
128
- builder .endObject () // FIELD_PROPERTIES
129
- .endObject () // indexType
127
+ builder .endObject () // indexType
130
128
.endObject () // root object
131
129
.close ();
132
130
@@ -135,7 +133,7 @@ public String buildPropertyMapping(Class<?> clazz) throws IOException {
135
133
136
134
private void mapEntity (XContentBuilder builder , @ Nullable ElasticsearchPersistentEntity entity , boolean isRootObject ,
137
135
String nestedObjectFieldName , boolean nestedOrObjectField , FieldType fieldType ,
138
- @ Nullable Field parentFieldAnnotation ) throws IOException {
136
+ @ Nullable Field parentFieldAnnotation , @ Nullable DynamicMapping dynamicMapping ) throws IOException {
139
137
140
138
boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField (entity ) || nestedOrObjectField );
141
139
if (writeNestedProperties ) {
@@ -146,14 +144,17 @@ private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersisten
146
144
147
145
if (nestedOrObjectField && FieldType .Nested == fieldType && parentFieldAnnotation != null
148
146
&& parentFieldAnnotation .includeInParent ()) {
149
-
150
147
builder .field ("include_in_parent" , parentFieldAnnotation .includeInParent ());
151
148
}
149
+ }
152
150
153
- builder .startObject (FIELD_PROPERTIES );
151
+ if (dynamicMapping != null ) {
152
+ builder .field (TYPE_DYNAMIC , dynamicMapping .value ().name ().toLowerCase ());
154
153
}
155
- if (entity != null ) {
156
154
155
+ builder .startObject (FIELD_PROPERTIES );
156
+
157
+ if (entity != null ) {
157
158
entity .doWithProperties ((PropertyHandler <ElasticsearchPersistentProperty >) property -> {
158
159
try {
159
160
if (property .isAnnotationPresent (Transient .class ) || isInIgnoreFields (property , parentFieldAnnotation )) {
@@ -167,9 +168,12 @@ private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersisten
167
168
});
168
169
}
169
170
171
+ builder .endObject ();
172
+
170
173
if (writeNestedProperties ) {
171
- builder .endObject (). endObject () ;
174
+ builder .endObject ();
172
175
}
176
+
173
177
}
174
178
175
179
private void buildPropertyMapping (XContentBuilder builder , boolean isRootObject ,
@@ -205,7 +209,7 @@ private void buildPropertyMapping(XContentBuilder builder, boolean isRootObject,
205
209
: null ;
206
210
207
211
mapEntity (builder , persistentEntity , false , property .getFieldName (), isNestedOrObjectProperty ,
208
- fieldAnnotation .type (), fieldAnnotation );
212
+ fieldAnnotation .type (), fieldAnnotation , property . findAnnotation ( DynamicMapping . class ) );
209
213
210
214
if (isNestedOrObjectProperty ) {
211
215
return ;
0 commit comments