Clarification on geo distance sorting versus geopoint field distance method

(Raul, Jr. Martinez) #1


I'm interested on how others have implemented the use-case that I'd like to


  • For each document, a field called "geoloc" is available (most but not
    all) which is a geopoint field.
  • On the front-end, each document, when geoloc, is available must
    display the "distance" based on the user's geographical location (lat/lon)
  • Allow the user to sort by "nearest distance"

ISSUE: Based on my implementation (below), the geopoint distance() method
is returning a different value compared with what's being computed by the
sort handler. It's a discrepancy that I don't seem to understand.

ES version: 0.17.6

Sample Result (showing the discrepancy between sort and geodistance
computed fields

    "_shard": 4,
    "_node": "yDPteNQkT2e-JfWkdNVEnQ",
    "_index": "asoc",
    "_type": "business",
    "_id": "4fcf818695f08d99090000d0",
    "_score": null,
    "fields": {
      "tags": null,
 "geoloc: { lat: 34.243161065225, lon: -111.31945564679"}
      "dtmPosted": "2012-06-06 16:12:54",
      "title": "Soaring Sky LLC",
      "username": "user123",
      "postedBy": null,
      "rating": 1,
      "geodistance": 32.4533052120666     ---> computed via 

"sort": [
27.45321291360537 --> geo distance sort.
"_explanation": {
"value": 1,
"description": "ConstantScore(:), product of:",
"details": [
"value": 1,
"description": "boost"
"value": 1,
"description": "queryNorm"

Following are the two important parameters being sent to ElasticSearch.

  1. *The Geo Distance Script fields parameters
 "script_fields": {
   "geodistance": {
     "params": {
       "lat": 42.3788774,
       "lon": -72.032366
     "script": "doc['geoloc'].empty ? null : 

doc['geoloc'].distance(lat, lon)"

The Sort specification parameters

 "sort": {
   "_geo_distance": {
     "field": "gd",
     "order": "asc",
     "unit": "mi",
     "default": "1",
     "geoloc": {
       "lat": 42.3788774,
       "lon": -72.032366

Please let me know what i'm missing.



(Thorsten-2) #2

Did you find any solution to this problem? I'm experiencing the same



(Thorsten-2) #3

Found the solution:

You need to use the arcDistanceInKm(lat,lon) method to get the same
results as in the geo_filter.


(system) #4