The art of sorting

Learn how search-time sorting in Meilisearch lets users order results by price, date, or any sortable attribute, using one index instead of three.

Carolina Ferreira

Carolina Ferreira

Developer Advocate @ Meilisearch·@CarolainFG

··7 min read
The art of sorting

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:

json

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:

  • artWorks with the default built-in ranking rules
  • artWorksAsc with a custom ranking rule for ascending sort
  • artWorksDesc with a custom ranking rule for descending sort
javascript

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:

javascript

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:

javascript

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:

javascript

I just needed to transform it into this:

javascript

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.

javascript

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.

Carolina Ferreira

Carolina Ferreira

Developer Advocate @ Meilisearch

Carolina joined Meilisearch in 2020 as a Developer Advocate. With a background in translation and teaching, she discovered programming by chance and quickly became passionate about it. She has worked in DevRel and tech support and is now transitioning into a Solution Engineer role, enjoying the diverse challenges along the way. Outside of work, she loves staying active, music, cinema, traveling, and exploring new cuisines—one of her favorite parts of any trip.

Related articles