# Search by distance and getting the actual distance

**URL:** <https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326>\
**Category:** Elasticsearch\
**Created:** [September 7, 2011, 3:58pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326 "2011-09-07T15:58:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_Nothhaft](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_nothhaft/32/3125_2.png) [@Matthias\_Nothhaft](https://discuss.elastic.co/u/Matthias_Nothhaft)\
**Post date:** [September 7, 2011, 3:58pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/1 "2011-09-07T15:58:47Z")

</div>

Hi,

I'm trying to use elasticsearch for geo location search. It works fine so  
using a filtered query and the geo\_distance filter. What I miss in the  
search result is the distance between "my location" and each of the results.  
Is it possible somehow to add the distance to the result? Can someone give  
me an example or point me to some docs how to make it?

regards,  
Matthias

---

<div class="post-metadata">

**Author:** ![Kaspars\_Sprogis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaspars_sprogis/32/1609_2.png) [@Kaspars\_Sprogis](https://discuss.elastic.co/u/Kaspars_Sprogis)\
**Post date:** [April 24, 2012, 5:51pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/2 "2012-04-24T17:51:59Z")

</div>

I am also interested in this. Can anyone help? Thanks

On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft wrote:

> Hi,
> 
> I'm trying to use elasticsearch for geo location search. It works fine so  
> using a filtered query and the geo\_distance filter. What I miss in the  
> search result is the distance between "my location" and each of the  
> results. Is it possible somehow to add the distance to the result? Can  
> someone give me an example or point me to some docs how to make it?
> 
> regards,  
> Matthias

---

<div class="post-metadata">

**Author:** ![Peter\_Schroder](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_schroder/32/2513_2.png) [@Peter\_Schroder](https://discuss.elastic.co/u/Peter_Schroder)\
**Post date:** [April 24, 2012, 6:50pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/3 "2012-04-24T18:50:07Z")

</div>

have a look at this  
posting [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)

Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:

> I am also interested in this. Can anyone help? Thanks
> 
> On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft wrote:
> 
> > Hi,
> > 
> > I'm trying to use elasticsearch for geo location search. It works fine so  
> > using a filtered query and the geo\_distance filter. What I miss in the  
> > search result is the distance between "my location" and each of the  
> > results. Is it possible somehow to add the distance to the result? Can  
> > someone give me an example or point me to some docs how to make it?
> > 
> > regards,  
> > Matthias

---

<div class="post-metadata">

**Author:** ![Kaspars\_Sprogis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaspars_sprogis/32/1609_2.png) [@Kaspars\_Sprogis](https://discuss.elastic.co/u/Kaspars_Sprogis)\
**Post date:** [April 24, 2012, 6:55pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/4 "2012-04-24T18:55:58Z")

</div>

I look at that posting already, and i am already sorting by \_geo\_distance  
but no actual distance value is returned (using latest 0.9.2)

Then only in once place somewhere on stackoverflow i found that we need to  
use script\_fields to get distance to the results and i am wondering why in  
many places it is mentioned that distance should be in results if you are  
sorting by \_geo\_distance if it is not?! Maybe i am doing something wrong,  
but i tested thousand times.

Only way i could get distance is like this:

"fields": [  
"\_source"  
],  
"script\_fields": {  
"distance": {  
"params": {  
"lat": 56.959942,  
"lon": 24.113656  
},  
"script": "doc['location'].arcDistanceInKm(lat, lon)"  
}  
}

On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:

> have a look at this posting  
> [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> 
> Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> 
> > I am also interested in this. Can anyone help? Thanks
> > 
> > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft wrote:
> > 
> > > Hi,
> > > 
> > > I'm trying to use elasticsearch for geo location search. It works fine  
> > > so using a filtered query and the geo\_distance filter. What I miss in the  
> > > search result is the distance between "my location" and each of the  
> > > results. Is it possible somehow to add the distance to the result? Can  
> > > someone give me an example or point me to some docs how to make it?
> > > 
> > > regards,  
> > > Matthias

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 24, 2012, 8:01pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/5 "2012-04-24T20:01:23Z")

</div>

Take a look at this example: [geo\_example.sh · GitHub](https://gist.github.com/2483149)

The value in the "sort" field is the distance:

```
  "sort" : [114.81801790248922]

```

On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:

> I look at that posting already, and i am already sorting by \_geo\_distance  
> but no actual distance value is returned (using latest 0.9.2)
> 
> Then only in once place somewhere on stackoverflow i found that we need to  
> use script\_fields to get distance to the results and i am wondering why in  
> many places it is mentioned that distance should be in results if you are  
> sorting by \_geo\_distance if it is not?! Maybe i am doing something wrong,  
> but i tested thousand times.
> 
> Only way i could get distance is like this:
> 
> "fields": [  
> "\_source"  
> ],  
> "script\_fields": {  
> "distance": {  
> "params": {  
> "lat": 56.959942,  
> "lon": 24.113656  
> },  
> "script": "doc['location'].arcDistanceInKm(lat, lon)"  
> }  
> }
> 
> On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> 
> > have a look at this posting  
> > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > 
> > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > 
> > > I am also interested in this. Can anyone help? Thanks
> > > 
> > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft  
> > > wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I'm trying to use elasticsearch for geo location search. It works fine  
> > > > so using a filtered query and the geo\_distance filter. What I miss in the  
> > > > search result is the distance between "my location" and each of the  
> > > > results. Is it possible somehow to add the distance to the result? Can  
> > > > someone give me an example or point me to some docs how to make it?
> > > > 
> > > > regards,  
> > > > Matthias

---

<div class="post-metadata">

**Author:** ![Kaspars\_Sprogis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaspars_sprogis/32/1609_2.png) [@Kaspars\_Sprogis](https://discuss.elastic.co/u/Kaspars_Sprogis)\
**Post date:** [April 24, 2012, 8:19pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/6 "2012-04-24T20:19:53Z")

</div>

Since in my test case all returned "sort" fields were almost identical  
(event if distance is different) i decided sort key is something different,  
some value of inner calculations.

So i think there is a bug somewhere with geo-search, so i reported whole  
test case to github issues

> <https://github.com/elastic/elasticsearch/issues/1885>
>
> Here is the complete data (index, mapping and data) for the tests:
> https://gist.…github.com/2483009
> 
> How is it possible that sorting by \*\*\_geo\_distance (arc) ASC\*\* results shows in following order:
> Maybe i am doing something wrong or is it a bug? Thanks.
> 
> \`\`\`
> 
> Pos Name Location Distance
> \---|----------|-------------------------------|-------------------
> 1 far 56.9440017845,24.117343883 1.7865229796151898
> 2 average 56.9624382022,24.1378033877 1.4899987260417218
> 3 closest 56.958344835,24.110908139 0.243505338088285
> 4 farthest 56.9428606076,24.077425299 2.9042299224497112
> \`\`\`

Because if you look at my test case: [Elasticsearch geo search debug · GitHub](https://gist.github.com/2483009)  
You will see that returned "sort" values are almost identical and also  
returned sort order is wrong...

Thanks, it is good to know "sort" actually should return distance, let's  
hope there is some explanation why problems in my test case appears.

On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:

> Take a look at this example: [geo\_example.sh · GitHub](https://gist.github.com/2483149)
> 
> The value in the "sort" field is the distance:
> 
> ```
> "sort" : [114.81801790248922]
> 
> ```
> 
> On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> 
> > I look at that posting already, and i am already sorting by \_geo\_distance  
> > but no actual distance value is returned (using latest 0.9.2)
> > 
> > Then only in once place somewhere on stackoverflow i found that we need  
> > to use script\_fields to get distance to the results and i am wondering why  
> > in many places it is mentioned that distance should be in results if you  
> > are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > wrong, but i tested thousand times.
> > 
> > Only way i could get distance is like this:
> > 
> > "fields": [  
> > "\_source"  
> > ],  
> > "script\_fields": {  
> > "distance": {  
> > "params": {  
> > "lat": 56.959942,  
> > "lon": 24.113656  
> > },  
> > "script": "doc['location'].arcDistanceInKm(lat, lon)"  
> > }  
> > }
> > 
> > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > 
> > > have a look at this posting  
> > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > 
> > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > > 
> > > > I am also interested in this. Can anyone help? Thanks
> > > > 
> > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft  
> > > > wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I'm trying to use elasticsearch for geo location search. It works fine  
> > > > > so using a filtered query and the geo\_distance filter. What I miss in the  
> > > > > search result is the distance between "my location" and each of the  
> > > > > results. Is it possible somehow to add the distance to the result? Can  
> > > > > someone give me an example or point me to some docs how to make it?
> > > > > 
> > > > > regards,  
> > > > > Matthias

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 24, 2012, 9:02pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/7 "2012-04-24T21:02:44Z")

</div>

Yeah, it's one of these gotchas that many people run into. When you specify  
location as an array, it follows GeoJson format [lon, lat], instead of lat,  
lon. So, if you will flip the numbers in \_geo\_distance sort location, it  
will work as expected:

"\_geo\_distance": {  
"location": [  
24.113656,  
56.959942  
],  
"order": "asc",  
"unit": "km"  
}

On Tuesday, April 24, 2012 4:19:53 PM UTC-4, Kaspars Sprogis wrote:

> Since in my test case all returned "sort" fields were almost identical  
> (event if distance is different) i decided sort key is something different,  
> some value of inner calculations.
> 
> So i think there is a bug somewhere with geo-search, so i reported whole  
> test case to github issues  
> [Sort by \_geo\_distance showing wrong order · Issue #1885 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1885)
> 
> Because if you look at my test case: [Elasticsearch geo search debug · GitHub](https://gist.github.com/2483009)  
> You will see that returned "sort" values are almost identical and also  
> returned sort order is wrong...
> 
> Thanks, it is good to know "sort" actually should return distance, let's  
> hope there is some explanation why problems in my test case appears.
> 
> On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:
> 
> > Take a look at this example: [geo\_example.sh · GitHub](https://gist.github.com/2483149)
> > 
> > The value in the "sort" field is the distance:
> > 
> > ```
> > "sort" : [114.81801790248922]
> > 
> > ```
> > 
> > On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> > 
> > > I look at that posting already, and i am already sorting by  
> > > \_geo\_distance but no actual distance value is returned (using latest 0.9.2)
> > > 
> > > Then only in once place somewhere on stackoverflow i found that we need  
> > > to use script\_fields to get distance to the results and i am wondering why  
> > > in many places it is mentioned that distance should be in results if you  
> > > are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > > wrong, but i tested thousand times.
> > > 
> > > Only way i could get distance is like this:
> > > 
> > > "fields": [  
> > > "\_source"  
> > > ],  
> > > "script\_fields": {  
> > > "distance": {  
> > > "params": {  
> > > "lat": 56.959942,  
> > > "lon": 24.113656  
> > > },  
> > > "script": "doc['location'].arcDistanceInKm(lat, lon)"  
> > > }  
> > > }
> > > 
> > > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > > 
> > > > have a look at this posting  
> > > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > > 
> > > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > > > 
> > > > > I am also interested in this. Can anyone help? Thanks
> > > > > 
> > > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft  
> > > > > wrote:
> > > > > 
> > > > > > Hi,
> > > > > > 
> > > > > > I'm trying to use elasticsearch for geo location search. It works  
> > > > > > fine so using a filtered query and the geo\_distance filter. What I miss in  
> > > > > > the search result is the distance between "my location" and each of the  
> > > > > > results. Is it possible somehow to add the distance to the result? Can  
> > > > > > someone give me an example or point me to some docs how to make it?
> > > > > > 
> > > > > > regards,  
> > > > > > Matthias

---

<div class="post-metadata">

**Author:** ![Kaspars\_Sprogis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaspars_sprogis/32/1609_2.png) [@Kaspars\_Sprogis](https://discuss.elastic.co/u/Kaspars_Sprogis)\
**Post date:** [April 24, 2012, 9:19pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/8 "2012-04-24T21:19:50Z")

</div>

Wow, thanks. I spent half a day running tests, googling and finally decided  
it could be a bug and even reported.  
There should be some warning or exclamation marks like _"Important notice"_ on  
docs where it mentions format changes 🙂 it too easy to mis-read lat, lon  
vs lon, lat.

Thanks a lot.

On Wednesday, April 25, 2012 12:02:44 AM UTC+3, Igor Motov wrote:

> Yeah, it's one of these gotchas that many people run into. When you  
> specify location as an array, it follows GeoJson format [lon, lat], instead  
> of lat, lon. So, if you will flip the numbers in \_geo\_distance sort  
> location, it will work as expected:
> 
> "\_geo\_distance": {  
> "location": [  
> 24.113656,  
> 56.959942  
> ],  
> "order": "asc",  
> "unit": "km"  
> }
> 
> On Tuesday, April 24, 2012 4:19:53 PM UTC-4, Kaspars Sprogis wrote:
> 
> > Since in my test case all returned "sort" fields were almost identical  
> > (event if distance is different) i decided sort key is something different,  
> > some value of inner calculations.
> > 
> > So i think there is a bug somewhere with geo-search, so i reported whole  
> > test case to github issues  
> > [Sort by \_geo\_distance showing wrong order · Issue #1885 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1885)
> > 
> > Because if you look at my test case: [Elasticsearch geo search debug · GitHub](https://gist.github.com/2483009)  
> > You will see that returned "sort" values are almost identical and also  
> > returned sort order is wrong...
> > 
> > Thanks, it is good to know "sort" actually should return distance, let's  
> > hope there is some explanation why problems in my test case appears.
> > 
> > On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:
> > 
> > > Take a look at this example: [geo\_example.sh · GitHub](https://gist.github.com/2483149)
> > > 
> > > The value in the "sort" field is the distance:
> > > 
> > > ```
> > > "sort" : [114.81801790248922]
> > > 
> > > ```
> > > 
> > > On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> > > 
> > > > I look at that posting already, and i am already sorting by  
> > > > \_geo\_distance but no actual distance value is returned (using latest 0.9.2)
> > > > 
> > > > Then only in once place somewhere on stackoverflow i found that we need  
> > > > to use script\_fields to get distance to the results and i am wondering why  
> > > > in many places it is mentioned that distance should be in results if you  
> > > > are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > > > wrong, but i tested thousand times.
> > > > 
> > > > Only way i could get distance is like this:
> > > > 
> > > > "fields": [  
> > > > "\_source"  
> > > > ],  
> > > > "script\_fields": {  
> > > > "distance": {  
> > > > "params": {  
> > > > "lat": 56.959942,  
> > > > "lon": 24.113656  
> > > > },  
> > > > "script": "doc['location'].arcDistanceInKm(lat, lon)"  
> > > > }  
> > > > }
> > > > 
> > > > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > > > 
> > > > > have a look at this posting  
> > > > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > > > 
> > > > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > > > > 
> > > > > > I am also interested in this. Can anyone help? Thanks
> > > > > > 
> > > > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft  
> > > > > > wrote:
> > > > > > 
> > > > > > > Hi,
> > > > > > > 
> > > > > > > I'm trying to use elasticsearch for geo location search. It works  
> > > > > > > fine so using a filtered query and the geo\_distance filter. What I miss in  
> > > > > > > the search result is the distance between "my location" and each of the  
> > > > > > > results. Is it possible somehow to add the distance to the result? Can  
> > > > > > > someone give me an example or point me to some docs how to make it?
> > > > > > > 
> > > > > > > regards,  
> > > > > > > Matthias

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 24, 2012, 9:34pm UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/9 "2012-04-24T21:34:35Z")

</div>

I totally agree with you. It's quite confusing. By the way, docs are in  
[https://github.com/elasticsearch/elasticsearch.github.com](https://github.com/elasticsearch/elasticsearch.github.com) Care to submit a  
pull request? 😉

On Tuesday, April 24, 2012 5:19:50 PM UTC-4, Kaspars Sprogis wrote:

> Wow, thanks. I spent half a day running tests, googling  
> and finally decided it could be a bug and even reported.  
> There should be some warning or exclamation marks like \*"Important notice"
> 
> - on docs where it mentions format changes 🙂 it too easy to mis-read  
> lat, lon vs lon, lat.
> 
> Thanks a lot.
> 
> On Wednesday, April 25, 2012 12:02:44 AM UTC+3, Igor Motov wrote:
> 
> > Yeah, it's one of these gotchas that many people run into. When you  
> > specify location as an array, it follows GeoJson format [lon, lat], instead  
> > of lat, lon. So, if you will flip the numbers in \_geo\_distance sort  
> > location, it will work as expected:
> > 
> > "\_geo\_distance": {  
> > "location": [  
> > 24.113656,  
> > 56.959942  
> > ],  
> > "order": "asc",  
> > "unit": "km"  
> > }
> > 
> > On Tuesday, April 24, 2012 4:19:53 PM UTC-4, Kaspars Sprogis wrote:
> > 
> > > Since in my test case all returned "sort" fields were almost identical  
> > > (event if distance is different) i decided sort key is something different,  
> > > some value of inner calculations.
> > > 
> > > So i think there is a bug somewhere with geo-search, so i reported whole  
> > > test case to github issues  
> > > [Sort by \_geo\_distance showing wrong order · Issue #1885 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1885)
> > > 
> > > Because if you look at my test case: [Elasticsearch geo search debug · GitHub](https://gist.github.com/2483009)  
> > > You will see that returned "sort" values are almost identical and also  
> > > returned sort order is wrong...
> > > 
> > > Thanks, it is good to know "sort" actually should return distance, let's  
> > > hope there is some explanation why problems in my test case appears.
> > > 
> > > On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:
> > > 
> > > > Take a look at this example: [geo\_example.sh · GitHub](https://gist.github.com/2483149)
> > > > 
> > > > The value in the "sort" field is the distance:
> > > > 
> > > > ```
> > > > "sort" : [114.81801790248922]
> > > > 
> > > > ```
> > > > 
> > > > On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> > > > 
> > > > > I look at that posting already, and i am already sorting by  
> > > > > \_geo\_distance but no actual distance value is returned (using latest 0.9.2)
> > > > > 
> > > > > Then only in once place somewhere on stackoverflow i found that we  
> > > > > need to use script\_fields to get distance to the results and i am wondering  
> > > > > why in many places it is mentioned that distance should be in results if  
> > > > > you are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > > > > wrong, but i tested thousand times.
> > > > > 
> > > > > Only way i could get distance is like this:
> > > > > 
> > > > > "fields": [  
> > > > > "\_source"  
> > > > > ],  
> > > > > "script\_fields": {  
> > > > > "distance": {  
> > > > > "params": {  
> > > > > "lat": 56.959942,  
> > > > > "lon": 24.113656  
> > > > > },  
> > > > > "script": "doc['location'].arcDistanceInKm(lat, lon)"  
> > > > > }  
> > > > > }
> > > > > 
> > > > > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > > > > 
> > > > > > have a look at this posting  
> > > > > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > > > > 
> > > > > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > > > > > 
> > > > > > > I am also interested in this. Can anyone help? Thanks
> > > > > > > 
> > > > > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft  
> > > > > > > wrote:
> > > > > > > 
> > > > > > > > Hi,
> > > > > > > > 
> > > > > > > > I'm trying to use elasticsearch for geo location search. It works  
> > > > > > > > fine so using a filtered query and the geo\_distance filter. What I miss in  
> > > > > > > > the search result is the distance between "my location" and each of the  
> > > > > > > > results. Is it possible somehow to add the distance to the result? Can  
> > > > > > > > someone give me an example or point me to some docs how to make it?
> > > > > > > > 
> > > > > > > > regards,  
> > > > > > > > Matthias

---

<div class="post-metadata">

**Author:** ![Kaspars\_Sprogis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaspars_sprogis/32/1609_2.png) [@Kaspars\_Sprogis](https://discuss.elastic.co/u/Kaspars_Sprogis)\
**Post date:** [April 25, 2012, 4:51am UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/10 "2012-04-25T04:51:51Z")

</div>

I'll explore how the docs are built and see what i can do. As i already  
have all the test data for geo search probably tutorial on geo search could  
be a help too. As i did lot of search yesterday, i noticed all the same  
questions all around about geo search. As kimchy added support for Latvian  
stemming after one day i wrote an issue (which was essential one for us  
choosing ES) and released this feature in a week in new version i think i  
owe this community some help too 🙂

On Wednesday, April 25, 2012 12:34:35 AM UTC+3, Igor Motov wrote:

> I totally agree with you. It's quite confusing. By the way, docs are in  
> [https://github.com/elasticsearch/elasticsearch.github.com](https://github.com/elasticsearch/elasticsearch.github.com) Care to submit  
> a pull request? 😉
> 
> On Tuesday, April 24, 2012 5:19:50 PM UTC-4, Kaspars Sprogis wrote:
> 
> > Wow, thanks. I spent half a day running tests, googling  
> > and finally decided it could be a bug and even reported.  
> > There should be some warning or exclamation marks like _"Important  
> > notice"_ on docs where it mentions format changes 🙂 it too easy to  
> > mis-read lat, lon vs lon, lat.
> > 
> > Thanks a lot.
> > 
> > On Wednesday, April 25, 2012 12:02:44 AM UTC+3, Igor Motov wrote:
> > 
> > > Yeah, it's one of these gotchas that many people run into. When you  
> > > specify location as an array, it follows GeoJson format [lon, lat], instead  
> > > of lat, lon. So, if you will flip the numbers in \_geo\_distance sort  
> > > location, it will work as expected:
> > > 
> > > "\_geo\_distance": {  
> > > "location": [  
> > > 24.113656,  
> > > 56.959942  
> > > ],  
> > > "order": "asc",  
> > > "unit": "km"  
> > > }
> > > 
> > > On Tuesday, April 24, 2012 4:19:53 PM UTC-4, Kaspars Sprogis wrote:
> > > 
> > > > Since in my test case all returned "sort" fields were almost identical  
> > > > (event if distance is different) i decided sort key is something different,  
> > > > some value of inner calculations.
> > > > 
> > > > So i think there is a bug somewhere with geo-search, so i reported  
> > > > whole test case to github issues  
> > > > [Sort by \_geo\_distance showing wrong order · Issue #1885 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1885)
> > > > 
> > > > Because if you look at my test case: [Elasticsearch geo search debug · GitHub](https://gist.github.com/2483009)  
> > > > You will see that returned "sort" values are almost identical and also  
> > > > returned sort order is wrong...
> > > > 
> > > > Thanks, it is good to know "sort" actually should return distance,  
> > > > let's hope there is some explanation why problems in my test case appears.
> > > > 
> > > > On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:
> > > > 
> > > > > Take a look at this example: [geo\_example.sh · GitHub](https://gist.github.com/2483149)
> > > > > 
> > > > > The value in the "sort" field is the distance:
> > > > > 
> > > > > ```
> > > > > "sort" : [114.81801790248922]
> > > > > 
> > > > > ```
> > > > > 
> > > > > On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> > > > > 
> > > > > > I look at that posting already, and i am already sorting by  
> > > > > > \_geo\_distance but no actual distance value is returned (using latest 0.9.2)
> > > > > > 
> > > > > > Then only in once place somewhere on stackoverflow i found that we  
> > > > > > need to use script\_fields to get distance to the results and i am wondering  
> > > > > > why in many places it is mentioned that distance should be in results if  
> > > > > > you are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > > > > > wrong, but i tested thousand times.
> > > > > > 
> > > > > > Only way i could get distance is like this:
> > > > > > 
> > > > > > "fields": [  
> > > > > > "\_source"  
> > > > > > ],  
> > > > > > "script\_fields": {  
> > > > > > "distance": {  
> > > > > > "params": {  
> > > > > > "lat": 56.959942,  
> > > > > > "lon": 24.113656  
> > > > > > },  
> > > > > > "script": "doc['location'].arcDistanceInKm(lat, lon)"  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > > > > > 
> > > > > > > have a look at this posting  
> > > > > > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > > > > > 
> > > > > > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > > > > > > 
> > > > > > > > I am also interested in this. Can anyone help? Thanks
> > > > > > > > 
> > > > > > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias Nothhaft  
> > > > > > > > wrote:
> > > > > > > > 
> > > > > > > > > Hi,
> > > > > > > > > 
> > > > > > > > > I'm trying to use elasticsearch for geo location search. It works  
> > > > > > > > > fine so using a filtered query and the geo\_distance filter. What I miss in  
> > > > > > > > > the search result is the distance between "my location" and each of the  
> > > > > > > > > results. Is it possible somehow to add the distance to the result? Can  
> > > > > > > > > someone give me an example or point me to some docs how to make it?
> > > > > > > > > 
> > > > > > > > > regards,  
> > > > > > > > > Matthias

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 27, 2012, 8:57am UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/11 "2012-04-27T08:57:03Z")

</div>

Heya, the formatting is specified here for example:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html). Its hard  
to find, I know..., and to be honest its confusing (the order) for me as  
well, it used to be the other way around, but we changed it to match the  
geojson spec.

On Wed, Apr 25, 2012 at 7:51 AM, Kaspars Sprogis [darklow@gmail.com](mailto:darklow@gmail.com) wrote:

> I'll explore how the docs are built and see what i can do. As i already  
> have all the test data for geo search probably tutorial on geo search could  
> be a help too. As i did lot of search yesterday, i noticed all the same  
> questions all around about geo search. As kimchy added support for Latvian  
> stemming after one day i wrote an issue (which was essential one for us  
> choosing ES) and released this feature in a week in new version i think i  
> owe this community some help too 🙂
> 
> On Wednesday, April 25, 2012 12:34:35 AM UTC+3, Igor Motov wrote:
> 
> > I totally agree with you. It's quite confusing. By the way, docs are in  
> > [https://github.com/\*\*elasticsearch/elasticsearch.\*\*github.com](https://github.com/ **elasticsearch/elasticsearch.** github.com)[https://github.com/elasticsearch/elasticsearch.github.com](https://github.com/elasticsearch/elasticsearch.github.com)Care to submit a pull request? 😉
> > 
> > On Tuesday, April 24, 2012 5:19:50 PM UTC-4, Kaspars Sprogis wrote:
> > 
> > > Wow, thanks. I spent half a day running tests, googling  
> > > and finally decided it could be a bug and even reported.  
> > > There should be some warning or exclamation marks like _"Important  
> > > notice"_ on docs where it mentions format changes 🙂 it too easy to  
> > > mis-read lat, lon vs lon, lat.
> > > 
> > > Thanks a lot.
> > > 
> > > On Wednesday, April 25, 2012 12:02:44 AM UTC+3, Igor Motov wrote:
> > > 
> > > > Yeah, it's one of these gotchas that many people run into. When you  
> > > > specify location as an array, it follows GeoJson format [lon, lat], instead  
> > > > of lat, lon. So, if you will flip the numbers in \_geo\_distance sort  
> > > > location, it will work as expected:
> > > > 
> > > > "\_geo\_distance": {  
> > > > "location": [  
> > > > 24.113656,  
> > > > 56.959942  
> > > > ],  
> > > > "order": "asc",  
> > > > "unit": "km"  
> > > > }
> > > > 
> > > > On Tuesday, April 24, 2012 4:19:53 PM UTC-4, Kaspars Sprogis wrote:
> > > > 
> > > > > Since in my test case all returned "sort" fields were almost identical  
> > > > > (event if distance is different) i decided sort key is something different,  
> > > > > some value of inner calculations.
> > > > > 
> > > > > So i think there is a bug somewhere with geo-search, so i reported  
> > > > > whole test case to github issues  
> > > > > [https://github.com/\*\*elasticsearch/elasticsearch/\*\*issues/1885](https://github.com/ **elasticsearch/elasticsearch/** issues/1885)[https://github.com/elasticsearch/elasticsearch/issues/1885](https://github.com/elasticsearch/elasticsearch/issues/1885)
> > > > > 
> > > > > Because if you look at my test case: [https://gist.github.com/](https://gist.github.com/)\*\*  
> > > > > 2483009 [https://gist.github.com/2483009](https://gist.github.com/2483009)  
> > > > > You will see that returned "sort" values are almost identical and also  
> > > > > returned sort order is wrong...
> > > > > 
> > > > > Thanks, it is good to know "sort" actually should return distance,  
> > > > > let's hope there is some explanation why problems in my test case appears.
> > > > > 
> > > > > On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:
> > > > > 
> > > > > > Take a look at this example: [https://gist.github](https://gist.github).\*\*com/2483149[https://gist.github.com/2483149](https://gist.github.com/2483149)
> > > > > > 
> > > > > > The value in the "sort" field is the distance:
> > > > > > 
> > > > > > ```
> > > > > > "sort" : [114.81801790248922]
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> > > > > > 
> > > > > > > I look at that posting already, and i am already sorting by  
> > > > > > > \_geo\_distance but no actual distance value is returned (using latest 0.9.2)
> > > > > > > 
> > > > > > > Then only in once place somewhere on stackoverflow i found that we  
> > > > > > > need to use script\_fields to get distance to the results and i am wondering  
> > > > > > > why in many places it is mentioned that distance should be in results if  
> > > > > > > you are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > > > > > > wrong, but i tested thousand times.
> > > > > > > 
> > > > > > > Only way i could get distance is like this:
> > > > > > > 
> > > > > > > "fields": [  
> > > > > > > "\_source"  
> > > > > > > ],  
> > > > > > > "script\_fields": {  
> > > > > > > "distance": {  
> > > > > > > "params": {  
> > > > > > > "lat": 56.959942,  
> > > > > > > "lon": 24.113656  
> > > > > > > },  
> > > > > > > "script": "doc['location'].\*\*arcDistanceInKm(lat, lon)"  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > > > > > > 
> > > > > > > > have a look at this posting [http://groups.google](http://groups.google).\*\*  
> > > > > > > > com/a/elasticsearch.com/group/ **users/browse\_thread/thread/**  
> > > > > > > > fab566d2a275456b/\*\*2424d39029721e30?lnk=gst&q=+\*\*distance&pli=1[http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > > > > > > 
> > > > > > > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > > > > > > > 
> > > > > > > > > I am also interested in this. Can anyone help? Thanks
> > > > > > > > > 
> > > > > > > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias  
> > > > > > > > > Nothhaft wrote:
> > > > > > > > > 
> > > > > > > > > > Hi,
> > > > > > > > > > 
> > > > > > > > > > I'm trying to use elasticsearch for geo location search. It works  
> > > > > > > > > > fine so using a filtered query and the geo\_distance filter. What I miss in  
> > > > > > > > > > the search result is the distance between "my location" and each of the  
> > > > > > > > > > results. Is it possible somehow to add the distance to the result? Can  
> > > > > > > > > > someone give me an example or point me to some docs how to make it?
> > > > > > > > > > 
> > > > > > > > > > regards,  
> > > > > > > > > > Matthias

---

<div class="post-metadata">

**Author:** ![Kaspars\_Sprogis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaspars_sprogis/32/1609_2.png) [@Kaspars\_Sprogis](https://discuss.elastic.co/u/Kaspars_Sprogis)\
**Post date:** [April 27, 2012, 9:03am UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/12 "2012-04-27T09:03:44Z")

</div>

The problem was not to found the correct guide page, but to notice that in  
one place order of arguments differs from usual one (lat,lon, which uses  
for storing, also google uses it) so it is very easy to mis-read it.  
Yesterday i set up ES docs on ruby, so that i can contribute some guide  
updates. Kimchy, do you agree that in place where lat, lon is way around  
there should be some visually more noticeable Note element, so that users  
really notice these changes? If you agree on that i can do that and  
contribute.

On Friday, April 27, 2012 11:57:03 AM UTC+3, kimchy wrote:

> Heya, the formatting is specified here for example:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html). Its  
> hard to find, I know..., and to be honest its confusing (the order) for me  
> as well, it used to be the other way around, but we changed it to match the  
> geojson spec.
> 
> On Wed, Apr 25, 2012 at 7:51 AM, Kaspars Sprogis wrote:
> 
> > I'll explore how the docs are built and see what i can do. As i already  
> > have all the test data for geo search probably tutorial on geo search could  
> > be a help too. As i did lot of search yesterday, i noticed all the same  
> > questions all around about geo search. As kimchy added support for Latvian  
> > stemming after one day i wrote an issue (which was essential one for us  
> > choosing ES) and released this feature in a week in new version i think i  
> > owe this community some help too 🙂
> > 
> > On Wednesday, April 25, 2012 12:34:35 AM UTC+3, Igor Motov wrote:
> > 
> > > I totally agree with you. It's quite confusing. By the way, docs are in  
> > > [https://github.com/\*\*elasticsearch/elasticsearch.\*\*github.com](https://github.com/ **elasticsearch/elasticsearch.** github.com)[https://github.com/elasticsearch/elasticsearch.github.com](https://github.com/elasticsearch/elasticsearch.github.com)Care to submit a pull request? 😉
> > > 
> > > On Tuesday, April 24, 2012 5:19:50 PM UTC-4, Kaspars Sprogis wrote:
> > > 
> > > > Wow, thanks. I spent half a day running tests, googling  
> > > > and finally decided it could be a bug and even reported.  
> > > > There should be some warning or exclamation marks like _"Important  
> > > > notice"_ on docs where it mentions format changes 🙂 it too easy to  
> > > > mis-read lat, lon vs lon, lat.
> > > > 
> > > > Thanks a lot.
> > > > 
> > > > On Wednesday, April 25, 2012 12:02:44 AM UTC+3, Igor Motov wrote:
> > > > 
> > > > > Yeah, it's one of these gotchas that many people run into. When you  
> > > > > specify location as an array, it follows GeoJson format [lon, lat], instead  
> > > > > of lat, lon. So, if you will flip the numbers in \_geo\_distance sort  
> > > > > location, it will work as expected:
> > > > > 
> > > > > "\_geo\_distance": {  
> > > > > "location": [  
> > > > > 24.113656,  
> > > > > 56.959942  
> > > > > ],  
> > > > > "order": "asc",  
> > > > > "unit": "km"  
> > > > > }
> > > > > 
> > > > > On Tuesday, April 24, 2012 4:19:53 PM UTC-4, Kaspars Sprogis wrote:
> > > > > 
> > > > > > Since in my test case all returned "sort" fields were almost  
> > > > > > identical (event if distance is different) i decided sort key is something  
> > > > > > different, some value of inner calculations.
> > > > > > 
> > > > > > So i think there is a bug somewhere with geo-search, so i reported  
> > > > > > whole test case to github issues  
> > > > > > [https://github.com/\*\*elasticsearch/elasticsearch/\*\*issues/1885](https://github.com/ **elasticsearch/elasticsearch/** issues/1885)[https://github.com/elasticsearch/elasticsearch/issues/1885](https://github.com/elasticsearch/elasticsearch/issues/1885)
> > > > > > 
> > > > > > Because if you look at my test case: [https://gist.github.com/](https://gist.github.com/)\*\*  
> > > > > > 2483009 [https://gist.github.com/2483009](https://gist.github.com/2483009)  
> > > > > > You will see that returned "sort" values are almost identical and  
> > > > > > also returned sort order is wrong...
> > > > > > 
> > > > > > Thanks, it is good to know "sort" actually should return distance,  
> > > > > > let's hope there is some explanation why problems in my test case appears.
> > > > > > 
> > > > > > On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:
> > > > > > 
> > > > > > > Take a look at this example: [https://gist.github](https://gist.github).\*\*com/2483149[https://gist.github.com/2483149](https://gist.github.com/2483149)
> > > > > > > 
> > > > > > > The value in the "sort" field is the distance:
> > > > > > > 
> > > > > > > ```
> > > > > > > "sort" : [114.81801790248922]
> > > > > > > 
> > > > > > > ```
> > > > > > > 
> > > > > > > On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> > > > > > > 
> > > > > > > > I look at that posting already, and i am already sorting by  
> > > > > > > > \_geo\_distance but no actual distance value is returned (using latest 0.9.2)
> > > > > > > > 
> > > > > > > > Then only in once place somewhere on stackoverflow i found that we  
> > > > > > > > need to use script\_fields to get distance to the results and i am wondering  
> > > > > > > > why in many places it is mentioned that distance should be in results if  
> > > > > > > > you are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > > > > > > > wrong, but i tested thousand times.
> > > > > > > > 
> > > > > > > > Only way i could get distance is like this:
> > > > > > > > 
> > > > > > > > "fields": [  
> > > > > > > > "\_source"  
> > > > > > > > ],  
> > > > > > > > "script\_fields": {  
> > > > > > > > "distance": {  
> > > > > > > > "params": {  
> > > > > > > > "lat": 56.959942,  
> > > > > > > > "lon": 24.113656  
> > > > > > > > },  
> > > > > > > > "script": "doc['location'].\*\*arcDistanceInKm(lat, lon)"  
> > > > > > > > }  
> > > > > > > > }
> > > > > > > > 
> > > > > > > > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > > > > > > > 
> > > > > > > > > have a look at this posting [http://groups.google](http://groups.google).\*\*  
> > > > > > > > > com/a/elasticsearch.com/group/ **users/browse\_thread/thread/**  
> > > > > > > > > fab566d2a275456b/\*\*2424d39029721e30?lnk=gst&q=+\*\*distance&pli=1[http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > > > > > > > 
> > > > > > > > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars Sprogis:
> > > > > > > > > 
> > > > > > > > > > I am also interested in this. Can anyone help? Thanks
> > > > > > > > > > 
> > > > > > > > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias  
> > > > > > > > > > Nothhaft wrote:
> > > > > > > > > > 
> > > > > > > > > > > Hi,
> > > > > > > > > > > 
> > > > > > > > > > > I'm trying to use elasticsearch for geo location search. It  
> > > > > > > > > > > works fine so using a filtered query and the geo\_distance filter. What I  
> > > > > > > > > > > miss in the search result is the distance between "my location" and each of  
> > > > > > > > > > > the results. Is it possible somehow to add the distance to the result? Can  
> > > > > > > > > > > someone give me an example or point me to some docs how to make it?
> > > > > > > > > > > 
> > > > > > > > > > > regards,  
> > > > > > > > > > > Matthias

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 27, 2012, 9:13am UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/13 "2012-04-27T09:13:34Z")

</div>

Yes, I definitely agree that it should be noticeable. I thought that it is  
(there is a special note in that format).

On Fri, Apr 27, 2012 at 12:03 PM, Kaspars Sprogis [darklow@gmail.com](mailto:darklow@gmail.com) wrote:

> The problem was not to found the correct guide page, but to notice that in  
> one place order of arguments differs from usual one (lat,lon, which uses  
> for storing, also google uses it) so it is very easy to mis-read it.  
> Yesterday i set up ES docs on ruby, so that i can contribute some guide  
> updates. Kimchy, do you agree that in place where lat, lon is way around  
> there should be some visually more noticeable Note element, so that users  
> really notice these changes? If you agree on that i can do that and  
> contribute.
> 
> On Friday, April 27, 2012 11:57:03 AM UTC+3, kimchy wrote:
> 
> > Heya, the formatting is specified here for example: [http://www](http://www).\*\*  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://elasticsearch.org/guide/**reference/api/search/sort.html)[http://www.elasticsearch.org/guide/reference/api/search/sort.html](http://www.elasticsearch.org/guide/reference/api/search/sort.html)  
> > \*\*. Its hard to find, I know..., and to be honest its confusing (the  
> > order) for me as well, it used to be the other way around, but we changed  
> > it to match the geojson spec.
> > 
> > On Wed, Apr 25, 2012 at 7:51 AM, Kaspars Sprogis wrote:
> > 
> > I'll explore how the docs are built and see what i can do. As i already
> > 
> > > have all the test data for geo search probably tutorial on geo search could  
> > > be a help too. As i did lot of search yesterday, i noticed all the same  
> > > questions all around about geo search. As kimchy added support for Latvian  
> > > stemming after one day i wrote an issue (which was essential one for us  
> > > choosing ES) and released this feature in a week in new version i think i  
> > > owe this community some help too 🙂
> > > 
> > > On Wednesday, April 25, 2012 12:34:35 AM UTC+3, Igor Motov wrote:
> > > 
> > > > I totally agree with you. It's quite confusing. By the way, docs are in  
> > > > [https://github.com/\*\*elasticsear\*\*ch/elasticsearch.\*\*github.com](https://github.com/ **elasticsear** ch/elasticsearch.**github.com)[https://github.com/elasticsearch/elasticsearch.github.com](https://github.com/elasticsearch/elasticsearch.github.com)Care to submit a pull request? 😉
> > > > 
> > > > On Tuesday, April 24, 2012 5:19:50 PM UTC-4, Kaspars Sprogis wrote:
> > > > 
> > > > > Wow, thanks. I spent half a day running tests, googling  
> > > > > and finally decided it could be a bug and even reported.  
> > > > > There should be some warning or exclamation marks like _"Important  
> > > > > notice"_ on docs where it mentions format changes 🙂 it too easy to  
> > > > > mis-read lat, lon vs lon, lat.
> > > > > 
> > > > > Thanks a lot.
> > > > > 
> > > > > On Wednesday, April 25, 2012 12:02:44 AM UTC+3, Igor Motov wrote:
> > > > > 
> > > > > > Yeah, it's one of these gotchas that many people run into. When you  
> > > > > > specify location as an array, it follows GeoJson format [lon, lat], instead  
> > > > > > of lat, lon. So, if you will flip the numbers in \_geo\_distance sort  
> > > > > > location, it will work as expected:
> > > > > > 
> > > > > > "\_geo\_distance": {  
> > > > > > "location": [  
> > > > > > 24.113656,  
> > > > > > 56.959942  
> > > > > > ],  
> > > > > > "order": "asc",  
> > > > > > "unit": "km"  
> > > > > > }
> > > > > > 
> > > > > > On Tuesday, April 24, 2012 4:19:53 PM UTC-4, Kaspars Sprogis wrote:
> > > > > > 
> > > > > > > Since in my test case all returned "sort" fields were almost  
> > > > > > > identical (event if distance is different) i decided sort key is something  
> > > > > > > different, some value of inner calculations.
> > > > > > > 
> > > > > > > So i think there is a bug somewhere with geo-search, so i reported  
> > > > > > > whole test case to github issues  
> > > > > > > [https://github.com/\*\*elasticsear\*\*ch/elasticsearch/\*\*issues/1885](https://github.com/ **elasticsear** ch/elasticsearch/**issues/1885)[https://github.com/elasticsearch/elasticsearch/issues/1885](https://github.com/elasticsearch/elasticsearch/issues/1885)
> > > > > > > 
> > > > > > > Because if you look at my test case: [https://gist.github.com/](https://gist.github.com/)\*\*  
> > > > > > > 248300\*\*9 [https://gist.github.com/2483009](https://gist.github.com/2483009)  
> > > > > > > You will see that returned "sort" values are almost identical and  
> > > > > > > also returned sort order is wrong...
> > > > > > > 
> > > > > > > Thanks, it is good to know "sort" actually should return distance,  
> > > > > > > let's hope there is some explanation why problems in my test case appears.
> > > > > > > 
> > > > > > > On Tuesday, April 24, 2012 11:01:23 PM UTC+3, Igor Motov wrote:
> > > > > > > 
> > > > > > > > Take a look at this example: [https://gist.github](https://gist.github). **c** om/2483149[https://gist.github.com/2483149](https://gist.github.com/2483149)
> > > > > > > > 
> > > > > > > > The value in the "sort" field is the distance:
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > "sort" : [114.81801790248922]
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > 
> > > > > > > > On Tuesday, April 24, 2012 2:55:58 PM UTC-4, Kaspars Sprogis wrote:
> > > > > > > > 
> > > > > > > > > I look at that posting already, and i am already sorting by  
> > > > > > > > > \_geo\_distance but no actual distance value is returned (using latest 0.9.2)
> > > > > > > > > 
> > > > > > > > > Then only in once place somewhere on stackoverflow i found that we  
> > > > > > > > > need to use script\_fields to get distance to the results and i am wondering  
> > > > > > > > > why in many places it is mentioned that distance should be in results if  
> > > > > > > > > you are sorting by \_geo\_distance if it is not?! Maybe i am doing something  
> > > > > > > > > wrong, but i tested thousand times.
> > > > > > > > > 
> > > > > > > > > Only way i could get distance is like this:
> > > > > > > > > 
> > > > > > > > > "fields": [  
> > > > > > > > > "\_source"  
> > > > > > > > > ],  
> > > > > > > > > "script\_fields": {  
> > > > > > > > > "distance": {  
> > > > > > > > > "params": {  
> > > > > > > > > "lat": 56.959942,  
> > > > > > > > > "lon": 24.113656  
> > > > > > > > > },  
> > > > > > > > > "script": "doc['location']. **arcDistanceIn** Km(lat, lon)"  
> > > > > > > > > }  
> > > > > > > > > }
> > > > > > > > > 
> > > > > > > > > On Tuesday, April 24, 2012 9:50:07 PM UTC+3, Peter Schröder wrote:
> > > > > > > > > 
> > > > > > > > > > have a look at this posting [http://groups.google](http://groups.google). **c**  
> > > > > > > > > > om/a/elasticsearch.com/group/ **u** sers/browse\_thread/thread/\*\*  
> > > > > > > > > > fab5\*\*66d2a275456b/\*\*2424d39029721e30? **lnk=gst&q=+**  
> > > > > > > > > > distance&pli=1[http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/fab566d2a275456b/2424d39029721e30?lnk=gst&q=+distance&pli=1)
> > > > > > > > > > 
> > > > > > > > > > Am Dienstag, 24. April 2012 19:51:59 UTC+2 schrieb Kaspars  
> > > > > > > > > > Sprogis:
> > > > > > > > > > 
> > > > > > > > > > > I am also interested in this. Can anyone help? Thanks
> > > > > > > > > > > 
> > > > > > > > > > > On Wednesday, September 7, 2011 6:58:47 PM UTC+3, Matthias  
> > > > > > > > > > > Nothhaft wrote:
> > > > > > > > > > > 
> > > > > > > > > > > > Hi,
> > > > > > > > > > > > 
> > > > > > > > > > > > I'm trying to use elasticsearch for geo location search. It  
> > > > > > > > > > > > works fine so using a filtered query and the geo\_distance filter. What I  
> > > > > > > > > > > > miss in the search result is the distance between "my location" and each of  
> > > > > > > > > > > > the results. Is it possible somehow to add the distance to the result? Can  
> > > > > > > > > > > > someone give me an example or point me to some docs how to make it?
> > > > > > > > > > > > 
> > > > > > > > > > > > regards,  
> > > > > > > > > > > > Matthias

---

<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:30am UTC](https://discuss.elastic.co/t/search-by-distance-and-getting-the-actual-distance/5326/14 "2017-07-06T03:30:56Z")

</div>


