Skip to content

Commit 9696f41

Browse files
authored
DATAES-747 - ElasticsearchConfigurationSupport does not set customConversions into the MappingElasticsearchConverter.
Original PR: spring-projects#395
1 parent 36a3d59 commit 9696f41

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

src/main/asciidoc/reference/elasticsearch-object-mapping.adoc

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
Spring Data Elasticsearch Object Mapping is the process that maps a Java object - the domain entity - into the JSON representation that is stored in Elasticsearch and back.
55

6-
Earlier versions of Spring Data Elasticsearch used a Jackson based conversion, Spring Data Elasticsearch 3.2.x introduced the <<elasticsearch.mapping.meta-model>>. As of version 4.0 only the Meta Object Mapping is used, the Jackson based mapper is not available anymore.
6+
Earlier versions of Spring Data Elasticsearch used a Jackson based conversion, Spring Data Elasticsearch 3.2.x introduced the <<elasticsearch.mapping.meta-model>>. As of version 4.0 only the Meta Object Mapping is used, the Jackson based mapper is not available anymore and the `MappingElasticsearchConverter` is used.
77

88
The main reasons for the removal of the Jackson based mapper are:
99

1010
* Custom mappings of fields needed to be done with annotations like `@JsonFormat` or `@JsonInclude`. This often caused problems when the same object was used in different JSON based datastores or sent over a JSON based API.
1111
* Custom field types and formats also need to be stored into the Elasticsearch index mappings. The Jackson based annotations did not fully provide all the information that is necessary to represent the types of Elasticsearch.
12+
* Fields must be mapped not only when converting fromand to entities, but also an query argument, returned data and on other places.
13+
14+
Using the `MappingElasticsearchConverter` now covers all these cases.
1215

1316

1417
[[elasticsearch.mapping.meta-model]]
@@ -20,7 +23,7 @@ This allows to register `Converter` instances for specific domain type mapping.
2023
[[elasticsearch.mapping.meta-model.annotations]]
2124
=== Mapping Annotation Overview
2225

23-
The `ElasticsearchEntityMapper` can use metadata to drive the mapping of objects to documents. The metadata is taken from the entities properties which can be annotated.
26+
The `MappingElasticsearchConverter` uses metadata to drive the mapping of objects to documents. The metadata is taken from the entity's properties which can be annotated.
2427

2528
The following annotations are available:
2629

