# How to create a GeoShape intersect query with elastic java client v8.6.0

**URL:** <https://discuss.elastic.co/t/how-to-create-a-geoshape-intersect-query-with-elastic-java-client-v8-6-0/324025>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 26, 2023, 1:00pm UTC](https://discuss.elastic.co/t/how-to-create-a-geoshape-intersect-query-with-elastic-java-client-v8-6-0/324025 "2023-01-26T13:00:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusufozcan](https://avatars.discourse-cdn.com/v4/letter/y/c0e974/32.png) [@yusufozcan](https://discuss.elastic.co/u/yusufozcan)\
**Post date:** [January 26, 2023, 1:00pm UTC](https://discuss.elastic.co/t/how-to-create-a-geoshape-intersect-query-with-elastic-java-client-v8-6-0/324025/1 "2023-01-26T13:00:57Z")

</div>

Hi!

We have a geo shape field which we store multi polygons and we want to filter it with a coordinate.  
Desired search request is:

```auto
"geo_shape": {
  "area": {
    "shape": {
      "type": "point",
      "coordinates": [
        29.0,
        40.0
      ]
    },
    "relation": "intersects"
  }
}

```

But with elastic java client v8.6.0 I couldn't find a way to build this query. What I've done so far is:

```auto
new GeoShapeQuery.Builder()
            .shape(new GeoShapeFieldQuery.Builder()
                .shape(JsonData.of(new GeoLocation.Builder()
                    .coords(List.of(
                        request.getLatitude(),
                        request.getLongitude())).build()))
                .build())
            .field(AREA).build()._toQuery()

```

And with this I got the following:

```auto
"geo_shape": {
    "area": {
      "shape": [
        29.147841,
        40.91953 
      ]
    }
  }

```

It doesn't work because Elasticsearch expects `type` and `coordinates` fields.  
What is the correct way to achieve this?

Thanks!

---

<div class="post-metadata">

**Author:** ![yusufozcan](https://avatars.discourse-cdn.com/v4/letter/y/c0e974/32.png) [@yusufozcan](https://discuss.elastic.co/u/yusufozcan)\
**Post date:** [January 26, 2023, 5:53pm UTC](https://discuss.elastic.co/t/how-to-create-a-geoshape-intersect-query-with-elastic-java-client-v8-6-0/324025/2 "2023-01-26T17:53:30Z")

</div>

I ended up with a custom shape class that implements `JsonpSerializable` interface.  
For those who are looking for the same thing here is the [GeoPoint](https://gist.github.com/ykozcan/fda2818f1ac88fbf0b26afaced1a8b21) class.

We can use it with GeoShapeQuery like

```auto
new GeoShapeQuery.Builder()
            .shape(new GeoShapeFieldQuery.Builder()
                .relation(GeoShapeRelation.Intersects)
                .shape(JsonData.of(new GeoPoint(longitude, latitude)))
                .build())
            .field(AREA).build()._toQuery()

```

And the actual query is generated as follows

```auto
"geo_shape": {
        "area": {
            "shape": {
                "type": "point",
                "coordinates": [
                    29.0,
                    40.0
                ]
            },
            "relation": "intersects"
        }
    }

```

---

<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:** [February 23, 2023, 5:53pm UTC](https://discuss.elastic.co/t/how-to-create-a-geoshape-intersect-query-with-elastic-java-client-v8-6-0/324025/3 "2023-02-23T17:53:57Z")

</div>

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