Sorting Meilisearch Results with Laravel Scout and Eloquent

Dan Pastori

April 4th, 2022

Meilisearch is an amazingly powerful full text search engine. In order to take full advantage of the power of Meilisearch and Laravel Scout, you'll have to get in the nitty gritty. One of those times is when you want to sort search results with once they've been returned from Meilisearch.

When making a query through Laravel Eloquent, this is easy. Simply just add an ->orderBy('column_name', 'direction') to the end of your query. However, with Laravel Scout and Meilisearch, there needs to be a few steps in configuration before you can order your results. Since Meilisearch is such a fine tuned system, everything has to be configured explicitly. Luckily, Laravel provides that power out of the box. We just have to configure everything correctly.

Prerequisites

I'm going to assume you have Meilisearch configured and connected to Laravel. This tutorial will focus solely on how to define sortable attributes on an index so you can use the full power of Meilisearch.

Step 1: Define Sortable Attributes

To sort by (order by) at search time with Meilisearch, you will need to define which keys are sortable within Meilisearch. Let's say we have an app that keeps track of customer data. We have a customers_index and each customer model has a first_name, last_name, and date_created field along with some other meta data. We want to allow the user to perform a full text search of the first_name, last_name and order customers newest to oldest (date_created , DESC).

We will have to update the sortable attributes. This can be done at any time after the index is created. If you have 0 records or a 1M records in your index, it doesn't matter. To update the sortable attributes on the customers_index you will have to either build a migration that connects to Meilisearch through PHP or connect through php artisan tinker. No matter how you directly connect to the Meilisearch instance, the methods performed will be the same.

First, we will need to create a client that interfaces with Meilisearch:

Creating a Meilisearch client instance

$client = new MeiliSearch\Client('https://url_to_meilisearch_instance:7700');

The MeiliSearch\\Client is installed as a dependency with the Laravel Scout package. Quick side note, as MeiliSearch grows, you might have to update your dependency manually. We've had to increase the version number on the package to support communication with newer versions of meilisearch.

The first parameter of the constructor is the URL to the MeiliSearch instance. Usually this lives on port 7700. Using Meilisearch in production, it's recommended to set a master key on your instance. To set this master key, you will have to pass that as the second variable to the constructor:

Creating a Meilisearch client with master key

$client = new MeiliSearch\Client('https://url_to_meilisearch_instance:7700', MASTER_KEY);

Next, we will have to run the following command:

Updating sortable attributes in Meilisearch

$client->index('customers_index')->updateSortableAttributes(['date_created'])

This is the magic piece right here. This command connects to the Meilisearch index we are looking for and calls the method updateSortableAttributes(). That method accepts an array of fields that can be used to sort the results directly from Meilisearch when called from Laravel.

Just a heads up, that if you are running this in production or on a system with a large amount of records, it might take awhile to run. A quick way to see if the sortable attributes have been updated and indexed is through php artisan tinker. You can call the following method on your client object:

Checking Meilisearch index status

$client->index('customers_index')->stats();

If the isIndexing => false then your update has completed.

In the next part we will show how order the results when running a search through Laravel Scout.

Step 2: Sorting From Laravel Scout to Meilisearch

Now that we have our sortable attribute defined on our customer, let's order by the attribute with an Eloquent query. To order directly through Meilisearch, construct a query like this:

Sorting search results with Laravel Scout

Artist::search( $term )
     ->orderBy('date_created', 'DESC')
     ->get();

With that query, all of your results will be returned, ordered, directly from Meilisearch!

Conclusion

Hope this helps a little with some of the implementation gotchas of Meilisearch! For more information, you can check out the official Meilisearch documentation on sortable attributes here. From there, you can see some of your options, how to view existing sortable attributes, etc.

If you have any questions, leave a comment on the community forum or reach out on Twitter!

Want to work together?

Professional developers choose Server Side Up to ship quality applications without surrendering control. Explore our tools and resources or work directly with us.

Join our community

We're a community of 3,000+ members help each other level up our development skills.

Platinum Sponsors

Active Discord Members

We help each other through the challenges and share our knowledge when we learn something cool.

Stars on GitHub

Our community is active and growing.

Newsletter Subscribers

We send periodic updates what we're learning and what new tools are available. No spam. No BS.

Sign up for our newsletter

Be the first to know about our latest releases and product updates.

    Privacy first. No spam. No sharing. Just updates.