Skip to content

open-southeners/laravel-scout-advanced-meilisearch

Repository files navigation

Laravel Scout Advanced Meilisearch required php version codecov Edit on VSCode online

Advanced Meilisearch integration with Laravel Scout.

Getting started

Install the package using Composer:

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

Filterable and sortable attributes

For sending filterable and sortable attributes to your Meilisearch server, configure your already searchable models like so:

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Tag extends Model
{
    use Searchable;

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

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

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

Using PHP attributes

In case your project is using PHP 8, you can do this by attributes on the model class or the toSearchableArray method:

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

#[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,
        ];
    }
}

And finally run the following artisan command:

php artisan scout:update "App\Models\User"

You could also run this command with --wait option which tells the command to wait for the task to finish:

php artisan scout:update "App\Models\User" --wait

Remember to check the official documentation about these filters and sorts.

Dumps

Create Meilisearch data dumps (data backups that will be saved on your Meilisearch server), with the following command:

php artisan scout:dump

As scout:update command, this also have a --wait option:

php artisan scout:dump --wait

Read more about Meilisearch dumps here.

Tasks

List all tasks via command line, just running the following:

php artisan scout:tasks

They can be even filtered! (see more options running it with --help)

php artisan scout:tasks --status=succeeded

Canceling enqueued tasks

Also can cancel tasks with a very simple command, you can either cancel an specific task or multiple:

php artisan scout:tasks-cancel 1

The previous command will cancel task with UID = 1. If you wish to cancel multiple you could send them separated by comma or using options like:

php artisan scout:tasks-cancel --before-enqueued=1d

So this will cancel all tasks that were enqueued before 1 day (can also send 1m, 1y... as in the background this is using Carbon::now()->add() & Carbon::now()->sub() methods)

Prune finished tasks

As canceling tasks won't make them disappear from the tasks history, you can just run the following:

php artisan scout:tasks-prune

Just for safety for debug purposes, this command does not remove those tasks that failed, if you wish to do so, run the command with --include-failed like so:

php artisan scout:tasks-prune --include-failed

Don't worry, this will not remove tasks that were enqueued and not finished, as stated by Meilisearch official docs (see link just below).

Read more about Meilisearch tasks here.

Partners

skore logo

License

This package is open-sourced software licensed under the MIT license.