Search code examples
sortingdistancealgolia

How to sort records by distance using Algolia?


Using Algolia Search, I’m trying sort my records by distance without filtering them by a radius but I fail to do so. I can successfully filter by a radius by setting aroundLatLng and aroundRadius in my query. But records are not sorted in the result. According to this part of Algolia's documentation, a ranking is created based on the distance from the central point. So, records must be sorted by distance if there aren't any sorting in the query. According to this, to sort by distance without filtering, one needs to set aroundRadius to "all". I've tried setting aroundRadius to "all" but it didn't change anything.

Also in this part of the docs, it’s stated that geo criterion must be present in the ranking formula to sort by distance and I confirmed that it is present. So what could be the problem here? Any help would be appreciated. Note that there is location info in every record with the key "_geoloc".


Solution

  • The problem was that the default ranking criteria did not exist. In Ranking and Sorting section of my index, I've reset settings to default and the default ranking criteria appeared. After that, I've been able sort records by distance. Below is a screenshot that shows the Ranking and Sorting section after resetting settings to default.

    enter image description here