nhsuk / gp-finder

GP Finder
MIT License
5 stars 1 forks source link

ES Geo Query - distance and units thresholds #145

Closed beseven closed 7 years ago

beseven commented 7 years ago

As part of the postcode validation, it was raised that YO62 4LA is 3.6 mi away from the nearest GP but the current radius is of 5km. A couple of solutions:

neilbmclaughlin commented 7 years ago

In ES there is the concept of queries and filters. The distance is a filter as I believe it is used to exclude locations based on a rough but computationally effective calculation and only then calculates distance on the remainder. This means it doesn't have to do a computationally intensive distance calculation on the whole index.

The distance we use can be set to something much larger than that which any user would travel and then return the top n ordered by distance.

neilbmclaughlin commented 7 years ago

A quick play with the API via curl showed that extending the distance to 50 miles and adding a size property to the query had the desired result.

Example

{ 
  "size": 3,
  "query": {
    "bool": {
      "must" : {
        "match_all" : {}
      },
      "filter": {
        "geo_distance": {
          "distance": "50mi", 
          "location.coordinates": { 
            "lon": -1.46519099452929,
            "lat": 54.0095586395326
          }
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location.coordinates": {
          "lon": -1.46519099452929,
          "lat": 54.0095586395326
        },
        "order":         "asc",
        "unit":          "mi",
        "distance_type": "plane"
      }
    }
  ]
}

That lat/long above previously returned 0 results.

beseven commented 7 years ago

@neilbmclaughlin Do you want me to add this in #144 ?

neilbmclaughlin commented 7 years ago

Resolved by extending radius to 50 miles.