|
51 | 51 | * @author Pavel Luhin
|
52 | 52 | * @author Mark Paluch
|
53 | 53 | * @author Sascha Woo
|
| 54 | + * @author Nordine Bittich |
54 | 55 | */
|
55 | 56 | class MappingBuilder {
|
56 | 57 |
|
@@ -221,85 +222,103 @@ private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder,
|
221 | 222 | }
|
222 | 223 |
|
223 | 224 | /**
|
224 |
| - * Apply mapping for a single @Field annotation |
| 225 | + * Add mapping for @Field annotation |
225 | 226 | *
|
226 | 227 | * @throws IOException
|
227 | 228 | */
|
228 |
| - private static void addSingleFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field, |
229 |
| - Field fieldAnnotation, boolean nestedOrObjectField) throws IOException { |
230 |
| - xContentBuilder.startObject(field.getName()); |
231 |
| - if(!nestedOrObjectField) { |
232 |
| - xContentBuilder.field(FIELD_STORE, fieldAnnotation.store()); |
233 |
| - } |
234 |
| - if(fieldAnnotation.fielddata()) { |
235 |
| - xContentBuilder.field(FIELD_DATA, fieldAnnotation.fielddata()); |
236 |
| - } |
237 |
| - |
238 |
| - if (FieldType.Auto != fieldAnnotation.type()) { |
239 |
| - xContentBuilder.field(FIELD_TYPE, fieldAnnotation.type().name().toLowerCase()); |
240 |
| - if (FieldType.Date == fieldAnnotation.type() && DateFormat.none != fieldAnnotation.format()) { |
241 |
| - xContentBuilder.field(FIELD_FORMAT, DateFormat.custom == fieldAnnotation.format() |
242 |
| - ? fieldAnnotation.pattern() : fieldAnnotation.format()); |
243 |
| - } |
244 |
| - } |
245 |
| - if(!fieldAnnotation.index()) { |
246 |
| - xContentBuilder.field(FIELD_INDEX, fieldAnnotation.index()); |
247 |
| - } |
248 |
| - if (isNotBlank(fieldAnnotation.searchAnalyzer())) { |
249 |
| - xContentBuilder.field(FIELD_SEARCH_ANALYZER, fieldAnnotation.searchAnalyzer()); |
250 |
| - } |
251 |
| - if (isNotBlank(fieldAnnotation.analyzer())) { |
252 |
| - xContentBuilder.field(FIELD_INDEX_ANALYZER, fieldAnnotation.analyzer()); |
253 |
| - } |
254 |
| - xContentBuilder.endObject(); |
255 |
| - } |
256 |
| - |
257 |
| - /** |
258 |
| - * Apply mapping for a single nested @Field annotation |
259 |
| - * |
260 |
| - * @throws IOException |
261 |
| - */ |
262 |
| - private static void addNestedFieldMapping(XContentBuilder builder, java.lang.reflect.Field field, |
263 |
| - InnerField annotation) throws IOException { |
264 |
| - builder.startObject(annotation.suffix()); |
265 |
| - //builder.field(FIELD_STORE, annotation.store()); |
266 |
| - if (FieldType.Auto != annotation.type()) { |
267 |
| - builder.field(FIELD_TYPE, annotation.type().name().toLowerCase()); |
268 |
| - } |
269 |
| - if(!annotation.index()) { |
270 |
| - builder.field(FIELD_INDEX, annotation.index()); |
271 |
| - } |
272 |
| - if (isNotBlank(annotation.searchAnalyzer())) { |
273 |
| - builder.field(FIELD_SEARCH_ANALYZER, annotation.searchAnalyzer()); |
274 |
| - } |
275 |
| - if (isNotBlank(annotation.indexAnalyzer())) { |
276 |
| - builder.field(FIELD_INDEX_ANALYZER, annotation.indexAnalyzer()); |
277 |
| - } |
278 |
| - if (annotation.fielddata()) { |
279 |
| - builder.field(FIELD_DATA, annotation.fielddata()); |
280 |
| - } |
| 229 | + private static void addSingleFieldMapping(XContentBuilder builder, java.lang.reflect.Field field, Field annotation, boolean nestedOrObjectField) throws IOException { |
| 230 | + builder.startObject(field.getName()); |
| 231 | + addFieldMappingParameters(builder, annotation, nestedOrObjectField); |
281 | 232 | builder.endObject();
|
282 | 233 | }
|
283 | 234 |
|
284 | 235 | /**
|
285 |
| - * Multi field mappings for string type fields, support for sorts and facets |
| 236 | + * Add mapping for @MultiField annotation |
286 | 237 | *
|
287 | 238 | * @throws IOException
|
288 | 239 | */
|
289 |
| - private static void addMultiFieldMapping(XContentBuilder builder, java.lang.reflect.Field field, |
290 |
| - MultiField annotation, boolean nestedOrObjectField) throws IOException { |
| 240 | + private static void addMultiFieldMapping( |
| 241 | + XContentBuilder builder, |
| 242 | + java.lang.reflect.Field field, |
| 243 | + MultiField annotation, |
| 244 | + boolean nestedOrObjectField) throws IOException { |
| 245 | + |
| 246 | + // main field |
291 | 247 | builder.startObject(field.getName());
|
292 |
| - builder.field(FIELD_TYPE, annotation.mainField().type().name().toLowerCase()); |
| 248 | + addFieldMappingParameters(builder, annotation.mainField(), nestedOrObjectField); |
| 249 | + |
| 250 | + // inner fields |
293 | 251 | builder.startObject("fields");
|
294 |
| - //add standard field |
295 |
| - //addSingleFieldMapping(builder, field, annotation.mainField(), nestedOrObjectField); |
296 | 252 | for (InnerField innerField : annotation.otherFields()) {
|
297 |
| - addNestedFieldMapping(builder, field, innerField); |
| 253 | + builder.startObject(innerField.suffix()); |
| 254 | + addFieldMappingParameters(builder, innerField, false); |
| 255 | + builder.endObject(); |
298 | 256 | }
|
299 | 257 | builder.endObject();
|
| 258 | + |
300 | 259 | builder.endObject();
|
301 | 260 | }
|
302 | 261 |
|
| 262 | + private static void addFieldMappingParameters(XContentBuilder builder, Object annotation, boolean nestedOrObjectField) throws IOException { |
| 263 | + boolean index = true; |
| 264 | + boolean store = false; |
| 265 | + boolean fielddata = false; |
| 266 | + FieldType type = null; |
| 267 | + DateFormat dateFormat = null; |
| 268 | + String datePattern = null; |
| 269 | + String analyzer = null; |
| 270 | + String searchAnalyzer = null; |
| 271 | + |
| 272 | + if (annotation instanceof Field) { |
| 273 | + // @Field |
| 274 | + Field fieldAnnotation = (Field) annotation; |
| 275 | + index = fieldAnnotation.index(); |
| 276 | + store = fieldAnnotation.store(); |
| 277 | + fielddata = fieldAnnotation.fielddata(); |
| 278 | + type = fieldAnnotation.type(); |
| 279 | + dateFormat = fieldAnnotation.format(); |
| 280 | + datePattern = fieldAnnotation.pattern(); |
| 281 | + analyzer = fieldAnnotation.analyzer(); |
| 282 | + searchAnalyzer = fieldAnnotation.searchAnalyzer(); |
| 283 | + } else if (annotation instanceof InnerField) { |
| 284 | + // @InnerField |
| 285 | + InnerField fieldAnnotation = (InnerField) annotation; |
| 286 | + index = fieldAnnotation.index(); |
| 287 | + store = fieldAnnotation.store(); |
| 288 | + fielddata = fieldAnnotation.fielddata(); |
| 289 | + type = fieldAnnotation.type(); |
| 290 | + dateFormat = fieldAnnotation.format(); |
| 291 | + datePattern = fieldAnnotation.pattern(); |
| 292 | + analyzer = fieldAnnotation.analyzer(); |
| 293 | + searchAnalyzer = fieldAnnotation.searchAnalyzer(); |
| 294 | + } else { |
| 295 | + throw new IllegalArgumentException("annotation must be an instance of @Field or @InnerField"); |
| 296 | + } |
| 297 | + |
| 298 | + if (!nestedOrObjectField) { |
| 299 | + builder.field(FIELD_STORE, store); |
| 300 | + } |
| 301 | + if (fielddata) { |
| 302 | + builder.field(FIELD_DATA, fielddata); |
| 303 | + } |
| 304 | + if (type != FieldType.Auto) { |
| 305 | + builder.field(FIELD_TYPE, type.name().toLowerCase()); |
| 306 | + |
| 307 | + if (type == FieldType.Date && dateFormat != DateFormat.none) { |
| 308 | + builder.field(FIELD_FORMAT, dateFormat == DateFormat.custom ? datePattern : dateFormat.toString()); |
| 309 | + } |
| 310 | + } |
| 311 | + if (!index) { |
| 312 | + builder.field(FIELD_INDEX, index); |
| 313 | + } |
| 314 | + if (isNotBlank(analyzer)) { |
| 315 | + builder.field(FIELD_INDEX_ANALYZER, analyzer); |
| 316 | + } |
| 317 | + if (isNotBlank(searchAnalyzer)) { |
| 318 | + builder.field(FIELD_SEARCH_ANALYZER, searchAnalyzer); |
| 319 | + } |
| 320 | + } |
| 321 | + |
303 | 322 | protected static boolean isEntity(java.lang.reflect.Field field) {
|
304 | 323 | TypeInformation typeInformation = ClassTypeInformation.from(field.getType());
|
305 | 324 | Class<?> clazz = getFieldType(field);
|
|
0 commit comments