Elasticsearch 8.x java migrating from QueryBuilders.geoShapeQuery to geoShapeQuery class

757 views Asked by At

I am trying to move from Spring data elasticsearch 4.x to 5.x & spring-boot-starter-data-elasticsearch 3.0.0 in order to remove rhlc from my code.

One of my query is a geoShapeQuery

Here my old code :

GeoShapeQueryBuilder geoShape = QueryBuilders.geoShapeQuery(ConvertUtils.FULL_GEO,new Point(lon, lat));
geoShape.relation(ShapeRelation.CONTAINS);
QueryBuilder bool = new BoolQueryBuilder().filter(geoShape).should(rankFeature);

I am trying to use the new class of geoShapeQuery without success

JsonData shape = JsonData.of("{\"type\": \"point\",\"coordinates\": [ 13.0, 53.0 ]}");
Query geoShape = GeoShapeQuery.of(f->f.field(ConvertUtils.FULL_GEO)
                .shape(s->s.relation(GeoShapeRelation.Contains).shape(shape)))._toQuery();
Query bool = BoolQuery.of(b->b
                .filter(geoShape)
                .should(rankFeature)
                )._toQuery();

When I am debugging I am seeing that the geopshape object put the shape as a String instead of a JSON object.

Query: {"geo_shape":{"fullGeo":{"shape":"{"type": "point","coordinates": [ 13.0, 53.0 ]}","relation":"contains"}}}

I am expecting to have this (without the double quote):

Query: {"geo_shape":{"fullGeo":{"shape":{"type": "point","coordinates": [ 13.0, 53.0 ]},"relation":"contains"}}}

I don't know what I am doing wrong.

3

There are 3 answers

0
Shalom Ohayon On BEST ANSWER

I am posting my solution if someone have the same issue...

My solution is to create a JSONObject via ObjectMapper (jackson) and then use JsonData

ObjectMapper om = new ObjectMapper();       
var node = om.readTree("{\"type\": \"point\",\"coordinates\":["+ lon +","+ lat + "]}");
JsonData shape = JsonData.of(node);
Query geoShape = GeoShapeQuery.of(f->f.field(ConvertUtils.FULL_GEO)
   .shape(s->s.relation(GeoShapeRelation.Contains).shape(shape)))._toQuery();
0
P.J.Meisch On

This is nothing from Spring Data Elasticsearch , it might be an issue in the Elasticsearch client. You might want to create an issue with Elasticsearch (https://github.com/elastic/elasticsearch-java/issues)

0
NoKnow On

in addition to the accepted answer, you can simply send the shape as WKT formatted String (and avoid custom converters)

JsonData shape = JsonData.of(WellKnownText.toWKT(new Point(longitude,latitude))); // will create a simple ValueNode, no JSON conversion
Query geoShape = QueryBuilders.geoShape().field("coordinates")
  .shape(s->s.relation(GeoShapeRelation.Contains).shape(shape)).build()._toQuery();