Sorting Meilisearch Results with Laravel Scout and Eloquent

Dan Pastori avatar
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.

Prerequsites

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:

$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:

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

Next, we will have to run the following command:

$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:

$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:

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!

Support future content

The Ultimate Guide to Building APIs and Single-Page Applications with Laravel + VueJS + Capacitor book cover.

Psst... any earnings that we make off of our book is being reinvested to bringing you more content. If you like what you read, consider getting our book or get sweet perks by becoming a sponsor.

Gold Sponsor SnapShooter Backups

Written By Dan

Dan Pastori avatar Dan Pastori

Builder, creator, and maker. Dan Pastori is a Laravel certified developer with over 10 years experience in full stack development. When you aren't finding Dan exploring new techniques in programming, catch him at the beach or hiking in the National Parks.

Like this? Subscribe

We’re privacy advocates. We will never spam you and we only want to send you emails that you actually want to receive. One-click unsubscribes are instantly honored.

What topics are you interested in?
Are you interested in our upcoming book?