Guides / Managing results / Refine results / Sorting results

Use relevant sorting to only show the most relevant results for a user’s search instead of displaying all the records (exhaustive sorting).

Exhaustive sorting Relevant sorting

Relevant sorting is only available on Premium plans.

Effect on ranking formula

Algolia’s ranking formula isn’t turned off when you sort by an attribute. Instead, the relevant sorting algorithm selects results as follows:

  1. Selects results matching the user’s query by using the primary index’s ranking formula.
  2. Selects only the most relevant results. This a threshold set by the relevancyStrictness setting. With ‘relevancyStrictness`, you instruct the engine to only return results with that relevancy score or better. The relevancy score for each result is computed at query time by comparing all the hits to the best hit of the search (a score of 100) and ranges from 100 to 0.
  3. Sorts the selected results based on the virtual replica’s customRanking sorting criteria.
  4. Returns the relevant results.

Relevancy strictness

If multiple results have the same value for a sorting attribute, tie-breaking proceeds as usual on the other defined sorting attributes. For example, suppose you’ve sorted by price and then by popularity. If a query returns three results that cost $99, the engine will then order them by their popularity score.

Relevant sorting considerations

Relevant sorting interacts with other features in ways you might not expect.

Caution: other re-ranking features can affect results

Other re-ranking features such as Rules apply after relevant sorting and will modify sort order. For example, if Personalization promotes expensive items for a particular user based on their past actions, that user might see expensive items at the top of the results, even if they’ve chosen to relevant sort from lowest to highest price.

To ensure predictable behavior, don’t use the following features or parameters with relevant sorting:

Facets count

When relevant sorting is enabled, the engine still computes facets on all results, not just those defined by the relevant sort. A UI banner detailing the behavior of the relevant sort may help manage the user’s expectations.

Enabling relevant sorting reduces the number of results

Essentially, that’s the point: it gives users more relevant results, not all results (exhaustive sorting).

For example, you search for “chromebook” on an ecommerce site and get Chromebook computers at the top of your results (so far, so good). However, if you then sort by ascending price (because you just want a cheap computer), the top (cheapest) listed items might be “Chromebook power adapters”. Whereas with a relevant sort, you would get fewer results but only see relevant items (computers).

Using a virtual replica even with relevancyStrictness set to 0 can’t mimic the exhaustive sorting of a standard replica because it doesn’t use the same algorithm. Be aware that a virtual replica:

  • Doesn’t duplicate the data. It sorts at query time.
  • Can only retrieve a maximum of 20k results, so there’s no guarantee that it will fetch the “deepest” hits.

How to configure relevant sorting

Step one - Create a virtual replica index

If you want to configure relevant sorting, you first need to create a virtual replica index for each attribute you want to sort by.

Virtual replicas provide an alternative “view” of the primary index. Virtual replicas reuse the primary’s ranking formula to return relevant results: for that reason, you can’t change the ranking on virtual replicas but can use customRanking to define the sort criteria.

Step two - Connect your UI to your index

Banner widget for relevant sorting

Virtual replica indices manage the back end of sorting, but you still need to implement the front end. You can do this with either custom logic or InstantSearch. If you’re using InstantSearch, you should use the sortBy UI widget.

sortBy widget

To ensure the user understands how relevant sort works and ensure that they can control their experience:

  • Indicate to the user that sorting removed some results
  • Offer them the option of displaying more.

You can do this with the relevantSort UI widget.

relevantSort widget

API Reference SortBy Widget
Did you find this page helpful?