BooleanFieldMapper.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package org.es.mapping.mapper;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.elasticsearch.common.xcontent.XContentBuilder;
  4. import org.es.mapping.annotations.fieldtype.BooleanField;
  5. import org.es.mapping.utils.BeanUtils;
  6. import java.io.IOException;
  7. import java.lang.reflect.Field;
  8. public class BooleanFieldMapper {
  9. public static boolean isValidBooleanType(Field field) {
  10. if (BeanUtils.isCollectionType(field)) {
  11. if (!BeanUtils.isValidCollectionType(field)) {
  12. throw new IllegalArgumentException(
  13. String.format("Unsupported list class type, name[%s].", field.getName()));
  14. }
  15. Class genericTypeClass = (Class) BeanUtils.getCollectionGenericType(field);
  16. return Boolean.class == genericTypeClass || boolean.class == genericTypeClass;
  17. }
  18. Class<?> fieldClass = field.getType();
  19. return Boolean.class == fieldClass || boolean.class == fieldClass;
  20. }
  21. public static void mapDataType(XContentBuilder mappingBuilder, BooleanField booleanField) throws IOException {
  22. mappingBuilder.field("type", "boolean");
  23. if (booleanField.boost() != 1.0f) {
  24. mappingBuilder.field("boost", booleanField.boost());
  25. }
  26. if (!booleanField.doc_values()) {
  27. mappingBuilder.field("doc_values", booleanField.doc_values());
  28. }
  29. if (!booleanField.index()) {
  30. mappingBuilder.field("index", booleanField.index());
  31. }
  32. if (StringUtils.isNotBlank(booleanField.null_value())) {
  33. mappingBuilder.field("null_value", booleanField.null_value());
  34. }
  35. if (booleanField.store()) {
  36. mappingBuilder.field("store", booleanField.store());
  37. }
  38. }
  39. public static void mapDataType(XContentBuilder mappingBuilder, Field field) throws IOException {
  40. if (!isValidBooleanType(field)) {
  41. throw new IllegalArgumentException(
  42. String.format("field type[%s] is invalid type of boolean.", field.getType()));
  43. }
  44. if (field.isAnnotationPresent(BooleanField.class)) {
  45. BooleanField booleanField = field.getDeclaredAnnotation(BooleanField.class);
  46. mapDataType(mappingBuilder, booleanField);
  47. return;
  48. }
  49. mappingBuilder.field("type", "boolean");
  50. }
  51. }