Skip to content

Commit 40cff58

Browse files
committed
Allow CustomConversions for entities - adaption for 4.0.x.
1 parent eefd5a2 commit 40cff58

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/test/java/org/springframework/data/elasticsearch/core/mapping/EntityCustomConversionIntegrationTests.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.LinkedHashMap;
2727
import java.util.Map;
2828

29+
import org.elasticsearch.common.geo.GeoPoint;
2930
import org.junit.jupiter.api.AfterEach;
3031
import org.junit.jupiter.api.BeforeEach;
3132
import org.junit.jupiter.api.DisplayName;
@@ -40,7 +41,6 @@
4041
import org.springframework.data.elasticsearch.core.IndexOperations;
4142
import org.springframework.data.elasticsearch.core.SearchHits;
4243
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
43-
import org.springframework.data.elasticsearch.core.geo.GeoJsonPoint;
4444
import org.springframework.data.elasticsearch.core.query.Query;
4545
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration;
4646
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
@@ -72,7 +72,7 @@ public ElasticsearchCustomConversions elasticsearchCustomConversions() {
7272
void setUp() {
7373
IndexOperations indexOps = operations.indexOps(Entity.class);
7474
indexOps.create();
75-
indexOps.putMapping();
75+
indexOps.putMapping(indexOps.createMapping());
7676
}
7777

7878
@AfterEach
@@ -86,7 +86,7 @@ void shouldUseCustomConversionsOnEntity() {
8686

8787
Entity entity = Entity.builder() //
8888
.value("hello") //
89-
.location(GeoJsonPoint.of(8.0, 42.7)) //
89+
.location(new GeoPoint(42.7, 8.0)) //
9090
.build();
9191

9292
org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document
@@ -104,10 +104,11 @@ void shouldStoreAndLoadEntityFromElasticsearch() {
104104

105105
Entity entity = Entity.builder() //
106106
.value("hello") //
107-
.location(GeoJsonPoint.of(8.0, 42.7)) //
107+
.location(new GeoPoint(42.7, 8.0)) //
108108
.build();
109109

110110
Entity savedEntity = operations.save(entity);
111+
operations.indexOps(Entity.class).refresh();
111112

112113
SearchHits<Entity> searchHits = operations.search(Query.findAll(), Entity.class);
113114
assertThat(searchHits.getTotalHits()).isEqualTo(1);
@@ -122,7 +123,7 @@ void shouldStoreAndLoadEntityFromElasticsearch() {
122123
@Document(indexName = "entity-with-custom-conversions")
123124
static class Entity {
124125
private String value;
125-
private GeoJsonPoint location;
126+
private GeoPoint location;
126127
}
127128

128129
@WritingConverter
@@ -131,8 +132,8 @@ static class EntityToMapConverter implements Converter<Entity, Map<String, Objec
131132
public Map<String, Object> convert(Entity source) {
132133
LinkedHashMap<String, Object> target = new LinkedHashMap<>();
133134
target.put("the_value", source.getValue());
134-
target.put("the_lat", "" + source.getLocation().getY());
135-
target.put("the_lon", "" + source.getLocation().getX());
135+
target.put("the_lat", String.valueOf(source.getLocation().getLat()));
136+
target.put("the_lon", String.valueOf(source.getLocation().getLon()));
136137
return target;
137138
}
138139
}
@@ -144,10 +145,10 @@ static class MapToEntityConverter implements Converter<Map<String, Object>, Enti
144145
public Entity convert(Map<String, Object> source) {
145146
Entity entity = new Entity();
146147
entity.setValue((String) source.get("the_value"));
147-
entity.setLocation(GeoJsonPoint.of( //
148-
Double.parseDouble((String) (source.get("the_lon"))), //
149-
Double.parseDouble((String) (source.get("the_lat"))) //
150-
));
148+
entity.setLocation(new GeoPoint( //
149+
Double.parseDouble((String) (source.get("the_lat"))), //
150+
Double.parseDouble((String) (source.get("the_lon"))) //
151+
));
151152
return entity;
152153
}
153154
}

0 commit comments

Comments
 (0)