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.