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