Search in sources :

Example 1 with ElasticsearchPersistentProperty

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

the class ComposableAnnotationsUnitTest method fieldAnnotationShouldBeComposable.

// DATAES-362
@Test
@DisplayName("Field annotation should be composable")
void fieldAnnotationShouldBeComposable() {
    SimpleElasticsearchPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(ComposedAnnotationEntity.class);
    ElasticsearchPersistentProperty property = entity.getRequiredPersistentProperty("nullValue");
    assertThat(property.getFieldName()).isEqualTo("null-value");
    assertThat(property.storeNullValue()).isTrue();
}
Also used : ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with ElasticsearchPersistentProperty

use of org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty 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 3 with ElasticsearchPersistentProperty

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

the class ReactiveElasticsearchTemplate method updateIndexedObject.

private <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
    ElasticsearchPersistentEntity<?> persistentEntity = converter.getMappingContext().getPersistentEntity(entity.getClass());
    if (persistentEntity != null) {
        PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
        ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
        // Only deal with text because ES generated Ids are strings!
        if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
            propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
        }
        if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null && persistentEntity.hasSeqNoPrimaryTermProperty()) {
            ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(seqNoPrimaryTermProperty, new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
        }
        if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
            ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
        }
        // noinspection unchecked
        T updatedEntity = (T) propertyAccessor.getBean();
        return updatedEntity;
    } else {
        AdaptibleEntity<T> adaptibleEntity = operations.forEntity(entity, converter.getConversionService(), routingResolver);
        adaptibleEntity.populateIdIfNecessary(indexedObjectInformation.getId());
    }
    return entity;
}
Also used : SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)

Example 4 with ElasticsearchPersistentProperty

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

the class AbstractElasticsearchTemplate method updateIndexedObject.

protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
    ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext().getPersistentEntity(entity.getClass());
    if (persistentEntity != null) {
        PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
        ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
        // Only deal with text because ES generated Ids are strings!
        if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
            propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
        }
        if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null && persistentEntity.hasSeqNoPrimaryTermProperty()) {
            ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(seqNoPrimaryTermProperty, new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
        }
        if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
            ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
        }
        // noinspection unchecked
        return (T) propertyAccessor.getBean();
    }
    return entity;
}
Also used : SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)

Example 5 with ElasticsearchPersistentProperty

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

the class SearchHitMapping method getPersistentEntity.

/**
 * find a {@link ElasticsearchPersistentEntity} following the property chain defined by the nested metadata
 *
 * @param persistentEntity base entity
 * @param nestedMetaData nested metadata
 * @return A {@link ElasticsearchPersistentEntityWithNestedMetaData} containing the found entity or null together with
 *         the {@link NestedMetaData} that has mapped field names.
 */
private ElasticsearchPersistentEntityWithNestedMetaData getPersistentEntity(@Nullable ElasticsearchPersistentEntity<?> persistentEntity, @Nullable NestedMetaData nestedMetaData) {
    NestedMetaData currentMetaData = nestedMetaData;
    List<NestedMetaData> mappedNestedMetaDatas = new LinkedList<>();
    while (persistentEntity != null && currentMetaData != null) {
        ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentPropertyWithFieldName(currentMetaData.getField());
        if (persistentProperty == null) {
            persistentEntity = null;
        } else {
            persistentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
            mappedNestedMetaDatas.add(0, NestedMetaData.of(persistentProperty.getName(), currentMetaData.getOffset(), null));
            currentMetaData = currentMetaData.getChild();
        }
    }
    NestedMetaData mappedNestedMetaData = mappedNestedMetaDatas.stream().reduce(null, (result, nmd) -> NestedMetaData.of(nmd.getField(), nmd.getOffset(), result));
    return new ElasticsearchPersistentEntityWithNestedMetaData(persistentEntity, mappedNestedMetaData);
}
Also used : NestedMetaData(org.springframework.data.elasticsearch.core.document.NestedMetaData) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty) LinkedList(java.util.LinkedList)

Aggregations

ElasticsearchPersistentProperty (org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)8 PropertyValueConverter (org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter)2 SeqNoPrimaryTerm (org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 VersionType (org.elasticsearch.index.VersionType)1 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)1 GeoDistanceSortBuilder (org.elasticsearch.search.sort.GeoDistanceSortBuilder)1 SortOrder (org.elasticsearch.search.sort.SortOrder)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1 BeansException (org.springframework.beans.BeansException)1 ScriptedField (org.springframework.data.elasticsearch.annotations.ScriptedField)1 Document (org.springframework.data.elasticsearch.core.document.Document)1 NestedMetaData (org.springframework.data.elasticsearch.core.document.NestedMetaData)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