Concepts / Managing results / Sorting results
Sep. 26, 2019

Sorting Results

Although sorting technically happens at each level of the ranking formula, on this page we restrict the definition to the organization of records by a chosen attribute like release date or price.

Sorting by attribute

Sorting by attribute is equivalent to placing a custom ranking at the top of your ranking formula. You can choose to sort in ascending or descending direction.

Typical implementation will have your users choose whether or not to have their results sorted by attribute by toggling a sort-by button on the search page. You can decide which attributes can be sorted on and how to display the option.

Effect on ranking formula

When you sort by attribute, Algolia’s ranking formula is not disabled; instead, the tie-breaking relevance criteria comes after your selected attribute. For example, if you’ve sorted by price and 3 records cost $99, then all 3 will be further ordered by the ranking formula.

If you’ve created a sort-by attribute, results are ranked in the following way:

  1. Results are sorted by the values of the relevant sort-by attribute. The goal of attribute-based sorting is to ensure all records which match the query appear in the order determined by the sort-by attribute.
  2. Records with the same value in the sort-by attribute are ranked by Algolia’s tie-breaking algorithm (this includes custom ranking).

Sorting vs. Ranking

In Algolia, each of your indices has its own ranking formula which is composed of up to three parts (the first part, sorting, is optional).

Ranking formula

Records are first ordered by the value of their sorted attribute. If two records have the same value or belong to an index that is not sorted by attribute, then they are ordered according to Algolia’s default ranking criteria. If they still have the same value then they are ordered by custom ranking.

1. Attribute based sorting

Attribute based sorting comes before Algolia’s ranking criteria in determining the order of your records. Note, you should only add a sort-by attribute to replica indices.

2. Algolia’s default ranking criteria

The default ranking criteria are the first seven criteria of Algolia’s ranking criteria. They are concerned with the relevance of searches.

3. Custom ranking

Custom ranking is the last criterion in Algolia’s ranking criteria; you can use it to inject business metrics into your ranking formula.

Backend implementation (replicas)

To make our search fast, we sort your data at indexing time. Therefore, each of your indices can only be sorted in one way. To allow for multiple sort orders, Algolia uses replica indices.

A replica is a copy of one of your indices, that has the same data, synchronizes data updates, but can have unique settings. The index from which you copy a replica’s data is the replica’s primary index.

If you want to configure sort-by, you should read up on replicas.

For each attribute you want to sort by, you will need to create a replica index, which will contain a copy of all the data in your primary index. Making a replica will add a new index to your application with the same number of records as the primary.

Frontend implementation

Replica indices manage the backend of sorting, but you’ll need to implement the front end. You can do this with either custom logic or InstantSearch. If you plan to use InstantSearch, you should use the SortBy UI widget.

SortBy Widget

API Reference SortBy Widget

Did you find this page helpful?