Icon relevance white

Relevance Overview

Last updated 03 July 2017

Relevance Configuration

Algolia relevance is based off a tie-breaking algorithm - certain criterion, or “rules”, are used to sort and bucket results together, and break ties between equal matches. Basic relevance configuration consists of defining attributes (and associated settings) for textual rules to apply to, as well as the business metrics to be used as additional criterion.

Attributes that you want to search in, such as the name of a product, the brand, keywords, and description, create the textual relevancy, while key metrics like sales_count, popularity, and customer_rating help us define what is business relevant.

We’ll dive into creating textual and business relevance below, but for a deep dive into the Algolia tie-breaking algorithm and how these concepts work together, read through our Ranking Formula Guide.

Configuring Textual Relevance

Textual relevance is easily built by setting searchableAttributes (also referred to as AttributesToIndex) either in the Dashboard or via the API.

Searchable attributes are what you can control to influence three aspects of textual relevance within the engine:

  • Declare the attributes of your records that you want to make searchable
  • Order these attributes by importance
  • Declare if the order of the words inside the attribute matters or not

You’ll see that the attribute rule within the Ranking Formula is directly tied to these, and the searchable attributes defined in an index are unique to each dataset.

Define the attributes you want to be searchable

Consider the example of an index with the following records:

[
  {
    "name": "John Doe",
    "company": "Acme",
    "title": "Developer",
    "url": "http://www.acme.com",
    "previous_titles": ["Web Developer", "Intern"]
  },
  {
    "name": "Jane Dawson",
    "company": "John & Bill",
    "title": "Engineer",
    "url": "http://www.johnandbill.com",
    "previous_titles": ["Junior Engineer", "Student"]
  }
]

You might want to search into the name, company, title, and previoust_titles attributes, but not the url. In this case, you would need to set all these attributes in the setting searchableAttributes to make them searchable.

Order the searchable attributes by order of importance

What will happen if we type “John”? This word is present in both objects: the first record’s name is “John Doe” and the second record’s company is “John & Bill”. For the matching “John Doe” record to appear first, you will need to ensure the name attribute is above the company attribute in the list. The higher an attribute is in the list, the more important it will be. It’s possible to declare two or more attributes as the same level of importance by placing them in the same string delineated by a comma. In this example, company and title are considered to be the same importance.

Declaring the order of words

For each attribute we also have an additional setting: Ordered or Unordered. In Ordered, matching words at the beginning of a given attribute will be considered more important than words further in this attribute.

For instance, the object iPhone 7 will be ranked higher than Case for iPhone for the query “iphone”, because this word is in first position of the attribute (instead of the third position).

In most cases, it’s recommended to set an attribute as Unordered, especially either array-type attributes, or attributes longer than an average of 5 words.

Putting together all the requirements, here’s how to set the searchable attributes for this example:

index.setSettings({
  searchableAttributes: [
    'name',
    'title,company',
    'unordered(previous_titles)'
  ]
});

Configuring Business Relevance

In combination with what is textually relevant, what is business relevant is a very important factor for calculating relevancy.

Business metrics like most sold products, most favorited videos, or other numerical or boolean values that your data contains can be defined in the Algolia Ranking Formula to promote these more business relevant results to the top of a results set.

Custom Ranking

To communicate your business metrics to the engine, you can set them in the setting called customRanking. You can put any type of numerical or boolean value that represents the popularity/importance of your records.

It can be a raw value like the number of sales, views or likes. It can also be a computed value such as an overall popularity score or a computed user rating.

Let’s take an example:

  {
    "name": "iPhone 5",
    "units_sold": 20
  },
  {
    "name": "iPhone 6",
    "units_sold": 10
  },
  {
    "name": "iPhone 7",
    "units_sold": 200
  }

If we use the units_sold attribute in our Custom Ranking, and type the query “iPhone”, we’ll get the following results: iPhone 7 will be first, followed by iPhone 6 and iPhone 5.

You can decide whether you want the sort to be descending (bigger values appear first in the results) or ascending (smaller values appear first in the results).

Customranking units

Adjusting Precision

While which metrics you choose to include and their relative order are important, you have an additional lever for further influencing ranking: the precision (that is, granularity) of the numeric metrics.

For instance, you might want to include “recency” as a factor when ranking your products. If you were to add the date timestamp to your custom ranking, results will never tie because the number is so precise. This means that secondary metrics you might have included won’t have a chance to come into play. Instead, you can map the timestamp to a “recency boost” - for example, items released in the last day are boosted “1”, while those released in the past week are boosted “2”, and so on and so forth.

This principle applies across any numeric attributes. Similarly, you would want to map a pageviews attribute to a more reasonable scale. Oftentimes, applying a logarithm is the easiest way to achieve this effect.

Using Personalization

Algolia is amazing at building a search which delivers relevant results in an immersive experience. Once you’ve refined your Algolia implementation and are happy with the quality of the results, you can go a step beyond: personalizing the search results.

To learn more on how to use personalization and Algolia, check out our Personalization Guide.

What’s Next

Continue building your Algolia knowledge with these concepts:

© Algolia - Privacy Policy