Search in sources :

Example 1 with Field

use of org.springframework.data.elasticsearch.annotations.Field in project spring-data-elasticsearch by spring-projects.

the class SimpleElasticsearchPersistentEntity method processSettingAnnotation.

private void processSettingAnnotation(Setting settingAnnotation, SettingsParameter settingsParameter) {
    settingsParameter.useServerConfiguration = settingAnnotation.useServerConfiguration();
    settingsParameter.settingPath = settingAnnotation.settingPath();
    settingsParameter.shards = settingAnnotation.shards();
    settingsParameter.replicas = settingAnnotation.replicas();
    settingsParameter.refreshIntervall = settingAnnotation.refreshInterval();
    settingsParameter.indexStoreType = settingAnnotation.indexStoreType();
    String[] sortFields = settingAnnotation.sortFields();
    if (sortFields.length > 0) {
        String[] fieldNames = new String[sortFields.length];
        int index = 0;
        for (String propertyName : sortFields) {
            ElasticsearchPersistentProperty property = getPersistentProperty(propertyName);
            if (property == null) {
                throw new IllegalArgumentException("sortField property " + propertyName + " not found");
            }
            Field fieldAnnotation = property.getRequiredAnnotation(Field.class);
            FieldType fieldType = fieldAnnotation.type();
            switch(fieldType) {
                case Boolean:
                case Long:
                case Integer:
                case Short:
                case Byte:
                case Float:
                case Half_Float:
                case Scaled_Float:
                case Date:
                case Date_Nanos:
                case Keyword:
                    break;
                default:
                    throw new IllegalArgumentException("field type " + fieldType + " not allowed for sortField");
            }
            if (!fieldAnnotation.docValues()) {
                throw new IllegalArgumentException("doc_values must be set to true for sortField");
            }
            fieldNames[index++] = property.getFieldName();
        }
        settingsParameter.sortFields = fieldNames;
        Setting.SortOrder[] sortOrders = settingAnnotation.sortOrders();
        if (sortOrders.length > 0) {
            if (sortOrders.length != sortFields.length) {
                throw new IllegalArgumentException("@Settings parameter sortFields and sortOrders must have the same size");
            }
            settingsParameter.sortOrders = sortOrders;
        }
        Setting.SortMode[] sortModes = settingAnnotation.sortModes();
        if (sortModes.length > 0) {
            if (sortModes.length != sortFields.length) {
                throw new IllegalArgumentException("@Settings parameter sortFields and sortModes must have the same size");
            }
            settingsParameter.sortModes = sortModes;
        }
        Setting.SortMissing[] sortMissingValues = settingAnnotation.sortMissingValues();
        if (sortMissingValues.length > 0) {
            if (sortMissingValues.length != sortFields.length) {
                throw new IllegalArgumentException("@Settings parameter sortFields and sortMissingValues must have the same size");
            }
            settingsParameter.sortMissingValues = sortMissingValues;
        }
    }
}
Also used : FieldType(org.springframework.data.elasticsearch.annotations.FieldType) Field(org.springframework.data.elasticsearch.annotations.Field) JoinField(org.springframework.data.elasticsearch.core.join.JoinField)

Example 2 with Field

use of org.springframework.data.elasticsearch.annotations.Field in project spring-data-elasticsearch by spring-projects.

the class SimpleElasticsearchPersistentProperty method initPropertyValueConverter.

/**
 * Initializes the property converter for this {@link PersistentProperty}, if any.
 */
