Code Monkey home page Code Monkey logo

laravel-scout-advanced-meilisearch's Introduction

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.

laravel-scout-advanced-meilisearch's People

Contributors

d8vjork avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

laravel-scout-advanced-meilisearch's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.