# GeoDistanceQuery Not Working

**URL:** <https://discuss.elastic.co/t/geodistancequery-not-working/306780>\
**Category:** Elasticsearch\
**Created:** [June 9, 2022, 10:04am UTC](https://discuss.elastic.co/t/geodistancequery-not-working/306780 "2022-06-09T10:04:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cugo](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@cugo](https://discuss.elastic.co/u/cugo)\
**Post date:** [June 9, 2022, 10:04am UTC](https://discuss.elastic.co/t/geodistancequery-not-working/306780/1 "2022-06-09T10:04:24Z")

</div>

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.

```auto
 "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.

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

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

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

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

```

related part in the document;

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

```

Any help would be great. Thanks!

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [June 9, 2022, 2:23pm UTC](https://discuss.elastic.co/t/geodistancequery-not-working/306780/2 "2022-06-09T14:23:05Z")

</div>

I have never used that client, but Elasticsearch accepts [array type coordinates](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html#_lat_lon_as_array_3): Format in `[lon, lat]`.

Therefore the created query looks ok.

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

---

<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 7, 2022, 3:35pm UTC](https://discuss.elastic.co/t/geodistancequery-not-working/306780/4 "2022-07-07T15:35:23Z")

</div>

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