Skip to content

Mappingbuilder uses the typeKey from the ElasticsearchTypeMapper instance. #2044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ public DefaultElasticsearchTypeMapper(@Nullable String typeKey, TypeAliasAccesso
this.typeKey = typeKey;
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.MongoTypeMapper#isTypeKey(java.lang.String)
*/
@Override
public boolean isTypeKey(String key) {
return typeKey != null && typeKey.equals(key);
}

@Override
@Nullable
public String getTypeKey() {
return typeKey;
}

/*
* (non-Javadoc)
* @see org.springframework.data.convert.DefaultTypeMapper#getFallbackTypeFor(java.lang.Object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.lang.Nullable;

/**
* Elasticsearch specific {@link TypeMapper} definition.
Expand All @@ -39,6 +40,13 @@ public interface ElasticsearchTypeMapper extends TypeMapper<Map<String, Object>>
*/
boolean isTypeKey(String key);

/**
* @return the type key.
* @since 4.4
*/
@Nullable
String getTypeKey();

default boolean containsTypeInformation(Map<String, Object> source) {
return readType(source) != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class MappingElasticsearchConverter
private CustomConversions conversions = new ElasticsearchCustomConversions(Collections.emptyList());
private final SpELContext spELContext = new SpELContext(new MapAccessor());
private final EntityInstantiators instantiators = new EntityInstantiators();
private final ElasticsearchTypeMapper typeMapper;

public MappingElasticsearchConverter(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
Expand All @@ -106,6 +107,7 @@ public MappingElasticsearchConverter(

this.mappingContext = mappingContext;
this.conversionService = conversionService != null ? conversionService : new DefaultConversionService();
this.typeMapper = ElasticsearchTypeMapper.create(mappingContext);
}

@Override
Expand Down Expand Up @@ -145,12 +147,16 @@ public void afterPropertiesSet() {
conversions.registerConvertersIn(conversionService);
}

public ElasticsearchTypeMapper getTypeMapper() {
return typeMapper;
}

// region read/write

@Override
public <R> R read(Class<R> type, Document source) {

Reader reader = new Reader(mappingContext, conversionService, conversions, spELContext, instantiators);
Reader reader = new Reader(mappingContext, conversionService, conversions, typeMapper, spELContext, instantiators);
return reader.read(type, source);
}

Expand All @@ -159,7 +165,7 @@ public void write(Object source, Document sink) {

Assert.notNull(source, "source to map must not be null");

Writer writer = new Writer(mappingContext, conversionService, conversions);
Writer writer = new Writer(mappingContext, conversionService, conversions, typeMapper);
writer.write(source, sink);
}

Expand All @@ -176,11 +182,11 @@ private static class Base {

private Base(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
GenericConversionService conversionService, CustomConversions conversions) {
GenericConversionService conversionService, CustomConversions conversions, ElasticsearchTypeMapper typeMapper) {
this.mappingContext = mappingContext;
this.conversionService = conversionService;
this.conversions = conversions;
this.typeMapper = ElasticsearchTypeMapper.create(mappingContext);
this.typeMapper = typeMapper;
}
}

Expand All @@ -195,10 +201,10 @@ private static class Reader extends Base {

public Reader(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
GenericConversionService conversionService, CustomConversions conversions, SpELContext spELContext,
EntityInstantiators instantiators) {
GenericConversionService conversionService, CustomConversions conversions, ElasticsearchTypeMapper typeMapper,
SpELContext spELContext, EntityInstantiators instantiators) {

super(mappingContext, conversionService, conversions);
super(mappingContext, conversionService, conversions, typeMapper);
this.spELContext = spELContext;
this.instantiators = instantiators;
}
Expand Down Expand Up @@ -670,8 +676,8 @@ static private class Writer extends Base {

public Writer(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
GenericConversionService conversionService, CustomConversions conversions) {
super(mappingContext, conversionService, conversions);
GenericConversionService conversionService, CustomConversions conversions, ElasticsearchTypeMapper typeMapper) {
super(mappingContext, conversionService, conversions, typeMapper);
}

void write(Object source, Document sink) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.springframework.data.elasticsearch.backend.elasticsearch7.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ResourceUtil;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchTypeMapper;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
Expand Down Expand Up @@ -88,7 +90,7 @@ public class MappingBuilder {
private static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length";
private static final String COMPLETION_CONTEXTS = "contexts";

private static final String TYPEHINT_PROPERTY = "_class";
private static final String TYPEHINT_PROPERTY = ElasticsearchTypeMapper.DEFAULT_TYPE_KEY;

private static final String TYPE_DYNAMIC = "dynamic";
private static final String TYPE_VALUE_KEYWORD = "keyword";
Expand Down Expand Up @@ -189,7 +191,17 @@ protected String buildPropertyMapping(ElasticsearchPersistentEntity<?> entity,
private void writeTypeHintMapping(ObjectNode propertiesNode) throws IOException {

if (writeTypeHints) {
propertiesNode.set(TYPEHINT_PROPERTY, objectMapper.createObjectNode() //
String typeHintProperty = null;

if (elasticsearchConverter instanceof MappingElasticsearchConverter) {
typeHintProperty = ((MappingElasticsearchConverter) elasticsearchConverter).getTypeMapper().getTypeKey();
}

if (typeHintProperty == null) {
typeHintProperty = TYPEHINT_PROPERTY;
}

propertiesNode.set(typeHintProperty, objectMapper.createObjectNode() //
.put(FIELD_PARAM_TYPE, TYPE_VALUE_KEYWORD) //
.put(FIELD_PARAM_INDEX, false) //
.put(FIELD_PARAM_DOC_VALUES, false));
Expand Down