0

I'm trying to deserialize the response of elasticsearch using jackson. Everything works fine until I add geometry.

I'm using geolatte-geojson to handle geometry.

But for some reason the deserialization of elasticsearch response is not going well, probably 'cause in WKT format.

I've created a bean for geolatteModule for deserialization:

@Bean
public GeolatteGeomModule geomModule() {
    return new GeolatteGeomModule();
}

Here's how geometry response of elasticsearch looks like:

POLYGON((-95.26605606079102 29.724060758766743,-95.26631355285645 29.70900307937485,-95.23798942565917 29.702218931464575,-95.22185325622557 29.704306410402122,-95.2236557006836 29.72592417587012,-95.25712966918945 29.727638489043596,-95.26605606079102 29.724060758766743))

Here's how I'm trying to deserialize the response of elasticsearch to POJO:

Document document = objectMapper.convertValue(hit.getSourceAsMap(), Document.class);

Some additional points that might be helpful:
I'm also sending geometry object to DTO, when sending it as WKT I'm getting the same error. But when sending it in geojson format it works fine.

Error that I'm encountering:

java.lang.IllegalArgumentException: (was java.lang.NullPointerException) (through reference chain: com.kayrros.searchmanager.model.entity.Document["geometry"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4393)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:4324)
cicada_
  • 325
  • 2
  • 9
  • it doesn't seem like it's supported: https://github.com/spring-projects/spring-data-elasticsearch/issues/2045. A custom Jackson Deserializer could probably do the trick – Val Aug 03 '22 at 13:20
  • @Val Actually I'm using java high-level rest client with geolatte-geojson. I've used debugger and found that it needs to be in {type: POLYGON, coordinates: []} format. Is there anyway to get this from WKT? – cicada_ Aug 03 '22 at 14:16
  • There are probably a few libraries that do this: https://stackoverflow.com/questions/53957417/parse-geojson-file-with-java-topology-suite-or-geotools + https://javamana.com/2022/160/202206092314124785.html – Val Aug 03 '22 at 14:22
  • Thanks will try using geo-tools, was just wondering if there is there any way to get data from elasticsearch in geojson format rather than WKT – cicada_ Aug 03 '22 at 16:04
  • 1
    From ES, you get whatever you index into your source documents. If you index WKT, you get WKT, if you index GeoJson, you get GeoJson – Val Aug 03 '22 at 16:09
  • Hi @Val, Thank you ver much, that helped me. I mean all I had to do was to store as GeoJSON instead of WKT. I'm just learning dealing with geoJSON data thanks for helping me out. You can post your comment as an answer I'll accept that :) – cicada_ Aug 03 '22 at 16:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247003/discussion-between-cicada-and-val). – cicada_ Aug 03 '22 at 16:32

1 Answers1

0

If you want to get GeoJson back, you should just send GeoJson into your source documents instead of WKT, that way it'll be much easier than doing conversions from WKT to GeoJson on read.

Val
  • 207,596
  • 13
  • 358
  • 360