# Clarification on geo distance sorting versus geopoint field distance method

**URL:** <https://discuss.elastic.co/t/clarification-on-geo-distance-sorting-versus-geopoint-field-distance-method/8759>\
**Category:** Elasticsearch\
**Created:** [August 16, 2012, 2:50pm UTC](https://discuss.elastic.co/t/clarification-on-geo-distance-sorting-versus-geopoint-field-distance-method/8759 "2012-08-16T14:50:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raul\_Jr\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_jr_martinez/32/1286_2.png) [@Raul\_Jr\_Martinez](https://discuss.elastic.co/u/Raul_Jr_Martinez)\
**Post date:** [August 16, 2012, 2:50pm UTC](https://discuss.elastic.co/t/clarification-on-geo-distance-sorting-versus-geopoint-field-distance-method/8759/1 "2012-08-16T14:50:44Z")

</div>

Hello,

I'm interested on how others have implemented the use-case that I'd like to  
support.

USE CASE:

- For each document, a field called "geoloc" is available (most but not  
all) which is a geopoint field.
- On the front-end, each document, when geoloc, is available must  
display the "distance" based on the user's geographical location (lat/lon)
- Allow the user to sort by "nearest distance"

ISSUE: Based on my implementation (below), the geopoint distance() method  
is returning a different value compared with what's being computed by the  
sort handler. It's a discrepancy that I don't seem to understand.

ES version: 0.17.6

_Sample Result (**showing the discrepancy between sort and geodistance  
computed fields**):_

```
  {
    "_shard": 4,
    "_node": "yDPteNQkT2e-JfWkdNVEnQ",
    "_index": "asoc",
    "_type": "business",
    "_id": "4fcf818695f08d99090000d0",
    "_score": null,
    "fields": {
      "tags": null,
 "geoloc: { lat: 34.243161065225, lon: -111.31945564679"}
      "dtmPosted": "2012-06-06 16:12:54",
      "title": "Soaring Sky LLC",
      "username": "user123",
      "postedBy": null,
      "rating": 1,
      "geodistance": 32.4533052120666 ---> computed via 

```

script\_fields.  
},  
"sort": [  
27.45321291360537 --\> geo distance sort.  
],  
"\_explanation": {  
"value": 1,  
"description": "ConstantScore(_:_), product of:",  
"details": [  
{  
"value": 1,  
"description": "boost"  
},  
{  
"value": 1,  
"description": "queryNorm"  
}  
]  
}  
}

Following are the two important parameters being sent to ElasticSearch.

1. \*The Geo Distance Script fields parameters

```
 "script_fields": {
   "geodistance": {
     "params": {
       "lat": 42.3788774,
       "lon": -72.032366
     },
     "script": "doc['geoloc'].empty ? null : 

```

doc['geoloc'].distance(lat, lon)"  
}  
},

- 

1. 
  - 

The Sort specification parameters  
\*

```
 "sort": {
   "_geo_distance": {
     "field": "gd",
     "order": "asc",
     "unit": "mi",
     "default": "1",
     "geoloc": {
       "lat": 42.3788774,
       "lon": -72.032366
     }
   }

```

Please let me know what i'm missing.

Regards,  
raul

--

---

<div class="post-metadata">

**Author:** ![Thorsten\_2](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Thorsten\_2](https://discuss.elastic.co/u/Thorsten_2)\
**Post date:** [September 8, 2012, 9:36am UTC](https://discuss.elastic.co/t/clarification-on-geo-distance-sorting-versus-geopoint-field-distance-method/8759/2 "2012-09-08T09:36:41Z")

</div>

Did you find any solution to this problem? I'm experiencing the same  
problems...

Thanks

--

---

<div class="post-metadata">

**Author:** ![Thorsten\_2](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Thorsten\_2](https://discuss.elastic.co/u/Thorsten_2)\
**Post date:** [September 10, 2012, 2:01pm UTC](https://discuss.elastic.co/t/clarification-on-geo-distance-sorting-versus-geopoint-field-distance-method/8759/3 "2012-09-10T14:01:57Z")

</div>

Found the solution:

You need to use the _arc_DistanceInKm(lat,lon) method to get the same  
results as in the geo\_filter.

--

---

<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 6, 2017, 3:13am UTC](https://discuss.elastic.co/t/clarification-on-geo-distance-sorting-versus-geopoint-field-distance-method/8759/4 "2017-07-06T03:13:21Z")

</div>


