Guides / Managing results / Must do

Custom Ranking

The main goal of search is to help users find what they are looking for. Search breaks down into two crucial steps: returning accurate results and ranking these results. This guide focuses on how Algolia natively ranks results and how you can impact it with custom ranking.

Algolia first orders results with its default ranking strategy, then with custom ranking. If you’re confused about custom ranking coming after default ranking, this is the case because Algolia’s default ranking criteria ensure that users find what they are looking for. Custom ranking helps you increase visibility for some of your most essential products, on top of Algolia’s default ranking strategy. It ensures that increasing visibility doesn’t prevent users from finding what they are looking for.

Custom ranking is a direct and powerful way to adapt Algolia to your business. When you classify records with business metrics such as popularity or rating, your best products appear higher in search results.

Algolia’s ranking strategy

Ranking is the second half of the relevance equation. Once the engine has found matching records, it needs to rank them, putting the best matches at the top.

The ranking criteria

Algolia uses a tie-breaking algorithm, which reuses much of the same textual matching criteria to determine the best matches.

A good example is typos and spelling differences. When searching for anything, you may accidentally swap letters, miss or add extra characters, or use a different spelling from the one in the index. For instance, you could type “iphoen” instead of “iphone”, or use the British spelling “theatre” while the index you search into uses “theater”. Thanks to typo tolerance, the engine returns exact matches and records with typos. Now, Algolia can reuse typo tolerance to rank records: all exact matches rank higher than records with typos. The tie-breaking algorithm privileges the best matches. If your index contained both “theater” and “theatre”, Algolia would return records for both, with the ones containing “theatre” first.

The ranking criteria are, in order:

There are eight in all, and they all play a role in ranking by breaking ties.

You can change the order of the default criteria, but we don’t recommend you do so. The out-of-the-box ranking order works for the vast majority of use cases.

How tie-breaking works

Most search engines use a coefficient-based approach and rank results based on a unique float value that is hard, if not impossible, to decipher.

Algolia has built its own approach, using a tie-breaking algorithm. Here’s how it works:

  • It orders all matching records according to the first criterion (number of typos, so exact matches rank first).
  • For all tied records, it orders them according to the second criterion (geolocation).
  • If there are still tied records (with the same geolocation), it orders them according to the third criterion (filters) and so on, until each record in the search results has a distinct ranking position.
  • If, after going through the first seven criteria, there are still tied records, Algolia uses each of your custom ranking attributes to break the tie.

Back to our “theatre” example. If two records have the same spelling, they will be tied. The algorithm needs to break the tie. The next criterion is geo. If the first record is geographically closer to the user than the second one, then the geo criterion breaks the tie, and the first record ranks higher than the second one. If, however, the movie theaters were within the same geographical range, the engine needs to go to the third applicable criterion. In our case, imagine that we also have a way to filter the search (and select features such as “3D”, “IMAX”, etc.) If we’ve selected “3D”, and only the second record has “3D” in its filters, then it breaks the tie, and the second record ranks before the first record one.

Custom ranking

Finding matching records using typos, geolocation, filters, and so on, is only part of what makes a compelling search experience. Algolia’s default ranking formula works well to handle this kind of record-matching relevance.

However, ordering on such properties alone undermines the value of thoughtfully ordering results based on custom metrics within each record. Custom ranking gives you direct control and is often the deciding factor on which records appear in the first set of results.

Here are some examples that add popularity to the ranking formula. When you type “spielberg films”, the custom ranking puts Spielberg’s most popular films at the top of the results. For a retail store, if you type “t-shirt”, then the most popular t-shirts appear at the top. For a blog website, if you type in “positive thinking”, the most popular articles on that subject appear at the top.

Typical custom ranking attributes include number of sales, views, likes, ratings, release date, etc. Any boolean or numeric attribute works with custom ranking.

This kind of control over ranking allows you to achieve many business goals: showcase your product line, encourage people to stay on your site and view as many products as possible, increase sales, and many more.

Defining custom ranking by leveraging business metrics is crucial for any successful search configuration.

Let’s take a look at a common use case, with the following dataset, and custom ranking on the popularity attribute (descending):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
  {
    "name": "iPhone 6 Plus",
    "popularity": 20
  },
  {
    "name": "iPhone 6",
    "popularity": 10
  },
  {
    "name": "iPhone 7",
    "popularity": 200
  }
]

With this configuration, if a user were to search for “iphone”, they would first get the iPhone 7, followed by the iPhone 6 Plus, and finally the iPhone 6.

You can decide whether you want the order to be descending (greater values first) or ascending (smaller values first).

Custom ranking precision

Tie-breaking only works if there are tied records to begin with. If a particular custom ranking metric is too precise, then the next custom metric may never come into play. For this reason, we stress the importance of reducing precision to allow for effective tie-breaking.

Imagine a custom ranking configuration for movies that sets both rating and views (in that order) as custom ranking attributes. If the rating across records is unnecessarily precise—say “4.321321”—the views may never be used to break the tie. However, the search results may not be more relevant. A movie with a slightly lower rating, but many more views would rank lower, while you would expect it to rank higher.

To fix the situation, we recommend creating another attribute (say truncated_rating) with values rounded to one decimal place. In this example, “4.321321” becomes “4.3”. By reducing the precision of the rating attribute, it makes it more likely to have several records with the same truncated_rating, allowing them to tie-break on the count attribute.

Did you find this page helpful?