Extend Meilisearch integration capabilities on your Laravel Scout

Extend Meilisearch integration capabilities on your Laravel Scout

Data backups, filterable attributes, sortable attributes and many more

ยท

3 min read

Laravel Scout is the perfect full-text search implementation for your Laravel application, it allows to connect to multiple search engines: Collections (for local testing), database (for a much simple to maintain), Meilisearch or Algolia (for a much faster search, managed or not, being Meilisearch the only open source engine in the market).

Meilisearch let us the option to self-host our own infrastructure saving a lot of database queries on the full-text search part.

Problem though is that Laravel Scout does a basic integration with all the engines (remember that they recently added Meilisearch to the list of compatible engines), so we've added more features on top of it with our package.

Backing up your Meilisearch database

So for this Meilisearch already have a feature called Dumps, it allows us to copy all the search data to a portable way (a file), so we can upgrade our Meilisearch server with peace of mind.

But... how we do this from Laravel Scout? Easy, installing our package with the following:

composer require open-southeners/laravel-scout-advaced-meilisearch

Then use the following Artisan command:

php artisan scout:dump

And that's all, then you must wait until Meilisearch's task scheduler performs the task, but if you don't want to wait you could tell the command to wait and report proper result afterwards:

php artisan scout:dump --wait

And the result we get:

scout-meilisearch-article-1.jpg

Adding filterable attributes to Meilisearch

Scout just let us add searchable attributes, but what about filterable or sortable attributes? Well, this package also let us add filterable or sortable attributes, or even both at the same time.

So for this we need to add the following to our already searchable models:

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Scout\Searchable;
// Add this line
use OpenSoutheners\LaravelScoutAdvancedMeilisearch\Attributes\ScoutSearchableAttributes;

// And this one before the class definition, but could be also added before the "toSearchableArray" method
#[ScoutSearchableAttributes(filterable: ['email'], sortable: ['name'])]
class User extends Authenticatable
{
    use Searchable;

    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
        ];
    }
}

Isn't PHP 8 beautiful? Well... If you're not a user yet of PHP 8 we got you cover:

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Scout\Searchable;

class User extends Authenticatable
{
    use Searchable;

    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
        ];
    }

    /**
     * Get the search sortable attributes array for the model.
     *
     * @return array<string>
     */
    public function searchableFilters(): array
    {
        return ['email'];
    }

    /**
     * Get the search sortable attributes array for the model.
     *
     * @return array<string>
     */
    public function searchableSorts(): array
    {
        return ['name'];
    }
}

Add searchableFilters or searchableSorts methods returning an array of attribute names, of course depending on your needs you may need some filterable or sortable so we let you choose which ones (that's your piece of work ๐Ÿ˜‰).

And that's all from now, keep checking our repositories you might find something useful and don't forget to give this one a star to support our work.

Happy coding!

ย