Add Map to ElasticsearchSimpleTypes.

See #1675.

(cherry picked from commit 27850e96f211b093a82f2e8a69d13bdc3364d50c)
This commit is contained in:
Mark Paluch 2021-02-02 10:27:45 +01:00 committed by Peter-Josef Meisch
parent d811ff2d78
commit 8fa261360f
No known key found for this signature in database
GPG Key ID: DE108246970C7708
3 changed files with 38 additions and 22 deletions

View File

@ -277,28 +277,26 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera
routingResolver); routingResolver);
adaptibleEntity.populateIdIfNecessary(indexedObjectInformation.getId()); adaptibleEntity.populateIdIfNecessary(indexedObjectInformation.getId());
ElasticsearchPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass()); ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity); if (persistentEntity != null) {
PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null
&& persistentEntity.hasSeqNoPrimaryTermProperty()) { && persistentEntity.hasSeqNoPrimaryTermProperty()) {
ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty(); ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
propertyAccessor.setProperty(seqNoPrimaryTermProperty, propertyAccessor.setProperty(seqNoPrimaryTermProperty,
new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm())); new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
} }
if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) { if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty(); ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion()); propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
}
} }
return entity; return entity;
} }
private ElasticsearchPersistentEntity<?> getRequiredPersistentEntity(Class<?> clazz) {
return converter.getMappingContext().getRequiredPersistentEntity(clazz);
}
@Override @Override
public <T> Flux<T> multiGet(Query query, Class<T> clazz) { public <T> Flux<T> multiGet(Query query, Class<T> clazz) {
return multiGet(query, clazz, getIndexCoordinatesFor(clazz)); return multiGet(query, clazz, getIndexCoordinatesFor(clazz));

View File

@ -107,6 +107,7 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*; import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -1730,12 +1731,18 @@ class RequestFactory {
@Nullable @Nullable
private String getPersistentEntityId(Object entity) { private String getPersistentEntityId(Object entity) {
Object identifier = elasticsearchConverter.getMappingContext() // MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext = elasticsearchConverter
.getRequiredPersistentEntity(entity.getClass()) // .getMappingContext();
.getIdentifierAccessor(entity).getIdentifier();
if (identifier != null) { ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entity.getClass());
return identifier.toString();
if (persistentEntity != null) {
Object identifier = persistentEntity //
.getIdentifierAccessor(entity).getIdentifier();
if (identifier != null) {
return identifier.toString();
}
} }
return null; return null;
@ -1743,8 +1750,16 @@ class RequestFactory {
private VersionType retrieveVersionTypeFromPersistentEntity(Class<?> clazz) { private VersionType retrieveVersionTypeFromPersistentEntity(Class<?> clazz) {
VersionType versionType = elasticsearchConverter.getMappingContext().getRequiredPersistentEntity(clazz) MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext = elasticsearchConverter
.getVersionType(); .getMappingContext();
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
VersionType versionType = null;
if (persistentEntity != null) {
versionType = persistentEntity.getVersionType();
}
return versionType != null ? versionType : VersionType.EXTERNAL; return versionType != null ? versionType : VersionType.EXTERNAL;
} }

View File

@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch.core.mapping;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.springframework.data.elasticsearch.core.document.Document; import org.springframework.data.elasticsearch.core.document.Document;
@ -40,6 +41,8 @@ public class ElasticsearchSimpleTypes {
Set<Class<?>> simpleTypes = new HashSet<>(); Set<Class<?>> simpleTypes = new HashSet<>();
simpleTypes.add(Document.class); simpleTypes.add(Document.class);
simpleTypes.add(Map.class);
ELASTICSEARCH_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes); ELASTICSEARCH_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
} }