Search responses are sorted according to a set of consecutive rules called ranking rules. When a search query is made, MeiliSearch uses a bucket sort to rank documents. Each rule is applied to all documents that are considered equal according to the previous rule to break the tie.
Ranking rules are built-in rules applied to the search results in order to improve their relevancy. To benefit from the ranking rules and make them meet your dataset and needs, it is important to understand how each of them works and how to create new ones.
For a more in-depth explanation of the algorithm and the default ranking rules, see this issue.
# Ranking rules
Ranking rules determine which documents are returned upon a search query. Each of them has a special use in finding the right results for a given search query.
The ranking rules are customizable which means existing rules can be deleted and new ones can be added.
The order in which they are applied has a significant impact on the search results. The first rules being the most impactful and the last one the least. The default order has been chosen because it meet most standard needs. This order can be changed in the settings.
By default, ranking rules are executed in the following order:
Results are sorted by increasing number of typos: find documents that match query terms with fewer typos first.
Results are sorted by decreasing number of matched query terms in each matching document: find documents that contain more occurrences of the query terms first.
It is now mandatory that all query terms are present in the returned documents. This rule does not impact search results yet. soon
Results are sorted by increasing distance between matched query terms: find documents that contain more query terms found close together (close proximity between two query terms) and appearing in the original order specified in the query string first.
Results are sorted according to the order of importance of the attributes: find documents that contain query terms in more important attributes first.
5. Words Position
Results are sorted by the position of the query words in the attributes: find documents that contain query terms earlier in their attributes first.
Results are sorted by the similarity of the matched words with the query words: find documents that contain exactly the same terms as the ones queried first.
# Order of the rules
By default, the built-in rules are executed in the following order to meet most standard needs.
["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 settings route of your index.
# Adding your rules
New rules can be added to the existing list at any time and placed anywhere in the sequence.
A custom rule allows you to create an ascending or descending sorting rule on a given attribute. The attribute must have a numeric value in the documents. If any value is not a numeric type, the sorting rule won't be applied. Only numbers can be arranged in ascending or descending order.
To add your own ranking rule, you have to communicate either
asc for ascending order or
desc for descending order followed by the field name between round brackets.
To apply an ascending sorting (results sorted by increasing value of the attribute):
To apply a descending sorting (results sorted by decreasing value of the attribute):
Add this rule to the existing list of ranking rules using the settings route.
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)" ]
# Importance of the attributes
In a dataset, some fields are more relevant to the search than others. A
title, for example, has a value more meaningful to a movie search than its
description or its
By default, the order of importance of the attributes is based on their order of appearance in the first document added. Then, each new attribute found in new documents will be added at the end of this ordered list.
If you wish to specify the order of the attributes you can either define them in the settings or set the correct order in the first document indexed.
# Changing the order of the attributes
You may want to change the order once the documents have been ingested. This is still very possible using the searchable attributes list.
Whenever a document is added to MeiliSearch, all new attributes found in it are automatically added to two lists:
- The searchable attributes list: Attributes of the fields in which to search for matching query words.
- The displayed attributes list: Attributes of the fields displayed in documents.
This searchable attributes list is ordered, which means the order in which the attributes appear in the list determines their relevancy. Attributes are arranged from the most important attribute to the least important attribute.
Place the attributes in the desired order and send this updated list using the settings routes. Attributes will be re-ordered.
["title", "description", "director"]
If you take a look at the above order, the matching words found in
title will make the document more relevant than one with the same matching words found in