# Incorrect distance using geo point calculation, off by a lot

**URL:** <https://discuss.elastic.co/t/incorrect-distance-using-geo-point-calculation-off-by-a-lot/11253>\
**Category:** Elasticsearch\
**Created:** [March 22, 2013, 2:25am UTC](https://discuss.elastic.co/t/incorrect-distance-using-geo-point-calculation-off-by-a-lot/11253 "2013-03-22T02:25:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno\_Miranda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruno_miranda/32/746_2.png) [@Bruno\_Miranda](https://discuss.elastic.co/u/Bruno_Miranda)\
**Post date:** [March 22, 2013, 2:25am UTC](https://discuss.elastic.co/t/incorrect-distance-using-geo-point-calculation-off-by-a-lot/11253/1 "2013-03-22T02:25:15Z")

</div>

Using 0.20.5, I am trying to calculate the distance between 2 geo points as  
such:

[http://0.0.0.0:9200/development\_facilities/facility/\_search?load=false&size=10&pretty](http://0.0.0.0:9200/development_facilities/facility/_search?load=false&size=10&pretty)  
'

{  
"fields" : ["\_source"],  
"script\_fields" : {  
"distance" : {  
"script" : "doc['lat\_lon'].distance(32.81,-117.21)"  
}  
}  
}  
'

And here's the result:

{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "development\_facilities",  
"\_type": "facility",  
"\_id": "663",  
"\_score": 1,  
"\_source": {  
"id": 663,  
"name": "Rady Children's Hospital & Health Center ",  
"zip": "92123",  
"beds": 292,  
"full\_address": "3020 Children's Way, San Diego, CA,  
92123",  
"short\_name": "Rady Children's Hosp & Hlth Ctr",  
"state": "CA",  
"city": "San Diego",  
"type\_code": null,  
"lat\_lon": [  
32.798,  
-117.151  
]  
},  
"fields": {  
"distance": 6934.980565249466  
}  
}  
]  
}  
}

As you can see the lat/lon is pretty should, should only be a few miles  
away, yet the calculated distance field should 6934.98, can someone spot  
what I am doing wrong?

Thank you

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Bruno\_Miranda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruno_miranda/32/746_2.png) [@Bruno\_Miranda](https://discuss.elastic.co/u/Bruno_Miranda)\
**Post date:** [March 22, 2013, 2:42am UTC](https://discuss.elastic.co/t/incorrect-distance-using-geo-point-calculation-off-by-a-lot/11253/2 "2013-03-22T02:42:32Z")

</div>

A few updates, if I change to arcDistance:

{  
"fields" : ["\_source"],  
"script\_fields" : {  
"distance" : {  
"script" : "doc['lat\_lon'].arcDistance(-117.151, 32.898)"  
}  
}  
}

And reverse the order of the lat, lon inside the arcDistance() call, it  
works:

"fields": {  
"distance": 3.15  
}

Reversing the order of the lat/lon and using distance() or distanceInKm()  
however _still does not work_

On Thursday, March 21, 2013 7:25:15 PM UTC-7, Bruno Miranda wrote:

> Using 0.20.5, I am trying to calculate the distance between 2 geo points  
> as such:
> 
> http://  
> 0.0.0.0:9200/development\_facilities/facility/\_search?load=false&size=10&pretty'
> 
> {  
> "fields" : ["\_source"],  
> "script\_fields" : {  
> "distance" : {  
> "script" : "doc['lat\_lon'].distance(32.81,-117.21)"  
> }  
> }  
> }  
> '
> 
> And here's the result:
> 
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 1,  
> "successful": 1,  
> "failed": 0  
> },  
> "hits": {  
> "total": 1,  
> "max\_score": 1,  
> "hits": [  
> {  
> "\_index": "development\_facilities",  
> "\_type": "facility",  
> "\_id": "663",  
> "\_score": 1,  
> "\_source": {  
> "id": 663,  
> "name": "Rady Children's Hospital & Health Center ",  
> "zip": "92123",  
> "beds": 292,  
> "full\_address": "3020 Children's Way, San Diego, CA,  
> 92123",  
> "short\_name": "Rady Children's Hosp & Hlth Ctr",  
> "state": "CA",  
> "city": "San Diego",  
> "type\_code": null,  
> "lat\_lon": [  
> 32.798,  
> -117.151  
> ]  
> },  
> "fields": {  
> "distance": 6934.980565249466  
> }  
> }  
> ]  
> }  
> }
> 
> As you can see the lat/lon is pretty should, should only be a few miles  
> away, yet the calculated distance field should 6934.98, can someone spot  
> what I am doing wrong?
> 
> Thank you

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 22, 2013, 8:17am UTC](https://discuss.elastic.co/t/incorrect-distance-using-geo-point-calculation-off-by-a-lot/11253/3 "2013-03-22T08:17:58Z")

</div>

Hey Bruno,

this is strange. I just tested with elasticsearch 0.90 (which includes a  
couple of improvements in geo stuff) and 0.20.5, and it looks ok.

curl -X PUT localhost:9200/geotest  
curl -X PUT localhost:9200/geotest/geotest/\_mapping -d '{"geotest": {  
"properties": { "location" : { "type":"geo\_point" } } }}'  
curl -X PUT localhost:9200/geotest/geotest/1 -d '{ "location":" 32.798,  
-117.151 " }'  
curl -X POST localhost:9200/geotest/geotest/\_search -d '{ "query": {  
"match\_all" : {} }, "script\_fields" : { "distance" : { "script":  
"doc["location"].distance(32.81,-117.21)" } } }'

