Share the article
Meilisearch supports sorting at search time: your users decide how results are ordered, per query, without you maintaining a separate index for every sort order. This post walks through what that replaced and how to set it up.
Search-time sorting is when results are sorted according to parameters decided at query time. By default, Meilisearch orders results according to their relevancy, but you can configure it to let users decide at search time what results they want to see first.
Let me show you how powerful this feature is. If I wanted to search for a MacBook and sort the results by ascending price, I would send the following query:
I could do the opposite and search by descending price. I would just need to replace asc by desc in my request.
How sorting worked before search-time sorting
Before search-time sorting existed, the only way to order results by a numeric attribute was through custom ranking rules.
Thanks to the custom ranking rules, we could have our search results sorted by the numeric attribute of our choice. However, the sorting was not at search time, the sorting order was decided in our index settings.
To change the sort order of the search results in the user interface, we had to duplicate the index and set different custom ranking rules for each index.
That's what I did for our MoMA demo a few months ago. For those who missed it, the MoMA demo is a Meilisearch demo created with the artworks dataset of the Museum of Modern Art available in their GitHub repository. You can learn more about the demo in this blog post. I wanted users to be able to sort the artworks by date, so I had to create three indexes:
artWorkswith the default built-in ranking rulesartWorksAscwith a custom ranking rule for ascending sortartWorksDescwith a custom ranking rule for descending sort
The duplication, or triplication, gave the impression of sorting at search time because Meilisearch is fast enough to hide the index switch.
The snippet above is kept for historical context. Both getOrCreateIndex and the asc(attribute) ranking-rule syntax have since been removed from the JavaScript client and the engine.
At that time, sorting was still on the roadmap under consideration, and one of the most requested features.
We have a public roadmap where you can submit feature or integration ideas and vote for the existing ones.
How do you sort results at search time?
Declare the attribute in the index's sortableAttributes setting, then pass a sort array on each search request. One index then serves every sort order your interface offers.
Here is what that looked like when the demo was reworked.
Behind the scenes
First of all, I have added the following line to the settings:
This addition is necessary because we need to inform Meilisearch of the attributes we want to use for sorting.
We only need one index (not three) and the default ranking rules. This has considerably reduced the back-end code. You can change the position of the sort rule to tune relevancy. By default it sits fifth in the ranking rules, after words, typo, proximity, and attribute, so that textual relevance is settled before the sort is applied.
Meilisearch applies these rules as a bucket sort: each rule reorders the documents the previous rule considered equal. You can read more about ranking rules and relevancy.
And if you are curious about why the sort rule sits fifth by default, this GitHub issue explains the choice.
With these modifications, the big block of code above has been reduced to the following line:
Easy, right? But, what about the front end?
In the spotlight
For this demo I used Vue InstantSearch, combined with Instant Meilisearch. This connects the Meilisearch instance with the open-source InstantSearch front-end tools allowing us to customize the search environment effortlessly.
The previous code looked like this:
I just needed to transform it into this:
As you can see, instead of using 3 different indexes (artWorks, artWorksAsc, and artworksDesc), we only need to append the attribute's name to the artWorks index followed by the desired sort order, asc or desc.
And this is it. Smooth and simple. You can test it here.
The MoMA demo, sorting by ascending and then descending date for the query "Magritte".
The end-user may not notice the difference, but resource-wise it's much more efficient.
If you are still on an older version of Meilisearch, it is worth upgrading: indexing throughput has improved substantially across releases since this feature landed. On Meilisearch Cloud those upgrades are handled for you.
The demo source code is available on GitHub. I invite you to play with it, you can add other attributes to sort by, as long as they are strings or numeric values. For a more in-depth explanation of sorting, take a look at the dedicated section of the documentation.
Picture by Héctor J. Rivas on Unsplash
Frequently asked questions (FAQs)
How do I sort search results in Meilisearch?
Add the attribute to the index's sortableAttributes setting, then pass a sort array such as ["price:asc"] on the search request. Sorting is decided per query, so the same index can serve every sort order your interface offers.
Do I need a separate index for each sort order?
No. That was necessary before search-time sorting existed, when order was fixed by custom ranking rules in the index settings. Today one index serves every order, which cuts both storage and indexing time.
Which attributes can I sort by?
Any attribute you declare in sortableAttributes, as long as its values are numbers or strings. Declaring an attribute sortable adds work at indexing time, so declare only the ones you actually offer as sort options.
Where does sorting fit among the ranking rules?
sort sits fifth by default, after words, typo, proximity, and attribute, and before exactness. Meilisearch applies ranking rules as a bucket sort, so sort only reorders documents the earlier rules judged equally relevant. Move it earlier if you want the sort to dominate textual relevance.






