Search in sources :

Example 1 with PropertyValueConverter

use of org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter in project spring-data-elasticsearch by spring-projects.

the class MappingElasticsearchConverter method updateCriteria.

private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?> persistentEntity) {
    Field field = criteria.getField();
    if (field == null) {
        return;
    }
    String[] fieldNames = field.getName().split("\\.");
    ElasticsearchPersistentEntity<?> currentEntity = persistentEntity;
    ElasticsearchPersistentProperty persistentProperty = null;
    int propertyCount = 0;
    boolean isNested = false;
    for (int i = 0; i < fieldNames.length; i++) {
        persistentProperty = currentEntity.getPersistentProperty(fieldNames[i]);
        if (persistentProperty != null) {
            propertyCount++;
            fieldNames[i] = persistentProperty.getFieldName();
            org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = persistentProperty.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
            if (fieldAnnotation != null && fieldAnnotation.type() == FieldType.Nested) {
                isNested = true;
            }
            try {
                currentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
            } catch (Exception e) {
                // using system types like UUIDs will lead to java.lang.reflect.InaccessibleObjectException in JDK 16
                // so if we cannot get an entity here, bail out.
                currentEntity = null;
            }
        }
        if (currentEntity == null) {
            break;
        }
    }
    field.setName(String.join(".", fieldNames));
    if (propertyCount > 1 && isNested) {
        List<String> propertyNames = Arrays.asList(fieldNames);
        field.setPath(String.join(".", propertyNames.subList(0, propertyCount - 1)));
    }
    if (persistentProperty != null) {
        if (persistentProperty.hasPropertyValueConverter()) {
            PropertyValueConverter propertyValueConverter = Objects.requireNonNull(persistentProperty.getPropertyValueConverter());
            criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
                if (criteriaEntry.getKey().hasValue()) {
                    Object value = criteriaEntry.getValue();
                    if (value.getClass().isArray()) {
                        Object[] objects = (Object[]) value;
                        for (int i = 0; i < objects.length; i++) {
                            objects[i] = propertyValueConverter.write(objects[i]);
                        }
                    } else {
                        criteriaEntry.setValue(propertyValueConverter.write(value));
                    }
                }
            });
        }
        org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = persistentProperty.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
        if (fieldAnnotation != null) {
            field.setFieldType(fieldAnnotation.type());
        }
    }
}
Also used : InstanceCreatorMetadata(org.springframework.data.mapping.InstanceCreatorMetadata) MappingException(org.springframework.data.mapping.MappingException) BeansException(org.springframework.beans.BeansException) ScriptedField(org.springframework.data.elasticsearch.annotations.ScriptedField) Field(org.springframework.data.elasticsearch.core.query.Field) PropertyValueConverter(org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)

Example 2 with PropertyValueConverter

use of org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter in project spring-data-elasticsearch by spring-projects.

the class PropertyValueConvertersUnitTests method propertyValueConverters.

static Stream<Arguments> propertyValueConverters() {
    SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
    SimpleElasticsearchPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(NoConverterForThisClass.class);
    ElasticsearchPersistentProperty persistentProperty = persistentEntity.getRequiredPersistentProperty("property");
    List<PropertyValueConverter> converters = new ArrayList<>();
    converters.add(new DatePropertyValueConverter(persistentProperty, Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
    converters.add(new DateRangePropertyValueConverter(persistentProperty, Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
    converters.add(new NumberRangePropertyValueConverter(persistentProperty));
    converters.add(new TemporalPropertyValueConverter(persistentProperty, Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
    converters.add(new TemporalRangePropertyValueConverter(persistentProperty, Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
    return converters.stream().map(propertyValueConverter -> arguments(Named.of(propertyValueConverter.getClass().getSimpleName(), propertyValueConverter)));
}
Also used : SimpleElasticsearchMappingContext(org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext) PropertyValueConverter(org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty) ArrayList(java.util.ArrayList)

Aggregations

ElasticsearchPersistentProperty (org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)2 PropertyValueConverter (org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter)2 ArrayList (java.util.ArrayList)1 BeansException (org.springframework.beans.BeansException)1 ScriptedField (org.springframework.data.elasticsearch.annotations.ScriptedField)1 SimpleElasticsearchMappingContext (org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext)1 Field (org.springframework.data.elasticsearch.core.query.Field)1 InstanceCreatorMetadata (org.springframework.data.mapping.InstanceCreatorMetadata)1 MappingException (org.springframework.data.mapping.MappingException)1