diff --git a/config/scout.php b/config/scout.php index 5c8b7d20..33035e1b 100644 --- a/config/scout.php +++ b/config/scout.php @@ -132,6 +132,11 @@ 'meilisearch' => [ 'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'), 'key' => env('MEILISEARCH_KEY', null), + 'index-settings' => [ + // 'users' => [ + // 'filterableAttributes'=> ['id', 'name', 'email'], + // ], + ], ], ]; diff --git a/src/Console/FlushCommand.php b/src/Console/FlushCommand.php index 1a1b5019..edcfc8f5 100644 --- a/src/Console/FlushCommand.php +++ b/src/Console/FlushCommand.php @@ -11,7 +11,7 @@ class FlushCommand extends Command * * @var string */ - protected $signature = 'scout:flush {model}'; + protected $signature = 'scout:flush {model : Class name of the model to flush}'; /** * The console command description. diff --git a/src/Console/IndexCommand.php b/src/Console/IndexCommand.php index 7537f99a..1dc595a0 100644 --- a/src/Console/IndexCommand.php +++ b/src/Console/IndexCommand.php @@ -41,7 +41,15 @@ public function handle(EngineManager $manager) $options = ['primaryKey' => $this->option('key')]; } - $engine->createIndex($this->argument('name'), $options); + $engine->createIndex($name = $this->argument('name'), $options); + + if (method_exists($engine, 'updateIndexSettings')) { + $driver = config('scout.driver'); + + if ($settings = config('scout.'.$driver.'.index-settings.'.$name, [])) { + $engine->updateIndexSettings($name, $settings); + } + } $this->info('Index ["'.$this->argument('name').'"] created successfully.'); } catch (Exception $exception) { diff --git a/src/Console/SyncIndexSettingsCommand.php b/src/Console/SyncIndexSettingsCommand.php new file mode 100644 index 00000000..cd2fcc0f --- /dev/null +++ b/src/Console/SyncIndexSettingsCommand.php @@ -0,0 +1,57 @@ +engine(); + + $driver = config('scout.driver'); + + if (! method_exists($engine, 'updateIndexSettings')) { + return $this->error('The "'.$driver.'" engine does not support updating index settings.'); + } + + try { + $indexes = (array) config('scout.'.$driver.'.index-settings', []); + + if (count($indexes)) { + foreach ($indexes as $name => $settings) { + $engine->updateIndexSettings($name, $settings); + + $this->info('Settings for the ["'.$name.'"] index synced successfully.'); + } + } else { + $this->info('No index settings found for the "'.$driver.'" engine.'); + } + } catch (Exception $exception) { + $this->error($exception->getMessage()); + } + } +} diff --git a/src/Engines/MeiliSearchEngine.php b/src/Engines/MeiliSearchEngine.php index 896fd478..1cfe1c9e 100644 --- a/src/Engines/MeiliSearchEngine.php +++ b/src/Engines/MeiliSearchEngine.php @@ -344,6 +344,20 @@ public function createIndex($name, array $options = []) return $this->meilisearch->createIndex($name, $options); } + /** + * Update an index's settings. + * + * @param string $name + * @param array $options + * @return array + * + * @throws \MeiliSearch\Exceptions\ApiException + */ + public function updateIndexSettings($name, array $options = []) + { + return $this->meilisearch->index($name)->updateSettings($options); + } + /** * Delete a search index. * diff --git a/src/ScoutServiceProvider.php b/src/ScoutServiceProvider.php index bada5a25..051bd177 100644 --- a/src/ScoutServiceProvider.php +++ b/src/ScoutServiceProvider.php @@ -7,6 +7,7 @@ use Laravel\Scout\Console\FlushCommand; use Laravel\Scout\Console\ImportCommand; use Laravel\Scout\Console\IndexCommand; +use Laravel\Scout\Console\SyncIndexSettingsCommand; use MeiliSearch\Client as MeiliSearch; class ScoutServiceProvider extends ServiceProvider @@ -45,6 +46,7 @@ public function boot() FlushCommand::class, ImportCommand::class, IndexCommand::class, + SyncIndexSettingsCommand::class, DeleteIndexCommand::class, ]);