Ascending and descending sorting rules
Meilisearch supports two types of custom rules: one for ascending sort and one for descending sort. To add a custom ranking rule, you have to communicate the attribute name followed by a colon (:
) and either asc
for ascending order or desc
for descending order.
-
To apply an ascending sort (results sorted by increasing value of the attribute):
attribute_name:asc
-
To apply a descending sort (results sorted by decreasing value of the attribute):
attribute_name:desc
Example
Suppose you have a movie dataset. The documents contain the fieldsrelease_date
with a timestamp as value, and movie_ranking
, an integer that represents its ranking.
The following example creates a rule that makes older movies more relevant than recent ones. A movie released in 1999 will appear before a movie released in 2020.
Sorting at search time and custom ranking rules
Meilisearch allows users to define sorting order at query time by using thesort
search parameter. There is some overlap between sorting and custom ranking rules, but the two do have different uses.
In general, sort
will be most useful when you want to allow users to define what type of results they want to see first. A good use-case for sort
is creating a webshop interface where customers can sort products by descending or ascending product price.
Custom ranking rules, instead, are always active once configured and are useful when you want to promote certain types of results. A good use-case for custom ranking rules is ensuring discounted products in a webshop always feature among the top results.
Meilisearch does not offer native support for promoting, pinning, and boosting specific documents so they are displayed more prominently than other search results. Consult these Meilisearch blog articles for workarounds on implementing promoted search results with React InstantSearch and document boosting.