2

I have a django project which uses elasticsearch 6.5.3 to index products in a store with locations as GeoPoints. I am trying to query this index and also calculate distance between an arbitrary point, say user's location to each oh these results.

I am using elasticsearch_dsl and my code looks something like this:

search_query = search_query.script_fields(distance={
'script':{
    'inline':"doc['location'].arcDistance(params.lat, params.lon)", 
    'params': {
        'lat':user_loc.lat, 
        'lon':user_loc.lon
        }
    }
})
for result in search_query.execute():
    print(result.distance)

Which gives me values that looks like:

[123456.456879123]

But I'm not sure about its units. By using and online distance calculator in https://www.nhc.noaa.gov/gccalc.shtml, which gives me the distance as ~123km, It looks like value is in meters.

So:

1. Where can I find some definitive answers about its units?

Please point me to the relevant documentation for these methods. I am also interested to know if there is a way to specify the units expected for the results in the method call.

2. Is there a better way to do this in python?

philoj
  • 468
  • 2
  • 13

2 Answers2

2

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

hudsonb
  • 2,214
  • 19
  • 20
0

I'm not sure about the exact python API, but elasticsearch have Geo Distance Query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html

In: https://github.com/elastic/elasticsearch-dsl-py/issues/398 there's an example of python usage of ES API:

MyDocType.search().filter(
'geo_distance', distance='1000m', location={"lat": "40", "lon": "-74"}
)

The 'geo_distance' query is the easiest way to get a distance between two geo points indexed to elasticsearch. I thinking that you don't need to use scripting in order to achieve that.

Regarding the distance unit, as you suspected the default is meters. from: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#distance-units

Wherever distances need to be specified, such as the distance parameter in the Geo Distance Query), the default unit if none is specified is the meter.

  • geo_distance queries filters and ranks results. but how to get the exact distance value that ES used to do this? What if i need to calculate distance in a pre-filtered query and sort it in a different way? – philoj Jan 10 '19 at 10:28