From 55b4bbb39cfbb262b0c1e1c1d38964740c65f974 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Fri, 6 Jan 2023 00:54:56 +0100 Subject: [PATCH] fix: fix meilisearch indexes import (monicahq/chandler#378) --- app/Console/Commands/SetupDummyAccount.php | 2 +- app/Console/Commands/SetupScout.php | 51 +++++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/app/Console/Commands/SetupDummyAccount.php b/app/Console/Commands/SetupDummyAccount.php index 92323e4b004..e87ddaddd7c 100644 --- a/app/Console/Commands/SetupDummyAccount.php +++ b/app/Console/Commands/SetupDummyAccount.php @@ -91,7 +91,7 @@ private function wipeAndMigrateDB(): void } else { $this->artisan('☐ Migration of the database', 'migrate:fresh', ['--force' => true]); } - $this->artisan('☐ Reset search engine', 'scout:setup', ['--force' => true]); + $this->artisan('☐ Reset search engine', 'scout:setup', ['--force' => true, '--flush' => true]); } private function stop(): void diff --git a/app/Console/Commands/SetupScout.php b/app/Console/Commands/SetupScout.php index 4b4b46fe9fd..886201fef6c 100644 --- a/app/Console/Commands/SetupScout.php +++ b/app/Console/Commands/SetupScout.php @@ -2,9 +2,6 @@ namespace App\Console\Commands; -use App\Models\Contact; -use App\Models\Group; -use App\Models\Note; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Symfony\Component\Console\Output\OutputInterface; @@ -22,6 +19,8 @@ class SetupScout extends Command * @var string */ protected $signature = 'scout:setup + {--flush : Flush the indexes.} + {--import : Import the models into the search index.} {--force : Force the operation to run when in production.}'; /** @@ -37,7 +36,9 @@ class SetupScout extends Command public function handle(): void { if ($this->confirmToProceed()) { - $this->scout(); + $this->scoutConfigure(); + $this->scoutFlush(); + $this->scoutImport(); } } @@ -46,19 +47,45 @@ public function handle(): void * * @return void */ - protected function scout(): void + protected function scoutConfigure(): void { - if (config('scout.driver') !== null) { - $this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Note::class, '--verbose' => true]); - $this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Contact::class, '--verbose' => true]); - $this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Group::class, '--verbose' => true]); + if (config('scout.driver') === 'meilisearch' && (config('scout.meilisearch.host')) !== '') { + $this->artisan('☐ Updating indexes on Meilisearch', 'scout:sync-index-settings', ['--verbose' => true]); } + } - if (config('scout.driver') === 'meilisearch' && (config('scout.meilisearch.host')) !== '') { - $this->artisan('☐ Creating indexes on Meilisearch', 'scout:sync-index-settings', ['--verbose' => true]); + /** + * Import models. + * + * @return void + */ + protected function scoutFlush(): void + { + if (config('scout.driver') !== null && $this->option('flush')) { + foreach (config('scout.meilisearch.index-settings') as $index => $settings) { + $name = (new $index)->getTable(); + $this->artisan("☐ Flush {$name} index", 'scout:flush', ['model' => $index, '--verbose' => true]); + } + + $this->info('✓ Indexes flushed'); } + } + + /** + * Import models. + * + * @return void + */ + protected function scoutImport(): void + { + if (config('scout.driver') !== null && $this->option('import')) { + foreach (config('scout.meilisearch.index-settings') as $index => $settings) { + $name = (new $index)->getTable(); + $this->artisan("☐ Import {$name}", 'scout:import', ['model' => $index, '--verbose' => true]); + } - $this->info('✓ Indexes created'); + $this->info('✓ Indexes imported'); + } } private function artisan(string $message, string $command, array $options = [])