29
29
import org .springframework .core .GenericCollectionTypeResolver ;
30
30
import org .springframework .data .annotation .Transient ;
31
31
import org .springframework .data .elasticsearch .annotations .*;
32
- import org .springframework .data .elasticsearch .core .completion .Completion ;
32
+ import org .springframework .data .elasticsearch .core .completion .Completion ;
33
33
import org .springframework .data .elasticsearch .core .facet .FacetRequest ;
34
34
import org .springframework .data .elasticsearch .core .geo .GeoPoint ;
35
35
import org .springframework .data .mapping .model .SimpleTypeHolder ;
@@ -54,10 +54,15 @@ class MappingBuilder {
54
54
public static final String FIELD_PROPERTIES = "properties" ;
55
55
public static final String FIELD_PARENT = "_parent" ;
56
56
57
+ public static final String COMPLETION_PAYLOADS = "payloads" ;
58
+ public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators" ;
59
+ public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments" ;
60
+ public static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length" ;
61
+
57
62
public static final String INDEX_VALUE_NOT_ANALYZED = "not_analyzed" ;
58
63
public static final String TYPE_VALUE_STRING = "string" ;
59
- public static final String TYPE_VALUE_GEO_POINT = "geo_point" ;
60
- public static final String TYPE_VALUE_COMPLETION = "completion" ;
64
+ public static final String TYPE_VALUE_GEO_POINT = "geo_point" ;
65
+ public static final String TYPE_VALUE_COMPLETION = "completion" ;
61
66
62
67
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder ();
63
68
@@ -96,11 +101,11 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
96
101
continue ;
97
102
}
98
103
99
- boolean isGeoField = isGeoField (field );
100
- boolean isCompletionField = isCompletionField (field );
104
+ boolean isGeoField = isGeoField (field );
105
+ boolean isCompletionField = isCompletionField (field );
101
106
102
107
Field singleField = field .getAnnotation (Field .class );
103
- if (!isGeoField && !isCompletionField && isEntity (field ) && isAnnotated (field )) {
108
+ if (!isGeoField && !isCompletionField && isEntity (field ) && isAnnotated (field )) {
104
109
if (singleField == null ) {
105
110
continue ;
106
111
}
@@ -113,13 +118,14 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
113
118
114
119
MultiField multiField = field .getAnnotation (MultiField .class );
115
120
116
- if (isGeoField ) {
117
- applyGeoPointFieldMapping (xContentBuilder , field );
118
- }
119
-
120
- if (isCompletionField ) {
121
- applyCompletionFieldMapping (xContentBuilder , field );
122
- }
121
+ if (isGeoField ) {
122
+ applyGeoPointFieldMapping (xContentBuilder , field );
123
+ }
124
+
125
+ if (isCompletionField ) {
126
+ CompletionField completionField = field .getAnnotation (CompletionField .class );
127
+ applyCompletionFieldMapping (xContentBuilder , field , completionField );
128
+ }
123
129
124
130
if (isRootObject && singleField != null && isIdField (field , idFieldName )) {
125
131
applyDefaultIdFieldMapping (xContentBuilder , field );
@@ -145,13 +151,13 @@ private static java.lang.reflect.Field[] retrieveFields(Class clazz) {
145
151
fields .addAll (Arrays .asList (targetClass .getDeclaredFields ()));
146
152
targetClass = targetClass .getSuperclass ();
147
153
}
148
- while (targetClass != null && targetClass != Object .class );
154
+ while (targetClass != null && targetClass != Object .class );
149
155
150
156
return fields .toArray (new java .lang .reflect .Field [fields .size ()]);
151
157
}
152
158
153
159
private static boolean isAnnotated (java .lang .reflect .Field field ) {
154
- return field .getAnnotation (Field .class ) != null || field .getAnnotation (MultiField .class ) != null || field .getAnnotation (GeoPointField .class ) != null ;
160
+ return field .getAnnotation (Field .class ) != null || field .getAnnotation (MultiField .class ) != null || field .getAnnotation (GeoPointField .class ) != null || field . getAnnotation ( CompletionField . class ) != null ;
155
161
}
156
162
157
163
private static void applyGeoPointFieldMapping (XContentBuilder xContentBuilder , java .lang .reflect .Field field ) throws IOException {
@@ -160,12 +166,24 @@ private static void applyGeoPointFieldMapping(XContentBuilder xContentBuilder, j
160
166
.endObject ();
161
167
}
162
168
163
- private static void applyCompletionFieldMapping (XContentBuilder xContentBuilder , java .lang .reflect .Field field ) throws IOException {
164
- xContentBuilder .startObject (field .getName ());
165
- xContentBuilder .field (FIELD_TYPE , TYPE_VALUE_COMPLETION )
166
- .endObject ();
167
- }
168
-
169
+ private static void applyCompletionFieldMapping (XContentBuilder xContentBuilder , java .lang .reflect .Field field , CompletionField annotation ) throws IOException {
170
+ xContentBuilder .startObject (field .getName ());
171
+ xContentBuilder .field (FIELD_TYPE , TYPE_VALUE_COMPLETION );
172
+ if (annotation != null ) {
173
+ xContentBuilder .field (COMPLETION_MAX_INPUT_LENGTH , annotation .maxInputLength ());
174
+ xContentBuilder .field (COMPLETION_PAYLOADS , annotation .payloads ());
175
+ xContentBuilder .field (COMPLETION_PRESERVE_POSITION_INCREMENTS , annotation .preservePositionIncrements ());
176
+ xContentBuilder .field (COMPLETION_PRESERVE_SEPARATORS , annotation .preserveSeparators ());
177
+ if (isNotBlank (annotation .searchAnalyzer ())) {
178
+ xContentBuilder .field (FIELD_SEARCH_ANALYZER , annotation .searchAnalyzer ());
179
+ }
180
+ if (isNotBlank (annotation .indexAnalyzer ())) {
181
+ xContentBuilder .field (FIELD_INDEX_ANALYZER , annotation .indexAnalyzer ());
182
+ }
183
+ }
184
+ xContentBuilder .endObject ();
185
+ }
186
+
169
187
private static void applyDefaultIdFieldMapping (XContentBuilder xContentBuilder , java .lang .reflect .Field field )
170
188
throws IOException {
171
189
xContentBuilder .startObject (field .getName ())
@@ -317,11 +335,11 @@ private static boolean isNestedOrObjectField(java.lang.reflect.Field field) {
317
335
return fieldAnnotation != null && (FieldType .Nested == fieldAnnotation .type () || FieldType .Object == fieldAnnotation .type ());
318
336
}
319
337
320
- private static boolean isGeoField (java .lang .reflect .Field field ) {
321
- return field .getType () == GeoPoint .class || field .getAnnotation (GeoPointField .class ) != null ;
322
- }
323
-
324
- private static boolean isCompletionField (java .lang .reflect .Field field ) {
325
- return field .getType () == Completion .class ;
326
- }
327
- }
338
+ private static boolean isGeoField (java .lang .reflect .Field field ) {
339
+ return field .getType () == GeoPoint .class || field .getAnnotation (GeoPointField .class ) != null ;
340
+ }
341
+
342
+ private static boolean isCompletionField (java .lang .reflect .Field field ) {
343
+ return field .getType () == Completion .class ;
344
+ }
345
+ }
0 commit comments