31
31
import org .slf4j .LoggerFactory ;
32
32
import org .springframework .core .io .ClassPathResource ;
33
33
import org .springframework .data .annotation .Transient ;
34
- import org .springframework .data .elasticsearch .ElasticsearchException ;
35
34
import org .springframework .data .elasticsearch .annotations .*;
36
35
import org .springframework .data .elasticsearch .core .ElasticsearchRestTemplate ;
37
36
import org .springframework .data .elasticsearch .core .ResourceUtil ;
66
65
*/
67
66
public class MappingBuilder {
68
67
68
+ private static final Logger logger = LoggerFactory .getLogger (ElasticsearchRestTemplate .class );
69
+
69
70
private static final String FIELD_INDEX = "index" ;
70
71
private static final String FIELD_PROPERTIES = "properties" ;
71
72
@ Deprecated private static final String FIELD_PARENT = "_parent" ;
@@ -88,7 +89,7 @@ public class MappingBuilder {
88
89
89
90
private static final String JOIN_TYPE_RELATIONS = "relations" ;
90
91
91
- private static final Logger logger = LoggerFactory . getLogger ( ElasticsearchRestTemplate . class ) ;
92
+ private static final String MAPPING_ENABLED = "enabled" ;
92
93
93
94
private final ElasticsearchConverter elasticsearchConverter ;
94
95
@@ -100,9 +101,9 @@ public MappingBuilder(ElasticsearchConverter elasticsearchConverter) {
100
101
* builds the Elasticsearch mapping for the given clazz.
101
102
*
102
103
* @return JSON string
103
- * @throws ElasticsearchException on errors while building the mapping
104
+ * @throws MappingException on errors while building the mapping
104
105
*/
105
- public String buildPropertyMapping (Class <?> clazz ) throws ElasticsearchException {
106
+ public String buildPropertyMapping (Class <?> clazz ) throws MappingException {
106
107
107
108
try {
108
109
ElasticsearchPersistentEntity <?> entity = elasticsearchConverter .getMappingContext ()
@@ -125,15 +126,23 @@ public String buildPropertyMapping(Class<?> clazz) throws ElasticsearchException
125
126
.close ();
126
127
127
128
return builder .getOutputStream ().toString ();
128
- } catch (MappingException | IOException e ) {
129
- throw new ElasticsearchException ("could not build mapping" , e );
129
+ } catch (IOException e ) {
130
+ throw new MappingException ("could not build mapping" , e );
130
131
}
131
132
}
132
133
133
134
private void mapEntity (XContentBuilder builder , @ Nullable ElasticsearchPersistentEntity <?> entity ,
134
135
boolean isRootObject , String nestedObjectFieldName , boolean nestedOrObjectField , FieldType fieldType ,
135
136
@ Nullable Field parentFieldAnnotation , @ Nullable DynamicMapping dynamicMapping ) throws IOException {
136
137
138
+ if (entity != null && entity .isAnnotationPresent (Mapping .class )) {
139
+
140
+ if (!entity .getRequiredAnnotation (Mapping .class ).enabled ()) {
141
+ builder .field (MAPPING_ENABLED , false );
142
+ return ;
143
+ }
144
+ }
145
+
137
146
boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField (entity ) || nestedOrObjectField );
138
147
if (writeNestedProperties ) {
139
148
@@ -189,14 +198,22 @@ private void buildPropertyMapping(XContentBuilder builder, boolean isRootObject,
189
198
190
199
if (property .isAnnotationPresent (Mapping .class )) {
191
200
192
- String mappingPath = property .getRequiredAnnotation (Mapping .class ).mappingPath ();
193
- if (!StringUtils .isEmpty (mappingPath )) {
201
+ Mapping mapping = property .getRequiredAnnotation (Mapping .class );
202
+
203
+ if (mapping .enabled ()) {
204
+ String mappingPath = mapping .mappingPath ();
194
205
195
- ClassPathResource mappings = new ClassPathResource (mappingPath );
196
- if (mappings .exists ()) {
197
- builder .rawField (property .getFieldName (), mappings .getInputStream (), XContentType .JSON );
198
- return ;
206
+ if (StringUtils .hasText (mappingPath )) {
207
+
208
+ ClassPathResource mappings = new ClassPathResource (mappingPath );
209
+ if (mappings .exists ()) {
210
+ builder .rawField (property .getFieldName (), mappings .getInputStream (), XContentType .JSON );
211
+ return ;
212
+ }
199
213
}
214
+ } else {
215
+ applyDisabledPropertyMapping (builder , property );
216
+ return ;
200
217
}
201
218
}
202
219
@@ -317,9 +334,29 @@ private void applyCompletionFieldMapping(XContentBuilder builder, ElasticsearchP
317
334
318
335
private void applyDefaultIdFieldMapping (XContentBuilder builder , ElasticsearchPersistentProperty property )
319
336
throws IOException {
337
+ builder .startObject (property .getFieldName ()) //
338
+ .field (FIELD_PARAM_TYPE , TYPE_VALUE_KEYWORD ) //
339
+ .field (FIELD_INDEX , true ) //
340
+ .endObject (); //
341
+ }
342
+
343
+ private void applyDisabledPropertyMapping (XContentBuilder builder , ElasticsearchPersistentProperty property )
344
+ throws IOException {
320
345
321
- builder .startObject (property .getFieldName ()).field (FIELD_PARAM_TYPE , TYPE_VALUE_KEYWORD ).field (FIELD_INDEX , true )
322
- .endObject ();
346
+ try {
347
+ Field field = property .getRequiredAnnotation (Field .class );
348
+
349
+ if (field .type () != FieldType .Object ) {
350
+ throw new IllegalArgumentException ("Field type must be 'object" );
351
+ }
352
+
353
+ builder .startObject (property .getFieldName ()) //
354
+ .field (FIELD_PARAM_TYPE , field .type ().name ().toLowerCase ()) //
355
+ .field (MAPPING_ENABLED , false ) //
356
+ .endObject (); //
357
+ } catch (Exception e ) {
358
+ throw new MappingException ("Could not write enabled: false mapping for " + property .getFieldName (), e );
359
+ }
323
360
}
324
361
325
362
/**
0 commit comments