{"took":2,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max\_score":1.0,"hits":[{"\_index":"geotest","\_type":"geotest","\_id":"1","\_score":1.0,"fields":{"distance":4.164719280569508}}]}}

Maybe I changed coordinates somewhere while trying to copy and paste. Can  
you provide some gists reproduce?

--Alex

On Fri, Mar 22, 2013 at 3:42 AM, Bruno Miranda [bru.miranda@gmail.com](mailto:bru.miranda@gmail.com)wrote:

> A few updates, if I change to arcDistance:
> 
> {  
> "fields" : ["\_source"],  
> "script\_fields" : {  
> "distance" : {
> 
> ```
> "script" : "doc['lat_lon'].arcDistance(-117.151, 32.898)"
> }
> 
> ```
> 
> }  
> }
> 
> And reverse the order of the lat, lon inside the arcDistance() call, it  
> works:
> 
> "fields": {  
> "distance": 3.15  
> }
> 
> Reversing the order of the lat/lon and using distance() or distanceInKm()  
> however _still does not work_
> 
> On Thursday, March 21, 2013 7:25:15 PM UTC-7, Bruno Miranda wrote:
> 
> > Using 0.20.5, I am trying to calculate the distance between 2 geo points  
> > as such:
> > 
> > [http://0.0.0.0:9200/\*\*development\_facilities/](http://0.0.0.0:9200/**development_facilities/)\*\*  
> > facility/\_search?load=false&\*\*size=10&pretty[http://0.0.0.0:9200/development\_facilities/facility/\_search?load=false&size=10&pretty](http://0.0.0.0:9200/development_facilities/facility/_search?load=false&size=10&pretty)'
> > 
> > {  
> > "fields" : ["\_source"],  
> > "script\_fields" : {  
> > "distance" : {  
> > "script" : "doc['lat\_lon'].distance(32.\*\*81,-117.21)"  
> > }  
> > }  
> > }  
> > '
> > 
> > And here's the result:
> > 
> > {  
> > "took": 2,  
> > "timed\_out": false,  
> > "\_shards": {  
> > "total": 1,  
> > "successful": 1,  
> > "failed": 0  
> > },  
> > "hits": {  
> > "total": 1,  
> > "max\_score": 1,  
> > "hits": [  
> > {  
> > "\_index": "development\_facilities",  
> > "\_type": "facility",  
> > "\_id": "663",  
> > "\_score": 1,  
> > "\_source": {  
> > "id": 663,  
> > "name": "Rady Children's Hospital & Health Center ",  
> > "zip": "92123",  
> > "beds": 292,  
> > "full\_address": "3020 Children's Way, San Diego, CA,  
> > 92123",  
> > "short\_name": "Rady Children's Hosp & Hlth Ctr",  
> > "state": "CA",  
> > "city": "San Diego",  
> > "type\_code": null,  
> > "lat\_lon": [  
> > 32.798,  
> > -117.151  
> > ]  
> > },  
> > "fields": {  
> > "distance": 6934.980565249466  
> > }  
> > }  
> > ]  
> > }  
> > }
> > 
> > As you can see the lat/lon is pretty should, should only be a few miles  
> > away, yet the calculated distance field should 6934.98, can someone spot  
> > what I am doing wrong?
> > 
> > Thank you
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 22, 2013, 10:28am UTC](https://discuss.elastic.co/t/incorrect-distance-using-geo-point-calculation-off-by-a-lot/11253/4 "2013-03-22T10:28:37Z")

</div>

The problem here is that you are using an array to specify a geopoint, and  
the format was changed to conform with the GeoJSON spec, as mentioned on  
[Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/mapping/geo-point-type.html).  
Instead of being [lat,lon], it's actually [lon,lat].

That issue is compounded by the fact that longitudes are normalized into  
the range -90 .. 90 so when you use -117 has a longitude it actually  
becomes 63

If you swap the values around, then it works correctly:

curl -XPUT '[http://127.0.0.1:9200/test/?pretty=1](http://127.0.0.1:9200/test/?pretty=1)' -d '  
{  
"mappings" : {  
"test" : {  
"properties" : {  
"lat\_lon" : {  
"type" : "geo\_point"  
}  
}  
}  
}  
}  
'

curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{  
"short\_name" : "Rady Children\u0027s Hosp & Hlth Ctr",  
"name" : "Rady Children\u0027s Hospital & Health Center ",  
"state" : "CA",  
"zip" : "92123",  
"full\_address" : "3020 Children\u0027s Way, San Diego, CA, 92123",  
"city" : "San Diego",  
"beds" : 292,  
"lat\_lon" : [  
-117.151,  
32.798  
],  
"type\_code" : "null",  
"id" : 663  
}  
'

curl -XGET '[http://127.0.0.1:9200/test/test/\_search?pretty=1](http://127.0.0.1:9200/test/test/_search?pretty=1)' -d '  
{  
"fields" : "\_source",  
"script\_fields" : {  
"distance" : {  
"script" : "doc[\u0027lat\_lon\u0027].distance(32.81,-117.21)"  
}  
}  
}  
'

# {

# "hits" : {

# "hits" : [

# {

# "\_source" : {

# "short\_name" : "Rady Children's Hosp & Hlth Ctr",

# "name" : "Rady Children's Hospital & Health Center

# \> ",

# "state" : "CA",

# "zip" : "92123",

# "full\_address" : "3020 Children's Way, San Diego,

# \> CA, 92123",

# "city" : "San Diego",

# "beds" : 292,

# "id" : 663,

# "lat\_lon" : [

# -117.151,

# 32.798

# ],

# "type\_code" : "null"

# },

# "\_score" : 1,

# "fields" : {

# "distance" : 4.16462911860311

# },

# "\_index" : "test",

# "\_id" : "KZL8CQXZQ6aKKEDUrjv5Ag",

# "\_type" : "test"

# }

# ],

# "max\_score" : 1,

# "total" : 1

# },

# "timed\_out" : false,

# "\_shards" : {

# "failed" : 0,

# "successful" : 5,

# "total" : 5

# },

# "took" : 5

# }

DB\<19\>

On Fri, Mar 22, 2013 at 3:25 AM, Bruno Miranda [bru.miranda@gmail.com](mailto:bru.miranda@gmail.com)wrote:

> Using 0.20.5, I am trying to calculate the distance between 2 geo points  
> as such:
> 
> http://  
> 0.0.0.0:9200/development\_facilities/facility/\_search?load=false&size=10&pretty'
> 
> {  
> "fields" : ["\_source"],  
> "script\_fields" : {  
> "distance" : {  
> "script" : "doc['lat\_lon'].distance(32.81,-117.21)"  
> }  
> }  
> }  
> '
> 
> And here's the result:
> 
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 1,  
> "successful": 1,  
> "failed": 0  
> },  
> "hits": {  
> "total": 1,  
> "max\_score": 1,  
> "hits": [  
> {  
> "\_index": "development\_facilities",  
> "\_type": "facility",  
> "\_id": "663",  
> "\_score": 1,  
> "\_source": {  
> "id": 663,  
> "name": "Rady Children's Hospital & Health Center ",  
> "zip": "92123",  
> "beds": 292,  
> "full\_address": "3020 Children's Way, San Diego, CA,  
> 92123",  
> "short\_name": "Rady Children's Hosp & Hlth Ctr",  
> "state": "CA",  
> "city": "San Diego",  
> "type\_code": null,  
> "lat\_lon": [  
> 32.798,  
> -117.151  
> ]  
> },  
> "fields": {  
> "distance": 6934.980565249466  
> }  
> }  
> ]  
> }  
> }
> 
> As you can see the lat/lon is pretty should, should only be a few miles  
> away, yet the calculated distance field should 6934.98, can someone spot  
> what I am doing wrong?
> 
> Thank you
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:45am UTC](https://discuss.elastic.co/t/incorrect-distance-using-geo-point-calculation-off-by-a-lot/11253/5 "2017-07-06T02:45:08Z")

</div>


