Concepts / Managing results / Sorting
Jan. 07, 2019

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, the next step is to apply business criteria, using your 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, also known as “sort-by”, refers to a more straightforward sorting which relies on a single attribute, like price or date. Attribute-based sorting ignores the relevance criteria, at first. The main concern is that all records which match the query appear in the order as determined by the sort-by attribute, regardless of the strength of the textual match.

When sorting by attribute - let’s say price - What do we do with items that have the same price? We use a tie-breaker. The tie-breaker, by default, is the objectID - whichever record has the highest objectID wins. Better alternatives include adding a second attribute (like date or most sold) or using the ranking strategy to break the tie.

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.

Replicas

By design, Algolia provides one ranking formula per index. It means that, when you want to provide different rankings for the same data, you need to use the replica indices.

Algolia provides this primary/replica partnership 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:

Data

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.

Settings

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:

1
2
3
4
5
6
7
8
[
  {
    "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

When you delete a replica’s primary index, the replica becomes a regular index, and it no longer is attached to another index. This means that you can access its data directly.

No query-time sorting

You create and maintain replicas at index time, not at query time. Every index, therefore, has a unique sorting strategy, which is statically defined at the index level. Pre-sorting at index time instead of at query time leads to a considerable performance boost.

Did you find this page helpful?