Skip to content

esadewater/laravel-meilisearch

Repository files navigation

Package to conveniently handle search indices for MeiliSearch.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Package to conveniently handle search index settings like sortable and filterable attributes for MeiliSearch through Laravel Scout.

Prerequisites

You need to have Laravel Scout installed and configured. If you haven't done yet, you can skip the "Configuring Filterable Data & Index Settings (Meilisearch)" -part while setting everything up.

Installation

You can install the package via composer:

composer require esadewater/laravel-meilisearch

Usage

Instead of the Searchable trait of Laravel Scout, you need to use the IsMeiliSearchable trait and in addition implement the MeiliSearchable interface. Replace the Searchable trait with the IsMeiliSearchable trait in your models. Your models should look like this:

class Food extends Model implements MeiliSearchable
{
    use IsMeiliSearchable;
    
    /**
     * @var string[] Mass-assignable attributes
     */
    protected $fillable = [
        'name',
    ];
    
    /**
     * Get attributes used for search
     */
    public function toSearchableArray(): array
    {
        return [
            'name' => $this->name,
        ];
    }
}

To make the handling of the index settings easier than with Laravel Scout, each model contains its own index settings for searchable, sortable and filterable attributes:

class Recipe extends Model implements MeiliSearchable
{
    use IsMeiliSearchable;
    
    /**
     * Define the searchable attributes for the model
     */
    public static function getSearchableAttributes(): array
    {
        return [
            'name',
            'difficulty',
            'ingredient_names',
        ];
    }

    /**
     * Define the search sortable attributes for the model
     */
    public static function getSortableAttributes(): array
    {
        return [
            'name',
            'difficulty',
        ];
    }

    /**
     * Define the search filter attributes for the model
     */
    public static function getFilterableAttributes(): array
    {
        return [
            'difficulty',
        ];
    }
}

By default, all attributes are searchable. If you want to omit some attributes from the search, you have to define all remaining attributes in the getSearchableAttributes() method.

Indexing

To create the search indices for all models, setup their index settings and import all existing models into the index, you can use the meili:setup command:

php artisan meili:setup

If you want to execute one of the steps separately for one model, you can use the following commands:

To create the search index for one model, you can use the meili:create {model} command:

php artisan meili:create App\\Models\\Food

To sync the index setting, like searchable, sortable and filterable attributes, you can use the meili:sync-settings {model} command:

php artisan meili:sync-settings App\Models\Food

To import models into the search index, you can use the meili:import {model} command:

php artisan meili:import App\Models\Food

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •