04 Apr 2018


Multiple Sorting Strategies for an Index

Every index has a unique sorting strategy, which is statically defined at the index level. Handling pre-sorting at index time instead of at query time leads to a large performance boost.

To create multiple sorts, you’ll need to create multiple indexes, each with a different sort strategy. This is where replica indexes come in handy.

Algolia Replica Indexes

Setting up your Replicas

Manually maintaining several indexes with the same data would be inconvenient and error-prone. To avoid sending data multiple times to different indexes, Algolia provides a primary + replica feature: this allows you to automatically replicate the content of one index (the primary index) directly to other indexes (each called a replica index). Each replica index can have completely different settings — in the below examples, each replica index has different sort criteria.

Enabling multiple sort strategies requires two steps:

  • Declare replicas on the primary index
  • Update replica settings like sort strategies or custom ranking

Once the primary index and its replicas are configured, you only need to push new data to the primary index; changes are automatically propagated to the replicas. This can be configured from the Algolia dashboard, or via the API.

// "products" is our primary index.
  replicas: [

// Another index that sorts by price, from lowest to highest.
  ranking: [

// Another index sorts by popularity, from highest to lowest.
  ranking: [

Forwarding Primary Index Settings To Replicas

It’s very common to have several replica indexes that differ from their primary index in only one setting, such as sort strategy. Products in an e-commerce store are one example — a primary index is sorted by popularity, and multiple replicas are created for products sorted by ascending price, descending price, and descending rating.

The forwardToReplicas setting facilitates creation of these replicas. When used, the replicas inherit all settings of the primary index. From there, it’s easy to change just the settings that need to be different.

index.setSettings({'customRanking': ['desc(followers)']}, {forwardToReplicas: true}, function(err) {
  if (!err) {

Counting the number of records in a Replica

Replica indexes replicate the content of a primary index. They have the same content but can have their own configuration.

Because of this, if your primary has 1,000 records and you create 2 replicas, you’ll end up with 3,000 records indexed by Algolia.

Essentially, the total number of records (primary + replicas) = the number of records in your primary * the number of replicas on that primary index.

Basic Sorting

Types of Sortable Attributes

Attributes that need to be sortable should have only boolean or numerical values, and can be sorted ascending or descending.

Numerical values should be indexed as actual numbers:

//  do this
{ "review_count": 12 }

//  not this
{ "review_count": "12" }

Custom Ranking

The most common way to configure a sorting strategy is through custom rankings, which are applied as the final rule of the ranking formula.

Essentially, after textual relevance is considered, the engine then looks at an index’s custom ranking attributes to decide the order of records. These attributes are typically business or domain-specific and help determine relative priority of records.

Sort By Attribute

Another sort, considered a “hard sort,” is using a sort-by attribute. This kind of sort is applied before all textual ranking factors; textual relevance is decreased in favor of the specific sorting strategy in place. Consider the following products:

    "name": "Tickle Me Elmo",
    "price": 49.99
    "name": "Tick Removal Spray",
    "price": 9.99

For the query “tick”, “Tick Removal Spray,” which matches the query exactly, would ordinarily be ranked above “Tickle Me Elmo,” which only matches on a prefix.

However, if the sort strategy had been defined as price descending, “Tickle Me Elmo” would then be ranked first.

UX Patterns For Sorting

Dropdowns are a familiar pattern, common in e-commerce scenarios and power user workflows. Dropdowns are provided by Algolia’s Instantsearch.js via the sortBySelector:

    container: '#sort-by-container',
    indices: [
      {name: 'products', label: 'Most Relevant'},
      {name: 'products_by_price_asc', label: 'Lowest Price'},
      {name: 'products_by_popularity_desc', label: 'Most Popular'}

Note: indices=indexes


Tabs are a great option when there are only a few sorting options, those options are expected to yield mutually exclusive results, and users are expected to toggle frequently.

Try these tutorials

If you want to get started implementing a sort strategy in your UI, we have a few tutorials you might find helpful:

© Algolia - Privacy Policy