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.
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
date_created field along with some other meta data. We want to allow the user to perform a full text search of the
last_name and order customers newest to oldest (
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');
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:
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:
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!
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.