How to get the distance between two geo points in the elasticsearch?
Use a script field with the same reference point as in the geo_distance
filter. Full query:
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "5000km",
"pin.location": [
-70,
40
]
}
}
}
},
"script_fields": {
"distance_in_m": {
"script": "doc['pin.location'].arcDistance(40, -70)"
}
}
}
OR
geo-sort and you'll get the distance info included in the sort response.
Return distance in elasticsearch results?
Yes you can, by using a script field.
For instance, assuming your doc have a geo-point field called location
, you could use the following:
(note the \u0027
is just an escaped single quote, so \u0027location\u0027
is really 'location'
)
curl -XGET 'http://127.0.0.1:9200/geonames/_search?pretty=1' -d '
{
"script_fields" : {
"distance" : {
"params" : {
"lat" : 2.27,
"lon" : 50.3
},
"script" : "doc[\u0027location\u0027].distanceInKm(lat,lon)"
}
}
}
'
# [Thu Feb 16 11:20:29 2012] Response:
# {
# "hits" : {
# "hits" : [
# {
# "_score" : 1,
# "fields" : {
# "distance" : 466.844095463887
# },
# "_index" : "geonames_1318324623",
# "_id" : "6436641_en",
# "_type" : "place"
# },
... etc
If you want the _source
field to be returned as well, then you can specify that as follows:
curl -XGET 'http://127.0.0.1:9200/geonames/_search?pretty=1' -d '
{
"fields" : [ "_source" ],
"script_fields" : {
"distance" : {
"params" : {
"lat" : 2.27,
"lon" : 50.3
},
"script" : "doc[\u0027location\u0027].distanceInKm(lat,lon)"
}
}
}
'
compute geo distance in elasticsearch
Sorting by _geo_distance
will return the distance in the results:
http://www.elasticsearch.org/guide/reference/api/search/sort.html
How to get distance from elasticsearch.net / NEST for a geo_point field
You need to use script field to return distance
"script_fields":{
"distance":{
"script":"doc['latlng'].arcDistance(params.lat,params.lng)",
"params":{
"lat":<some value>,
"lng":<some value>
}
}
}
Nest
var scriptFields = new ScriptFields
{
{
"distance", new ScriptField {
Script = new InlineScript( "if(doc['"+field+"'].size() > 0) { doc['"+field+"'].arcDistance(params.lat,params.lon) }")
{
Params=new FluentDictionary<string, object>
{
{ "lat", latitude},
{ "lon", longitude}
}
}
}
}
};
How to calculate geographic distance in a Painless script (OpenSearch 1.0)
It looks like distanceInKm
and distanceInMiles
were both replaced some time ago by arcDistance(double lat, double lon)
and planeDistance(double lat, double lon)
- and all distances are now returned in meters.
See here for an explanation of how these two are different.
Return distance in geo distance query in elastic search 5.x
The method distanceInKm
was deprecated and is removed now (5.x). Use for example arcDistance
instead. More information here: https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_50_scripting.html#_geopoint_scripts
The syntax is a little bit changed. This works for me in Elasticsearch 5.6.
{
"script_fields": {
"geo_distance": {
"script": {
"params": {
"lat": 18.00,
"lon": 72.00
},
"inline": "doc['location'].arcDistance(params.lat, params.lon)"
}
}
}
}
Units of an elasticsearch query to get distance from arbitrary point to Geopoint
The units are those returned by the arcDistance
method providing the value in your script.
The arc distance (in meters) of this geo point field from the provided lat/lon
The painless docs leave a lot to be desired (there appears to be no docs on this method in 6.5). The quote above was obtained from here: https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html
Additionally, they mention arcDistance
caluclates meters here: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/breaking_50_scripting.html
elastic search query for geo points distance
finally i used script:
GET development-restaurants/restaurants/_search
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : {
"source": "doc['restaurantPoint'].arcDistance(35.73702, 51.40909) <= doc['deliveryRadius'].value",
"lang": "painless"
}
}
}
}
}
}
Related Topics
Have a Parent Class's Method Access the Subclass's Constants
How to Pass Named Arguments to a Rake Task
Rails 3.1 Rspec Creating Test Case Validate Field for Model
Ruby: How to Join Elements of an Array Together with a Prefix
Installing Pl/Ruby for Postgresql 8.3
Rails 3/ Git/ Bundler Fatal Could Not Parse Object
Which Http Web Server How to Use to Debug Ruby Code Using Rubymine 3.0.1
How to Replicate Class_Inheritable_Accessor's Behavior in Rails 3.1
List of Ruby Metaprogramming Methods
Why Array.Reverse_Each Is Faster Than Array.Reverse.Each
Logger.Debug Not Writing to Log File in Rails
"Cannot Load Such File - Rubygems/Defaults/Operating_System" When Installing Ruby 2.0.0
Ruby Comparison Operators? == VS. ===
Cancan Load_And_Authorize_Resource Triggers Forbidden Attributes
How to Iterate Over an Array of Arrays
Mocking Chain of Methods in Rspec
Rails 3. How to Explicitly Round a Number to Two Decimal Places in The Model