Sorting Results Based on Distance
I know the Coveo JS framework currently supports sorting results based on the distance, but i have a scenario where one item can have multiple locations and I want Coveo to sort my results using the item's closest location to me. Does Coveo support this functionality?
I was also thinking I could duplicate the item for each location and then have Coveo filter out duplicates only based on the item's name for instance. I know Coveo supports filtering duplicates but can we specify which fields it uses to figure out if an item is a duplicate?
Coveo distance sorting works with a Query Function (QF) that creates an additional field on search results calculated from their proximity from a fixed location. To work, the results location need to be stored in a latitude and longitude floating point fields.
In your case, having multiple locations per search result implies your latitudes and longitudes are probably stored in a single multi-value facet string field which isn't supported for distance calculation.
If you have a fixed maximum number of locations per search result, you could store each latitude and longitude in separate floating point fields and determine the shortest distance of a search result from a fixed location using the
min() query function and use the resulting
@shortestDistance field as your sort field. Example with only 2 possible locations:
$qf(function:'min(dist(@latitude1, @longitude1, 46.8167, -71.2167), dist(@latitude2, @longitude2, 46.8167, -71.2167))', fieldName: 'shortestDistance')
If you don't have a fixed number of locations per search result, I think you could craft a more complex query function that would iterate each latitude/longitude to calculate the distance and keep only the shortest. That would require string to float conversion, array iteration, variable definition… All the syntax of ExprTk is supported. I never tried it but it might be feasible.
As for the duplicate filtering, the logic to determine duplicates is fixed. The index is comparing the body, title and other properties of documents to determine if they are the same. I'm not sure if a single different field value would make all the results unique or duplicates.
I hope this helps,