Skip to content

Commit 1e006fd

Browse files
committed
Update write converters to write Geo objects into Document.
Document is now a simple type that can be used as target type for complex objects. Writing GeoConverters now serialize objects as Document instead of Map. Since Jackson considers Map-like types as JSON objects, we can safely use Document. Resolves #1675.
1 parent 02eb279 commit 1e006fd

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/convert/GeoConverters.java

+30-29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.core.convert.converter.Converter;
2727
import org.springframework.data.convert.ReadingConverter;
2828
import org.springframework.data.convert.WritingConverter;
29+
import org.springframework.data.elasticsearch.core.document.Document;
2930
import org.springframework.data.elasticsearch.core.geo.GeoJson;
3031
import org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection;
3132
import org.springframework.data.elasticsearch.core.geo.GeoJsonLineString;
@@ -67,14 +68,14 @@ public class GeoConverters {
6768
* {@link Converter} to write a {@link Point} to {@link Map} using {@code lat/long} properties.
6869
*/
6970
@WritingConverter
70-
public enum PointToMapConverter implements Converter<Point, Map<String, Object>> {
71+
public enum PointToMapConverter implements Converter<Point, Document> {
7172

7273
INSTANCE;
7374

7475
@Override
75-
public Map<String, Object> convert(Point source) {
76+
public Document convert(Point source) {
7677

77-
Map<String, Object> target = new LinkedHashMap<>();
78+
Document target = Document.create();
7879
target.put("lat", source.getY());
7980
target.put("lon", source.getX());
8081
return target;
@@ -104,13 +105,13 @@ public Point convert(Map<String, Object> source) {
104105
* {@link Converter} to write a {@link GeoPoint} to {@link Map} using {@code lat/long} properties.
105106
*/
106107
@WritingConverter
107-
public enum GeoPointToMapConverter implements Converter<GeoPoint, Map<String, Object>> {
108+
public enum GeoPointToMapConverter implements Converter<GeoPoint, Document> {
108109

109110
INSTANCE;
110111

111112
@Override
112-
public Map<String, Object> convert(GeoPoint source) {
113-
Map<String, Object> target = new LinkedHashMap<>();
113+
public Document convert(GeoPoint source) {
114+
Document target = Document.create();
114115
target.put("lat", source.getLat());
115116
target.put("lon", source.getLon());
116117
return target;
@@ -135,12 +136,12 @@ public GeoPoint convert(Map<String, Object> source) {
135136

136137
// region GeoJson
137138
@WritingConverter
138-
public enum GeoJsonToMapConverter implements Converter<GeoJson<? extends Iterable<?>>, Map<String, Object>> {
139+
public enum GeoJsonToMapConverter implements Converter<GeoJson<? extends Iterable<?>>, Document> {
139140

140141
INSTANCE;
141142

142143
@Override
143-
public Map<String, Object> convert(GeoJson<? extends Iterable<?>> source) {
144+
public Document convert(GeoJson<? extends Iterable<?>> source) {
144145
if (source instanceof GeoJsonPoint) {
145146
return GeoJsonPointToMapConverter.INSTANCE.convert((GeoJsonPoint) source);
146147
} else if (source instanceof GeoJsonMultiPoint) {
@@ -195,13 +196,13 @@ public GeoJson<? extends Iterable<?>> convert(Map<String, Object> source) {
195196

196197
// region GeoJsonPoint
197198
@WritingConverter
198-
public enum GeoJsonPointToMapConverter implements Converter<GeoJsonPoint, Map<String, Object>> {
199+
public enum GeoJsonPointToMapConverter implements Converter<GeoJsonPoint, Document> {
199200

200201
INSTANCE;
201202

202203
@Override
203-
public Map<String, Object> convert(GeoJsonPoint geoJsonPoint) {
204-
Map<String, Object> map = new LinkedHashMap<>();
204+
public Document convert(GeoJsonPoint geoJsonPoint) {
205+
Document map = Document.create();
205206
map.put("type", geoJsonPoint.getType());
206207
map.put("coordinates", geoJsonPoint.getCoordinates());
207208
return map;
@@ -233,13 +234,13 @@ public GeoJsonPoint convert(Map<String, Object> source) {
233234

234235
// region GeoJsonMultiPoint
235236
@WritingConverter
236-
public enum GeoJsonMultiPointToMapConverter implements Converter<GeoJsonMultiPoint, Map<String, Object>> {
237+
public enum GeoJsonMultiPointToMapConverter implements Converter<GeoJsonMultiPoint, Document> {
237238

238239
INSTANCE;
239240

240241
@Override
241-
public Map<String, Object> convert(GeoJsonMultiPoint geoJsonMultiPoint) {
242-
Map<String, Object> map = new LinkedHashMap<>();
242+
public Document convert(GeoJsonMultiPoint geoJsonMultiPoint) {
243+
Document map = Document.create();
243244
map.put("type", geoJsonMultiPoint.getType());
244245
map.put("coordinates", pointsToCoordinates(geoJsonMultiPoint.getCoordinates()));
245246
return map;
@@ -268,13 +269,13 @@ public GeoJsonMultiPoint convert(Map<String, Object> source) {
268269

269270
// region GeoJsonLineString
270271
@WritingConverter
271-
public enum GeoJsonLineStringToMapConverter implements Converter<GeoJsonLineString, Map<String, Object>> {
272+
public enum GeoJsonLineStringToMapConverter implements Converter<GeoJsonLineString, Document> {
272273

273274
INSTANCE;
274275

275276
@Override
276-
public Map<String, Object> convert(GeoJsonLineString geoJsonLineString) {
277-
Map<String, Object> map = new LinkedHashMap<>();
277+
public Document convert(GeoJsonLineString geoJsonLineString) {
278+
Document map = Document.create();
278279
map.put("type", geoJsonLineString.getType());
279280
map.put("coordinates", pointsToCoordinates(geoJsonLineString.getCoordinates()));
280281
return map;
@@ -303,12 +304,12 @@ public GeoJsonLineString convert(Map<String, Object> source) {
303304

304305
// region GeoJsonMultiLineString
305306
@WritingConverter
306-
public enum GeoJsonMultiLineStringToMapConverter implements Converter<GeoJsonMultiLineString, Map<String, Object>> {
307+
public enum GeoJsonMultiLineStringToMapConverter implements Converter<GeoJsonMultiLineString, Document> {
307308

308309
INSTANCE;
309310

310311
@Override
311-
public Map<String, Object> convert(GeoJsonMultiLineString source) {
312+
public Document convert(GeoJsonMultiLineString source) {
312313
return geoJsonLinesStringsToMap(source.getType(), source.getCoordinates());
313314
}
314315
}
@@ -331,12 +332,12 @@ public GeoJsonMultiLineString convert(Map<String, Object> source) {
331332

332333
// region GeoJsonPolygon
333334
@WritingConverter
334-
public enum GeoJsonPolygonToMapConverter implements Converter<GeoJsonPolygon, Map<String, Object>> {
335+
public enum GeoJsonPolygonToMapConverter implements Converter<GeoJsonPolygon, Document> {
335336

336337
INSTANCE;
337338

338339
@Override
339-
public Map<String, Object> convert(GeoJsonPolygon source) {
340+
public Document convert(GeoJsonPolygon source) {
340341
return geoJsonLinesStringsToMap(source.getType(), source.getCoordinates());
341342
}
342343
}
@@ -369,9 +370,9 @@ public enum GeoJsonMultiPolygonToMapConverter implements Converter<GeoJsonMultiP
369370
INSTANCE;
370371

371372
@Override
372-
public Map<String, Object> convert(GeoJsonMultiPolygon source) {
373+
public Document convert(GeoJsonMultiPolygon source) {
373374

374-
Map<String, Object> map = new LinkedHashMap<>();
375+
Document map = Document.create();
375376
map.put("type", source.getType());
376377

377378
List<Object> coordinates = source.getCoordinates().stream() //
@@ -400,7 +401,7 @@ public GeoJsonMultiPolygon convert(Map<String, Object> source) {
400401
Assert.isTrue(coordinates instanceof List, "coordinates must be a List");
401402

402403
List<GeoJsonPolygon> geoJsonPolygons = ((List<?>) coordinates).stream().map(it -> {
403-
Map<String, Object> map = new LinkedHashMap<>();
404+
Document map = Document.create();
404405
map.put("type", GeoJsonPolygon.TYPE);
405406
map.put("coordinates", it);
406407
return map;
@@ -414,14 +415,14 @@ public GeoJsonMultiPolygon convert(Map<String, Object> source) {
414415
// region GeoJsonGeometryCollection
415416
@WritingConverter
416417
public enum GeoJsonGeometryCollectionToMapConverter
417-
implements Converter<GeoJsonGeometryCollection, Map<String, Object>> {
418+
implements Converter<GeoJsonGeometryCollection, Document> {
418419

419420
INSTANCE;
420421

421422
@Override
422-
public Map<String, Object> convert(GeoJsonGeometryCollection source) {
423+
public Document convert(GeoJsonGeometryCollection source) {
423424

424-
Map<String, Object> map = new LinkedHashMap<>();
425+
Document map = Document.create();
425426
map.put("type", source.getType());
426427
List<Map<String, Object>> geometries = source.getGeometries().stream()
427428
.map(GeoJsonToMapConverter.INSTANCE::convert).collect(Collectors.toList());
@@ -484,8 +485,8 @@ private static List<Point> coordinatesToPoints(List<List<Number>> pointList) {
484485
}).collect(Collectors.toList());
485486
}
486487

487-
private static Map<String, Object> geoJsonLinesStringsToMap(String type, List<GeoJsonLineString> lineStrings) {
488-
Map<String, Object> map = new LinkedHashMap<>();
488+
private static Document geoJsonLinesStringsToMap(String type, List<GeoJsonLineString> lineStrings) {
489+
Document map = Document.create();
489490
map.put("type", type);
490491
List<List<List<Double>>> coordinates = lineStrings.stream()
491492
.map(it -> GeoConverters.pointsToCoordinates(it.getCoordinates())).collect(Collectors.toList());

Diff for: src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchSimpleTypes.java

+5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
import java.util.HashSet;
2020
import java.util.Set;
2121

22+
import org.springframework.data.elasticsearch.core.document.Document;
2223
import org.springframework.data.mapping.model.SimpleTypeHolder;
2324

2425
/**
26+
* Utility to define simple types understood by Spring Data Elasticsearch and the Elasticsearch client.
27+
*
2528
* @author Christoph Strobl
29+
* @author Mark Paluch
2630
* @since 3.2
2731
*/
2832
public class ElasticsearchSimpleTypes {
@@ -35,6 +39,7 @@ public class ElasticsearchSimpleTypes {
3539
AUTOGENERATED_ID_TYPES = Collections.unmodifiableSet(classes);
3640

3741
Set<Class<?>> simpleTypes = new HashSet<>();
42+
simpleTypes.add(Document.class);
3843
ELASTICSEARCH_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
3944
}
4045

0 commit comments

Comments
 (0)