Guides / Managing results / Refine results / Sorting results

Exhaustive sorting refers to strictly reordering results by an attribute. For example, you can exhaustively sort results from lowest to highest price, regardless of textual and business relevance. It’s intended for use cases requiring strict sorting or where relevance isn’t essential, like an inventory app or a database.

An alternative to exhaustive sorting is relevant sorting.

Effect on ranking formula

In Algolia, each of your indices has a ranking formula of two or three parts:

Ranking formula

Sort-by attribute

You should only add a sort-by attribute to replica indices.

If a sort-by attribute is present, the engine first orders results by the sort-by attribute’s value.

For example, suppose you have an index where you’ve set “price (ascending)” as a sort-by attribute. For the query “iphone”, the engine first selects all records that match “iphone” (and alternatives like prefixes, typos, and synonyms) in the searchableAttributes. Then, it orders the results from lowest to highest price.

  • Once sorted by the sort-by attribute’s value (price), products with the same price are ordered according to Algolia’s default textual ranking criteria.
  • If records have the same values for all textual ranking criteria, the engine orders them by custom ranking.

In summary, textual ranking criteria come after your selected sort-by attribute.

With exhaustive sorting, if the sort-by attribute values are very granular and or relatively unique, the textual ranking criteria won’t have a significant role in ranking. For example, consider exhaustively sorting on a timestamp attribute down to the millisecond. If none of your records have the same timestamp value, the timestamp is the only attribute that plays a role in ranking. Depending on your use case, this may be what you want, or you may want to decrease your timestamp granularity, or relevant sorting might be a more appropriate choice.

Backend implementation

To ensure a fast search experience, the engine sorts your data at indexing time. Therefore, each of your indices can only be sorted in one way. However, to allow for multiple sort orders, Algolia uses replica indices.

Exhaustive sorting uses standard index replicas

A replica is a copy of one of your indices, with the same data and synchronized data updates, but can have unique settings. The index from which you copy a replica’s data is the replica’s primary index. If you want to configure an exhaustive sort-by, use standard replicas.

For each attribute you want to sort by, create a replica index, which contains a copy of all the data in your primary index.

Frontend implementation

Replica indices manage the backend of sorting, but you still need to implement the frontend with custom logic or InstantSearch’s SortBy UI widget.

SortBy Widget

API Reference SortBy Widget
Did you find this page helpful?