Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Indexes settings using config file

Clémentine Urquizar edited this page Feb 4, 2021 · 4 revisions

First create a command file:

php artisan make:command ScoutSync

The content of app/Console/Commands/ScoutSync.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Laravel\Scout\EngineManager;
use MeiliSearch\Client;

class ScoutSync extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'scout:sync';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sync your configuration with meilisearch';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        /** @var Client $client */
        $client = app(EngineManager::class)->driver('meilisearch');

        $settings = collect(config('meilisearch.settings'));
        $settings->each(function ($config, $class) use ($client) {
            $model = new $class;
            $index = $client->index($model->searchableAs());

            collect($config)->each(function ($value, $key) use ($index) {
                $status = $index->{$key}($value);
                $this->line("{$key} has been updated, updateId: {$status['updateId']}");
            });
        });
    }
}

Then you can add all your settings inside config/meilisearch.php

<?php

return [
    'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
    'key' => env('MEILISEARCH_KEY', null),

    'settings' => [
        \App\Models\Movie::class => [
            'updateAttributesForFaceting' => [
                'director',
                'genres'
            ],
            'updateSynonyms' => [
                'movie' => ['video'],
                'video' => ['movie'],
            ],
            'updateSearchableAttributes': ['director']
        ]
    ]
];

Here is the list of all available settings:

  • updateSynonyms
  • updateStopWords
  • updateAttributesForFaceting
  • updateRankingRules
  • updateDistinctAttribute
  • updateDisplayedAttributes
  • updateSearchableAttributes

More information on how to configure your settings Meilisearch Settings

Clone this wiki locally