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;
}
}
}
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;
}
}
Aggregations