Icon ranking white

Sorting

Last updated 13 October 2017

Multiple Sorting Strategies for an Index

Every index has a unique sorting strategy, which is statically defined at the index level. Handling this 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 indices, each with a different sort strategy. This is where replica indices come in handy.

Algolia Replica Indices

Manually maintaining several indices with the same data would be very inconvenient and error-prone. To avoid sending data multiple times to different indices, Algolia provides a primary + replica feature: this allows you to automatically replicate the content of one index (the primary index) directly to other indices (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.
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 indices 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');
  }
});

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" }

Basic Sorting

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 were 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'}
    ]
  })
);

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.

Tabs are not provided by Algolia’s Instantsearch.js; use of the helper library is required.

What’s Next

Continue building your Algolia knowledge with these concepts:

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