Refactor MappingElasticsearchConverter.

* Add support for SpEL expressions via @Value.
* Simplify readCollectionOrArray to consider properly nested lists and maps
* Simplify readMap to allow reading generic maps and entities in maps.
* Report a fallback TypeInformation in DefaultElasticsearchTypeMapper to properly convert nested maps.

We now no longer rely on isSimpleType when writing Maps. This is the preparation to consider Map as simple type.

Resolves #1676.
See #1675.

(cherry picked from commit 877de9c51ce94e5efe294944cf752b20ac2f2cd6)
This commit is contained in:
Mark Paluch 2021-02-01 11:49:06 +01:00 committed by Peter-Josef Meisch
parent 63eebdea88
commit a2baea7792
No known key found for this signature in database
GPG Key ID: DE108246970C7708
3 changed files with 565 additions and 287 deletions

View File

@ -26,6 +26,8 @@ import org.springframework.data.convert.TypeInformationMapper;
import org.springframework.data.mapping.Alias;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
/**
@ -38,6 +40,9 @@ import org.springframework.lang.Nullable;
public class DefaultElasticsearchTypeMapper extends DefaultTypeMapper<Map<String, Object>>
implements ElasticsearchTypeMapper {
@SuppressWarnings("rawtypes") //
private static final TypeInformation<Map> MAP_TYPE_INFO = ClassTypeInformation.from(Map.class);
private final @Nullable String typeKey;
public DefaultElasticsearchTypeMapper(@Nullable String typeKey) {
@ -62,11 +67,23 @@ public class DefaultElasticsearchTypeMapper extends DefaultTypeMapper<Map<String
this.typeKey = typeKey;
}
@Override
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.MongoTypeMapper#isTypeKey(java.lang.String)
*/
public boolean isTypeKey(String key) {
return typeKey != null && typeKey.equals(key);
}
/*
* (non-Javadoc)
* @see org.springframework.data.convert.DefaultTypeMapper#getFallbackTypeFor(java.lang.Object)
*/
@Override
protected TypeInformation<?> getFallbackTypeFor(Map<String, Object> source) {
return MAP_TYPE_INFO;
}
/**
* {@link TypeAliasAccessor} to store aliases in a {@link Map}.
*

View File

@ -19,10 +19,14 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.mapping.model.SimpleTypeHolder;
/**
* Utility to define simple types understood by Spring Data Elasticsearch and the Elasticsearch client.
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 3.2
*/
public class ElasticsearchSimpleTypes {
@ -35,6 +39,7 @@ public class ElasticsearchSimpleTypes {
AUTOGENERATED_ID_TYPES = Collections.unmodifiableSet(classes);
Set<Class<?>> simpleTypes = new HashSet<>();
simpleTypes.add(Document.class);
ELASTICSEARCH_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
}