Skip to content

Commit d95f62e

Browse files
authored
Mappingbuilder uses the typeKey from the ElasticsearchTypeMapper instance.
Original Pull Request #2044
1 parent f914d68 commit d95f62e

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

src/main/java/org/springframework/data/elasticsearch/core/convert/DefaultElasticsearchTypeMapper.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@ public DefaultElasticsearchTypeMapper(@Nullable String typeKey, TypeAliasAccesso
6767
this.typeKey = typeKey;
6868
}
6969

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

75+
@Override
76+
@Nullable
77+
public String getTypeKey() {
78+
return typeKey;
79+
}
80+
7881
/*
7982
* (non-Javadoc)
8083
* @see org.springframework.data.convert.DefaultTypeMapper#getFallbackTypeFor(java.lang.Object)

src/main/java/org/springframework/data/elasticsearch/core/convert/ElasticsearchTypeMapper.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
2222
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
2323
import org.springframework.data.mapping.context.MappingContext;
24+
import org.springframework.lang.Nullable;
2425

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

43+
/**
44+
* @return the type key.
45+
* @since 4.4
46+
*/
47+
@Nullable
48+
String getTypeKey();
49+
4250
default boolean containsTypeInformation(Map<String, Object> source) {
4351
return readType(source) != null;
4452
}

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public class MappingElasticsearchConverter
9292
private CustomConversions conversions = new ElasticsearchCustomConversions(Collections.emptyList());
9393
private final SpELContext spELContext = new SpELContext(new MapAccessor());
9494
private final EntityInstantiators instantiators = new EntityInstantiators();
95+
private final ElasticsearchTypeMapper typeMapper;
9596

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

107108
this.mappingContext = mappingContext;
108109
this.conversionService = conversionService != null ? conversionService : new DefaultConversionService();
110+
this.typeMapper = ElasticsearchTypeMapper.create(mappingContext);
109111
}
110112

111113
@Override
@@ -145,12 +147,16 @@ public void afterPropertiesSet() {
145147
conversions.registerConvertersIn(conversionService);
146148
}
147149

150+
public ElasticsearchTypeMapper getTypeMapper() {
151+
return typeMapper;
152+
}
153+
148154
// region read/write
149155

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

153-
Reader reader = new Reader(mappingContext, conversionService, conversions, spELContext, instantiators);
159+
Reader reader = new Reader(mappingContext, conversionService, conversions, typeMapper, spELContext, instantiators);
154160
return reader.read(type, source);
155161
}
156162

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

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

162-
Writer writer = new Writer(mappingContext, conversionService, conversions);
168+
Writer writer = new Writer(mappingContext, conversionService, conversions, typeMapper);
163169
writer.write(source, sink);
164170
}
165171

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

177183
private Base(
178184
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
179-
GenericConversionService conversionService, CustomConversions conversions) {
185+
GenericConversionService conversionService, CustomConversions conversions, ElasticsearchTypeMapper typeMapper) {
180186
this.mappingContext = mappingContext;
181187
this.conversionService = conversionService;
182188
this.conversions = conversions;
183-
this.typeMapper = ElasticsearchTypeMapper.create(mappingContext);
189+
this.typeMapper = typeMapper;
184190
}
185191
}
186192

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

196202
public Reader(
197203
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
198-
GenericConversionService conversionService, CustomConversions conversions, SpELContext spELContext,
199-
EntityInstantiators instantiators) {
204+
GenericConversionService conversionService, CustomConversions conversions, ElasticsearchTypeMapper typeMapper,
205+
SpELContext spELContext, EntityInstantiators instantiators) {
200206

201-
super(mappingContext, conversionService, conversions);
207+
super(mappingContext, conversionService, conversions, typeMapper);
202208
this.spELContext = spELContext;
203209
this.instantiators = instantiators;
204210
}
@@ -670,8 +676,8 @@ static private class Writer extends Base {
670676

671677
public Writer(
672678
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
673-
GenericConversionService conversionService, CustomConversions conversions) {
674-
super(mappingContext, conversionService, conversions);
679+
GenericConversionService conversionService, CustomConversions conversions, ElasticsearchTypeMapper typeMapper) {
680+
super(mappingContext, conversionService, conversions, typeMapper);
675681
}
676682

677683
void write(Object source, Document sink) {

src/main/java/org/springframework/data/elasticsearch/core/index/MappingBuilder.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.springframework.data.elasticsearch.backend.elasticsearch7.ElasticsearchRestTemplate;
3636
import org.springframework.data.elasticsearch.core.ResourceUtil;
3737
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
38+
import org.springframework.data.elasticsearch.core.convert.ElasticsearchTypeMapper;
39+
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
3840
import org.springframework.data.elasticsearch.core.document.Document;
3941
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
4042
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
@@ -88,7 +90,7 @@ public class MappingBuilder {
8890
private static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length";
8991
private static final String COMPLETION_CONTEXTS = "contexts";
9092

91-
private static final String TYPEHINT_PROPERTY = "_class";
93+
private static final String TYPEHINT_PROPERTY = ElasticsearchTypeMapper.DEFAULT_TYPE_KEY;
9294

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

191193
if (writeTypeHints) {
192-
propertiesNode.set(TYPEHINT_PROPERTY, objectMapper.createObjectNode() //
194+
String typeHintProperty = null;
195+
196+
if (elasticsearchConverter instanceof MappingElasticsearchConverter) {
197+
typeHintProperty = ((MappingElasticsearchConverter) elasticsearchConverter).getTypeMapper().getTypeKey();
198+
}
199+
200+
if (typeHintProperty == null) {
201+
typeHintProperty = TYPEHINT_PROPERTY;
202+
}
203+
204+
propertiesNode.set(typeHintProperty, objectMapper.createObjectNode() //
193205
.put(FIELD_PARAM_TYPE, TYPE_VALUE_KEYWORD) //
194206
.put(FIELD_PARAM_INDEX, false) //
195207
.put(FIELD_PARAM_DOC_VALUES, false));

0 commit comments

Comments
 (0)