Relevancy is a term referring to the accuracy and effectiveness of search results. If search results are almost always appropriate, then they can be considered relevant, and vice versa.
MeiliSearch has a number of features for fine-tuning the relevancy of search results. The most important tool among them is ranking rules.
# Ranking rules
In order to ensure relevant results, search responses are sorted according to a set of consecutive rules called ranking rules.
Each index possesses a list of ranking rules stored as an array in the settings object. This array is fully customizable, meaning that existing rules can be deleted, new ones can be added, and all can be reordered freely.
Whenever a search query is made, MeiliSearch uses a bucket sort to rank documents. The first ranking rule is applied to all documents, while each subsequent rule is only applied to documents that are considered equal under the previous rule (i.e. as a tiebreaker).
The order in which ranking rules are applied matters. The first rule in the array has the most impact, and the last rule has the least. Our default configuration has been chosen because it meets most standard needs. This order can be changed in the settings.
# Built-in rules
MeiliSearch contains six built-in ranking rules: typo, words, proximity, attribute, words position, and exactness, in that default order.
Results are sorted by increasing number of typos. Returns documents that match query terms with fewer typos first.
Results are sorted by decreasing number of matched query terms. Returns documents that contain all query terms first.
For now, it is mandatory that all query terms are present in returned documents. Therefore, this rule does not impact search results yet. soon
Results are sorted by increasing distance between matched query terms. Returns documents where query terms occur close together and in the same order as the query string first.
Results are sorted according to the attribute ranking order. Returns documents that contain query terms in more important attributes first.
5. Words position
Results are sorted by the location of the query word in the field. Returns documents that contain query terms close to the beginning of the field first.
Results are sorted by the similarity of the matched words with the query words. Returns documents containing terms that are more similar to the query terms first.
# Custom rules
For now, MeiliSearch supports two custom rules that can be added to the ranking rules array: one for ascending sort and one for descending sort.
To add a custom ranking rule, you have to communicate either
asc for ascending order or
desc for descending order followed by the field name in parentheses.
To apply an ascending sort (results sorted by increasing value of the attribute):
To apply a descending sort (results sorted by decreasing value of the attribute):
The attribute must have a numeric value in all of the documents contained in that index. If any value is not a numeric type, the sorting rule won't be applied.
Add this rule to the existing list of ranking rules using the update ranking rules endpoint.
Let's say you have a movie dataset. The documents contain the fields
release_date with a timestamp as value, and
movie_ranking an integer that represents its ranking.
The following example will create a rule that makes older movies more relevant than more recent ones. A movie released in 1999 will appear before a movie released in 2020.
The following example will create a rule that makes movies with a good rank more relevant than movies with a lower rank. Movies with a higher ranking will appear first.
To add a rule to the existing ranking rule, you have to add the rule to the existing ordered rules array using the settings route,
[ "typo", "attribute", "proximity", "words", "wordsPosition", "exactness", "asc(release_date)", "desc(movie_ranking)" ]
# Default order
By default, the built-in rules are executed in the following order.
["typo", "words", "proximity", "attribute", "wordsPosition", "exactness"]
Depending on your needs, you might want to change this order of importance. To do so, you can use the update ranking rules endpoint.
# Attribute ranking order
In a typical dataset, some fields are more relevant to search than others. A
title, for example, has a value more meaningful to a movie search than its
description or its
By default, the attribute ranking order is generated automatically based on the attributes' order of appearance in the indexed documents. However, it can also be set manually.
For a more detailed look at this subject, see our reference page for the searchable attributes list.
["title", "description", "release_date"]
With the above attribute ranking order, matching words found in the
title field would have a higher impact on relevancy than the same words found in
release_date. If you searched "1984", for example, results like Michael Radford's film "1984" would be ranked higher than movies released in the year 1984.