Concepts / Managing results / Sorting results
May. 10, 2019

Sorting Results

Two Kinds of Sorting Strategies

To understand sorting, it’s worth comparing the two kinds of sorting strategies Algolia offers:

Sorting by relevance (ranking)

The ranking strategy sorts by textual and business relevance. It follows a tie-breaking comparison process, using 8 criteria as tie breakers. These include typos, filters, and exact matching. If two records are still tied after being compared textually (using the first 7 criteria), the next step is to apply business criteria, using custom ranking attributes. With custom ranking, you strengthen relevance with your own business metrics.

In the end, your records are sorted by relevance - both textually and business-wise.

Sorting by attribute (ascending, descending)

Sorting by attribute, which is called “sort-by” on the Dashboard, refers to a more straightforward sorting that relies on a single attribute, like price or date. Another name for this is attribute-based sorting.

Attribute-based sorting comes before the tie-breaking relevance criteria. This is how it differs from the above-mentioned sorting by relevance, which is applied only after the tie-breaking.

Therefore, the process is as follows:

  • The first step is to sort results by the values in their sort-by attributes. The main goal of attribute-based sorting is, therefore, to ensure that all records which match the query appear in the order as determined by the sort-by attribute.
  • The next step, if needed, is to manage what happens when two or more records have the same value in the sort-by attribute. This is where the tie-breaking algorithm comes into play: any records that are equal go through the tie-breaking algorithm, including custom ranking.

On the dashboard

It’s important to understand how the dashboard represents these two strategies. You have 3 configurable items you can use to order your records.

  • Algolia’s tie-breaking algorithm is the heart of textual relevance. You have access to all tie-breaking criteria, in their default order. You’ll rarely need to touch this.
  • Custom ranking supplements your textual relevance by injecting your own business metrics. Notice that when you add custom ranking, it goes to the bottom of the tie-breaking formula. It’s therefore only triggered if the ranking formula cannot break the tie.
  • Sort-By. The sort-by feature places the sorting attribute(s) above the ranking formula to indicate its overriding effect. This creates a straightforward sort. This can be used with or without any further ranking.


By design, Algolia provides one ranking formula per index. Every index has a unique sorting strategy, which is statically defined at the index level, and cannot be changed at query time. Pre-sorting at index time instead of at query time leads to a considerable performance boost.

Whenever you want to provide different rankings for the same data, you therefore need to use replica indices.

Algolia provides this primary/replica relationship to let you automatically replicate the content of one index (the primary) onto other indices (the replicas).

What’s in a replica?

Replicas are linked to their primary index. A primary index and its replicas have many things in common:


Replicas have the exact same data as their primary index, and data synchronization is done automatically for you. Every time you add, update, or delete records in your primary index, your replica is simultaneously modified. You can’t alter this automatic data synchronization. You can’t, for example, directly add, update, or delete records in your replica indices.


Replicas start with the exact same settings as their primary index. However, unlike with data, you can change a replica’s settings. Typically, though, you’ll want to keep most settings in sync with your primary index. You ensure setting synchronization by forwarding all settings changes made to your index to your replicas. You can achieve this with the forwardToReplica parameter.

The most common settings that differ in each replica is the sorting strategy.

How to Create and Use Replica Indices

Multiple ranking formulas are useful to implement different sorting strategies (e.g., sort by price, ascending or descending, sort by most viewed, etc.)

You can create replicas with the dashboard, or you can use the replicas setting when using the API. Whichever method you choose, these actions replicate all data and settings. You then need to change the sorting strategy of the replica.

Setting up the sort-by attributes

Attributes used for sorting should have only boolean or numerical values and can be sorted in ascending or descending order. You can’t use a string attribute. Dates must be represented as numbers.

Numerical values should be indexed as actual numbers:

    "review_count": 12 // do this ✅
    "review_count": "12" // not this ❌

Querying a replica

When you want to search in a replica index, you need to target the replica index when searching, as you would do with any primary index. That’s it.

Changing Settings of a replica

Concerning settings, a replica is like any other index: You can change any of its settings. This can be done for many reasons, one of the most common is when doing A/B testing.

Delete a replica’s primary index

You cannot edit the contents of a replica index. If you want to edit or delete the replica, you first have to unlink it from its primary index, which turns it into a regular index. Once done, changes to the primary index no longer propagate to the replica index.

Did you find this page helpful?