
E
Endru Reza
Posted on Nov 13
Sorting By Attribute On Algolia
Since Algolia search is done by indexing, it's not returning the latest result by default. Because, what matters is relevance when you are doing some searching. But, still it provides a slighlity detour way to do sorting. There is called Ranking in Algolia. For most of the time, the ranking by default is shown by picture below:

The easiest way is to add attribute manually, an attribute that you want to refer as sorted one. But, we can't do this on primary index. There are some reasons why you can't do it on primary index:
- Performance Impact: Since primary index is utilized to do full-text search, by adding an additonal step of sorting, it will slow the query. It's getting worse, if we already had large datasets.
- Relevancy Issue: Algolia prioritize search relevancy. It may conflict with the sorting and will lead to ignore relevancy.
- Cost Implications: Sorting may lead to additional computing resources and time, which would increase quota usage.
Hence, Algolia recommends Replica to do this.
Create Replica
- Select your index.
- Go to replica and create new replica. (tips: make sure you name the replica unique based on sorting attribute, ex: indexName_date_desc).

Add Custom Ranking Attributes
- Choose the replica index.
- Go to configuration and choose Ranking and Sorting.
- Create custom ranking, in my case createdAt. Because i want to have index that sorted by the latest date.

Now, you just need to use that index to search. Good Luck !
2 min read