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

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:

"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:

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:

"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!

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 class.

We can use it with GeoShapeQuery like

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

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

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