GeoShape query with Java High Level Rest Client fails

I have the following mapping in AWS Elasticsearch index

"Location": {
   "type": "geo_shape",
   "tree": "quadtree"

The following geo_shape query works perfectly fine on my Kibana console

GET test/doc/_search
  "query": {
    "geo_shape": {
      "Location": {
        "shape": {
          "type": "point",
          "coordinates": [-122.28,37.83]
              "relation": "intersects"

I am trying to perform the similar query using Elasticsearch Java High Level Rest Client 6.6.1

//Create new CountRequest for index test
CountRequest countRequest = new CountRequest("test");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

QueryBuilder qb = QueryBuilders.matchAllQuery();
Coordinate coordinate = new Coordinate(-122.28, 37.83);
List<Coordinate> coordinateList = new ArrayList<>();

// ElasticShapeBuilder is my class which extends the the abstract class
ShapeBuilder shapeBuilder = new ElasticShapeBuilder(coordinateList);

QueryBuilder qb2 = QueryBuilders.geoShapeQuery("Location", shapeBuilder);
QueryBuilder qb3 = QueryBuilders.boolQuery().must(qb).filter(qb2);
CountResponse countResponse = highRestClient.count(countRequest, RequestOptions.DEFAULT);

I get the following error

com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value 
Suppressed: java.lang.IllegalStateException: Failed to close the XContentBuilder 
Caused by: Unclosed object or array found

The query that you are building from your java code is not the same as what you are running through the kibana console. In the console you don't have a bool query , and a count request.

Also, can you share the content of your ElasticShapeBuilder , especially the toXContent method?


Hey Luca,

Thanks for the response. As you suspected toXContent method in my ElasticShapeBuilder was the culprit.

After some googling around, I found out class and it seems to perfectly fit my use case. So I removed my ElasticShapeBuilder class and replaced it with PointBuilders class.

You can close this ticket now.


This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.