Isak Berglind

Isak Berglind

Sort results in meilisearch using laravel scout

Isak Berglind • February 23, 2022

php meilisearch scout laravel tips

I tried meilisearch for the first time the other day. It worked great and it was a breeze getting it up 'n' running following the instructions in the laravel documentation

I had some trouble, however, figuring out how to sort the results from meilisearch.

To do this, you first have to tell meilisearch which index and which fields you want to sort. Say our index is called "users" and the fields we want to sort is "name" and "occupation" Do that using this curl request:

$ curl -X POST 'http://[ip]:7700/indexes/users/settings/sortable-attributes' \
-H 'Content-Type: application/json' \
--data-binary '[ "name", "occupation" ]' \
-H 'Authorization: Bearer [token]'

You should get a response similar to this:


Now, sort your results using scout:

$users = User::search("Isak Berglind", function ($meilisearch, $query, $options) {
    $options['sort'] = ['name:asc'];

    return $meilisearch->search($query, $options);

Thats it!

Did you like this article? Then please let me know on twitter