Vishnu S Kumar
Vishnu S Kumar

Reputation: 686

How to get the distance between two geo points in the elasticsearch?

The following Query in the elasticsearch will give results located within the specified distance. The result doesn't includes the actual distance. Is there any way to get this information in elasticsearch ?

"query": {
    "bool" : {
        "must" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km",
                "pin.location" : [-70, 40]
            }
        }
    }
}

Upvotes: 2

Views: 3331

Answers (1)

Joe - Check out my books
Joe - Check out my books

Reputation: 16925

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.

Upvotes: 3

Related Questions