02 Jul 2018

Sorting

Multiple Sorting Strategies for an Index

Every index 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 huge performance boost. Consequently, if you want to use different ranking formulas depending on the use case, you need to create one index per ranking formula.

Multiple ranking formulas are useful to implement different sorting strategies (e.g. sort by prices, ascending or descending, sort by most-viewed, …). 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

Replicas are used to create different rankings. Replicas allow you to perform write operations on a single, primary index and automatically execute the same operations on all of its 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.

Setting up your Replicas

Enabling multiple sorting strategies requires two steps:

  • Declare replicas on the primary index. This is done either on the Dashboard or using the replicas API method.
  • Update replica settings like sort strategies or custom ranking. This is done as you would for any index, using either the Dashboard or API.

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.
products.setSettings({
  replicas: [
    "products_by_price_asc",
    "products_by_popularity_desc"
  ]
})

// Another index that sorts by price, from lowest to highest.
products_by_price_asc.setSettings({
  ranking: [
    "asc(price)",
    "..."
  ]
});

// Another index sorts by popularity, from highest to lowest.
products_by_popularity_desc.setSettings({
  ranking: [
    "desc(popularity)",
    "..."
  ]
});

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) {
    console.log('success');
  }
});

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:

search.addWidget(
  instantsearch.widgets.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

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