Search in sources :

Example 1 with FieldType

use of org.springframework.data.elasticsearch.annotations.FieldType 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)

Aggregations

Field (org.springframework.data.elasticsearch.annotations.Field)1 FieldType (org.springframework.data.elasticsearch.annotations.FieldType)1 JoinField (org.springframework.data.elasticsearch.core.join.JoinField)1