@@ -206,25 +209,14 @@ public class Config extends AbstractElasticsearchConfiguration {
206209
return RestClients.create(ClientConfiguration.create("localhost:9200")).rest();
207210
}
208211
209-
@Bean
210-
@Override
211-
public EntityMapper entityMapper() {
212-
213-
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(
214-
elasticsearchMappingContext(), new DefaultConversionService());
215-
entityMapper.setConversions(elasticsearchCustomConversions()); <1>
216-
217-
return entityMapper;
218-
}
219-
220212
@Bean
221213
@Override
222214
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
223215
return new ElasticsearchCustomConversions(
224-
Arrays.asList(new AddressToMap(), new MapToAddress())); <2>
216+
Arrays.asList(new AddressToMap(), new MapToAddress())); <1>
225217
}
226218
227-
@WritingConverter <3>
219+
@WritingConverter <2>
228220
static class AddressToMap implements Converter<Address, Map<String, Object>> {
229221
230222
@Override
@@ -238,7 +230,7 @@ public class Config extends AbstractElasticsearchConfiguration {
238230
}
239231
}
240232
241-
@ReadingConverter <4>
233+
@ReadingConverter <3>
242234
static class MapToAddress implements Converter<Map<String, Object>, Address> {
243235
244236
@Override
@@ -258,8 +250,7 @@ public class Config extends AbstractElasticsearchConfiguration {
258250
"localidad" : { "lat" : 34.118347, "lon" : -118.3026284 }
259251
}
260252
----
261-
<1> Register `ElasticsearchCustomConversions` with the `EntityMapper`.
262-
<2> Add `Converter` implementations.
263-
<3> Set up the `Converter` used for writing `DomainType` to Elasticsearch.
264-
<4> Set up the `Converter` used for reading `DomainType` from search result.
253+
<1> Add `Converter` implementations.
254+
<2> Set up the `Converter` used for writing `DomainType` to Elasticsearch.
255+
<3> Set up the `Converter` used for reading `DomainType` from search result.
265256
====

src/main/asciidoc/reference/elasticsearch-operations.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class TransportClientConfig extends ElasticsearchConfigurationSupport {
4141
4242
@Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
4343
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException { <2>
44-
return new ElasticsearchTemplate(elasticsearchClient(), entityMapper());
44+
return new ElasticsearchTemplate(elasticsearchClient());
4545
}
4646
}
4747
----

src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public class ElasticsearchConfigurationSupport {
4848
@Bean
4949
public ElasticsearchConverter elasticsearchEntityMapper(
5050
SimpleElasticsearchMappingContext elasticsearchMappingContext) {
51-
return new MappingElasticsearchConverter(elasticsearchMappingContext);
51+
MappingElasticsearchConverter elasticsearchConverter = new MappingElasticsearchConverter(
52+
elasticsearchMappingContext);
53+
elasticsearchConverter.setConversions(elasticsearchCustomConversions());
54+
return elasticsearchConverter;
5255
}
5356

5457
/**

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,16 @@ private Long getEntityVersion(Object entity) {
326326
}
327327

328328
private <T> IndexQuery getIndexQuery(T entity) {
329-
return new IndexQueryBuilder().withObject(entity).withId(getEntityId(entity)).withVersion(getEntityVersion(entity))
329+
String id = getEntityId(entity);
330+
331+
if (id != null) {
332+
id = elasticsearchConverter.convertId(id);
333+
}
334+
335+
return new IndexQueryBuilder() //
336+
.withId(id) //
337+
.withVersion(getEntityVersion(entity)) //
338+
.withObject(entity) //
330339
.build();
331340
}
332341

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, Index
173173

174174
@Override
175175
public String delete(String id, IndexCoordinates index) {
176-
DeleteRequest request = new DeleteRequest(index.getIndexName(), id);
176+
DeleteRequest request = new DeleteRequest(index.getIndexName(), elasticsearchConverter.convertId(id));
177177
try {
178178
return client.delete(request, RequestOptions.DEFAULT).getId();
179179
} catch (IOException e) {

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, Index
165165

166166
@Override
167167
public String delete(String id, IndexCoordinates index) {
168-
return client.prepareDelete(index.getIndexName(), IndexCoordinates.TYPE, id).execute().actionGet().getId();
168+
return client.prepareDelete(index.getIndexName(), IndexCoordinates.TYPE, elasticsearchConverter.convertId(id))
169+
.execute().actionGet().getId();
169170
}
170171

171172
@Override

src/test/java/org/springframework/data/elasticsearch/core/RequestFactoryTest.java renamed to src/test/java/org/springframework/data/elasticsearch/core/RequestFactoryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/**
4141
* @author Peter-Josef Meisch
4242
*/
43-
class RequestFactoryTest {
43+
class RequestFactoryTests {
4444

4545
@Nullable private static RequestFactory requestFactory;
4646
@Nullable private static MappingElasticsearchConverter converter;
@@ -74,7 +74,7 @@ void shouldBuildSearchWithGeoSortSort() throws JSONException {
7474
" \"query_string\": {" + //
7575
" \"query\": \"Smith\"," + //
7676
" \"fields\": [" + //
77-
" \"first-name^1.0\"" + //
77+
" \"last-name^1.0\"" + //
7878
" ]" + //
7979
" }" + //
8080
" }" + //

src/test/java/org/springframework/data/elasticsearch/core/index/MappingContextBaseTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private ElasticsearchConverter setupElasticsearchConverter() {
3434

3535
private SimpleElasticsearchMappingContext setupMappingContext() {
3636

37-
SimpleElasticsearchMappingContext mappingContext = new ElasticsearchConfigurationSupport() {}
37+
SimpleElasticsearchMappingContext mappingContext = new ElasticsearchConfigurationSupport()
3838
.elasticsearchMappingContext();
3939
mappingContext.initialize();
4040
return mappingContext;

0 commit comments

Comments
 (0)