# GeoShape query with Java High Level Rest Client fails

**URL:** <https://discuss.elastic.co/t/geoshape-query-with-java-high-level-rest-client-fails/183396>\
**Category:** Elasticsearch\
**Created:** [May 29, 2019, 5:30pm UTC](https://discuss.elastic.co/t/geoshape-query-with-java-high-level-rest-client-fails/183396 "2019-05-29T17:30:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ganesh\_Kudva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ganesh_kudva/32/41942_2.png) [@Ganesh\_Kudva](https://discuss.elastic.co/u/Ganesh_Kudva)\
**Post date:** [May 29, 2019, 5:30pm UTC](https://discuss.elastic.co/t/geoshape-query-with-java-high-level-rest-client-fails/183396/1 "2019-05-29T17:30:29Z")

</div>

I have the following mapping in AWS Elasticsearch index

```auto
"Location": {
   "type": "geo_shape",
   "tree": "quadtree"
}

```

The following **geo\_shape** query works perfectly fine on my Kibana console

```auto
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**

```auto
//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<>();
coordinateList.add(coordinate);

// ElasticShapeBuilder is my class which extends the the abstract class org.elasticsearch.common.geo.builders.ShapeBuilder
ShapeBuilder shapeBuilder = new ElasticShapeBuilder(coordinateList);

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

```

I get the following error

```auto
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: java.io.IOException: Unclosed object or array found

```

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [May 31, 2019, 9:08am UTC](https://discuss.elastic.co/t/geoshape-query-with-java-high-level-rest-client-fails/183396/2 "2019-05-31T09:08:16Z")

</div>

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?

Thanks

---

<div class="post-metadata">

**Author:** ![Ganesh\_Kudva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ganesh_kudva/32/41942_2.png) [@Ganesh\_Kudva](https://discuss.elastic.co/u/Ganesh_Kudva)\
**Post date:** [June 3, 2019, 3:57pm UTC](https://discuss.elastic.co/t/geoshape-query-with-java-high-level-rest-client-fails/183396/3 "2019-06-03T15:57:14Z")

</div>

Hey Luca,

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

After some googling around, I found out org.elasticsearch.common.geo.builders.PointBuilders 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.

Thanks,  
Ganesh

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 1, 2019, 3:58pm UTC](https://discuss.elastic.co/t/geoshape-query-with-java-high-level-rest-client-fails/183396/4 "2019-07-01T15:58:59Z")

</div>

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