Incorrect distance using geo point calculation, off by a lot

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.comwrote:

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&prettyhttp://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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

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
Elasticsearch Platform — Find real-time answers at scale | Elastic.
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' -d '
{
"mappings" : {
"test" : {
"properties" : {
"lat_lon" : {
"type" : "geo_point"
}
}
}
}
}
'

curl -XPOST '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' -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.comwrote:

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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.