Skip to content

Commit

Permalink
[9.x] Support Meilisearch index settings (#669)
Browse files Browse the repository at this point in the history
* Update comment for flush command

* Sync index settings for Meilisearch

* wip

* wip

* Update IndexCommand.php

* wip

* wip

* Update FlushCommand.php

* Update FlushCommand.php

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
driesvints and taylorotwell authored Nov 29, 2022
1 parent 5926848 commit 649d5bb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
5 changes: 5 additions & 0 deletions config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@
'meilisearch' => [
'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
'key' => env('MEILISEARCH_KEY', null),
'index-settings' => [
// 'users' => [
// 'filterableAttributes'=> ['id', 'name', 'email'],
// ],
],
],

];
2 changes: 1 addition & 1 deletion src/Console/FlushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion src/Console/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
57 changes: 57 additions & 0 deletions src/Console/SyncIndexSettingsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Laravel\Scout\Console;

use Exception;
use Illuminate\Console\Command;
use Laravel\Scout\EngineManager;

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

/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync your configured index settings with your search engine (MeiliSearch)';

/**
* Execute the console command.
*
* @param \Laravel\Scout\EngineManager $manager
* @return void
*/
public function handle(EngineManager $manager)
{
$engine = $manager->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());
}
}
}
14 changes: 14 additions & 0 deletions src/Engines/MeiliSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/ScoutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,6 +46,7 @@ public function boot()
FlushCommand::class,
ImportCommand::class,
IndexCommand::class,
SyncIndexSettingsCommand::class,
DeleteIndexCommand::class,
]);

Expand Down

0 comments on commit 649d5bb

Please sign in to comment.