In Elasticsearch.js .
elsaticsearch version is 2.3.4
use code like this
body: {
from: data['offset'] || 0
, size: data['limit'] || 20
, query: {
bool: {
must: {
range: {
state: {
gte: 0
}
}
}
, filter: {
geo_distance_range: {
from: data['from'] || '0km'
, to: data['to'] || '5km'
, location: {
lat: data['lat']
, lon: data['lon']
}
}
}
}
}
, sort: [{
_geo_distance: {
location: {
lat: data['lat']
, lon: data['lon']
}, order: 'asc'
, unit: 'm'
}
}]
, script_fields: {
distance: {
lang: "groovy",
script: "doc['location'].distanceInKm(" + data['lat'] + "," + data['lon'] + ")*1000"
}
}
}
run that, just get
[
{
"_index": "stores_location_v1",
"_type": "location_info",
"_id": "65",
"_score": null,
"fields": {
"distance": [
632.513773747282
]
},
"sort": [
631.9282534390322
]
},
{
"_index": "stores_location_v1",
"_type": "location_info",
"_id": "100976",
"_score": null,
"fields": {
"distance": [
772.123560941117
]
},
"sort": [
656.1648199724189
]
},
{
"_index": "stores_location_v1",
"_type": "location_info",
"_id": "64",
"_score": null,
"fields": {
"distance": [
663.1312353690903
]
},
"sort": [
662.5164209175506
]
},
{
"_index": "stores_location_v1",
"_type": "location_info",
"_id": "100542",
"_score": null,
"fields": {
"distance": [
695.1804755172814
]
},
"sort": [
669.5809632061855
]
}
]
The result calculated by the _geo_distance in sort is not equal to the result calculated using the distanceInKm method in script_fields .
I want to know why this is? Then what should be done?
Is there a way to replace distanceInKm
form
-based computations instead of km
and then * 1000.
Is this part of the reason for the loss of accuracy?
And also, I just want to calculate the distance between one point and another point, how should be calculated? Direct use script_fields`` distanceInKm
approach?