private void initPropertyValueConverter() {
    initPropertyValueConverterFromAnnotation();
    if (hasPropertyValueConverter()) {
        return;
    }
    Class<?> actualType = getActualTypeOrNull();
    if (actualType == null) {
        return;
    }
    Field field = findAnnotation(Field.class);
    if (field == null) {
        return;
    }
    switch(field.type()) {
        case Date:
        case Date_Nanos:
            {
                List<ElasticsearchDateConverter> dateConverters = getDateConverters(field, actualType);
                if (dateConverters.isEmpty()) {
                    LOGGER.warn(String.format("No date formatters configured for property '%s'.", getName()));
                    return;
                }
                if (TemporalAccessor.class.isAssignableFrom(actualType)) {
                    propertyValueConverter = new TemporalPropertyValueConverter(this, dateConverters);
                } else if (Date.class.isAssignableFrom(actualType)) {
                    propertyValueConverter = new DatePropertyValueConverter(this, dateConverters);
                } else {
                    LOGGER.warn(String.format("Unsupported type '%s' for date property '%s'.", actualType, getName()));
                }
                break;
            }
        case Date_Range:
            {
                if (!Range.class.isAssignableFrom(actualType)) {
                    return;
                }
                List<ElasticsearchDateConverter> dateConverters = getDateConverters(field, actualType);
                if (dateConverters.isEmpty()) {
                    LOGGER.warn(String.format("No date formatters configured for property '%s'.", getName()));
                    return;
                }
                Class<?> genericType = getTypeInformation().getTypeArguments().get(0).getType();
                if (TemporalAccessor.class.isAssignableFrom(genericType)) {
                    propertyValueConverter = new TemporalRangePropertyValueConverter(this, dateConverters);
                } else if (Date.class.isAssignableFrom(genericType)) {
                    propertyValueConverter = new DateRangePropertyValueConverter(this, dateConverters);
                } else {
                    LOGGER.warn(String.format("Unsupported generic type '{%s' for date range property '%s'.", genericType, getName()));
                }
                break;
            }
        case Integer_Range:
        case Float_Range:
        case Long_Range:
        case Double_Range:
            {
                if (!Range.class.isAssignableFrom(actualType)) {
                    return;
                }
                Class<?> genericType = getTypeInformation().getTypeArguments().get(0).getType();
                if ((field.type() == FieldType.Integer_Range && !Integer.class.isAssignableFrom(genericType)) || (field.type() == FieldType.Float_Range && !Float.class.isAssignableFrom(genericType)) || (field.type() == FieldType.Long_Range && !Long.class.isAssignableFrom(genericType)) || (field.type() == FieldType.Double_Range && !Double.class.isAssignableFrom(genericType))) {
                    LOGGER.warn(String.format("Unsupported generic type '%s' for range field type '%s' of property '%s'.", genericType, field.type(), getName()));
                    return;
                }
                propertyValueConverter = new NumberRangePropertyValueConverter(this);
                break;
            }
        case Ip_Range:
            {
            // TODO currently unsupported, needs a library like https://seancfoley.github.io/IPAddress/
            }
        default:
            break;
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalRangePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.TemporalRangePropertyValueConverter) DatePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.DatePropertyValueConverter) Field(org.springframework.data.elasticsearch.annotations.Field) GeoPointField(org.springframework.data.elasticsearch.annotations.GeoPointField) MultiField(org.springframework.data.elasticsearch.annotations.MultiField) GeoShapeField(org.springframework.data.elasticsearch.annotations.GeoShapeField) JoinField(org.springframework.data.elasticsearch.core.join.JoinField) TemporalPropertyValueConverter(org.springframework.data.elasticsearch.core.convert.TemporalPropertyValueConverter) DateRangePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter) NumberRangePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.NumberRangePropertyValueConverter) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Field (org.springframework.data.elasticsearch.annotations.Field)2 JoinField (org.springframework.data.elasticsearch.core.join.JoinField)2 TemporalAccessor (java.time.temporal.TemporalAccessor)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FieldType (org.springframework.data.elasticsearch.annotations.FieldType)1 GeoPointField (org.springframework.data.elasticsearch.annotations.GeoPointField)1 GeoShapeField (org.springframework.data.elasticsearch.annotations.GeoShapeField)1 MultiField (org.springframework.data.elasticsearch.annotations.MultiField)1 DatePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.DatePropertyValueConverter)1 DateRangePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter)1 NumberRangePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.NumberRangePropertyValueConverter)1 TemporalPropertyValueConverter (org.springframework.data.elasticsearch.core.convert.TemporalPropertyValueConverter)1 TemporalRangePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.TemporalRangePropertyValueConverter)1