Home > Back-end >  Elasticsearch 8.x java migrating from QueryBuilders.geoShapeQuery to geoShapeQuery class
Elasticsearch 8.x java migrating from QueryBuilders.geoShapeQuery to geoShapeQuery class

Time:12-08

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.

CodePudding user response:

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)

CodePudding user response:

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();
  • Related