GeoDistanceQuery Not Working

I am trying to create a Geo Distance filter using Spring Data Elasticsearch like the one below which is a working example created by JSON.

 "filter":{
        "geo_distance":{
           "distance":"32.42742km",
           "location.coordinates":{
              "lat":52.52000659999999,
              "lon":13.404954
           }
        }
     }

Below is the piece of code that generates a similar query but not quite the same.

    String distance = Double.toString(radiusKm);
    GeoPoint geoPoint = new GeoPoint(esLocation.getLatitude(), esLocation.getLongitude());
    GeoDistanceQueryBuilder filterQuery = QueryBuilders
             .geoDistanceQuery("location.coordinates")
             .distance(distance + "km")
             .point(geoPoint);

    finalQuery.filter(filterQuery);

I tried to use the 2 alternatives below but did not make any difference;

.distance(distance, DistanceUnit.KILOMETERS) //same
.point(esLocation.getLatitude(), esLocation.getLongitude()); // same

Below is what I get but it does not return the correct response. "filter" and "location.coordinates" are represented as arrays therefore no "lat" and "lon".

"km" is missing in the distance and it is not a String value.

     "filter":[
    {
       "geo_distance":{
          "location.coordinates":[
             13.404954,
             52.52000659999999
          ],
          "distance":32.42742,
          "distance_type":"arc",
          "validation_method":"STRICT",
          "ignore_unmapped":false,
          "boost":1.0
       }
    }
 ]

related part in the mappings:

"location":{
   "properties":{
      "coordinates":{
         "type":"geo_point"
      }
   }
}

related part in the document;

  "hits":[
         {
            "_source":{
                ....   
               "location":{
                 
                  "coordinates":{
                     "lat":59.329323,
                     "lon":18.06858
                  }
               }
         }
    ]

Any help would be great. Thanks!

I have never used that client, but Elasticsearch accepts array type coordinates: Format in [lon, lat].

Therefore the created query looks ok.

As the default distance unit is meters, how about trying .distance(distance*100)?

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