dist(), uses the Haversine formula to compute the great circle distance in meters between two points.
To do so, you need to have:
Indexed content with latitude and longitude as metadata fields (see Understanding Fields).
Latitude and longitude fields loaded in memory (see Dynamic Fields).
Current user latitude and longitude at hand when making a query.
The following query leverages the
dist() function to retrieve the latitude and the longitude field data and compute the distance in meters from the user location, which is put in the newly created, dynamic
$qf(function:'dist(@latitude, @longitude, 37.77, -122.41)', fieldName: 'distance')
Then, the following query influences the result ranking based on the distance from the user location.
$qrf(expression: '-@distance', normalizeWeight: 'true')
You could also choose to sort results on the
distance field alone.
A query function generated field can be used almost anywhere a normal numerical field would. One notable exception is automatic facet ranges (see Coveo FacetRange Component (CoveoFacetRange)). If facet ranges are needed on query function generated fields, ranges must be provided at query time, as they cannot be determined by the indexer.
Geolocation is very resource-consuming and should not be used on a large index, unless items are filtered beforehand. Expect longer query times when using geolocation.
If the distance field is not needed, and the distance is only used to rank items, performance can be greatly improved by directly calling the
dist()function inside the ranking function, instead of referencing a distance field previously computed.
Coveo for Sitecore comes with a component to easily implement geolocation distances (see Adding a Custom Geolocation Provider).