Skip to content

Commit 649d5bb

Browse files
[9.x] Support Meilisearch index settings (#669)
* 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]>
1 parent 5926848 commit 649d5bb

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

config/scout.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@
132132
'meilisearch' => [
133133
'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
134134
'key' => env('MEILISEARCH_KEY', null),
135+
'index-settings' => [
136+
// 'users' => [
137+
// 'filterableAttributes'=> ['id', 'name', 'email'],
138+
// ],
139+
],
135140
],
136141

137142
];

src/Console/FlushCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FlushCommand extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'scout:flush {model}';
14+
protected $signature = 'scout:flush {model : Class name of the model to flush}';
1515

1616
/**
1717
* The console command description.

src/Console/IndexCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ public function handle(EngineManager $manager)
4141
$options = ['primaryKey' => $this->option('key')];
4242
}
4343

44-
$engine->createIndex($this->argument('name'), $options);
44+
$engine->createIndex($name = $this->argument('name'), $options);
45+
46+
if (method_exists($engine, 'updateIndexSettings')) {
47+
$driver = config('scout.driver');
48+
49+
if ($settings = config('scout.'.$driver.'.index-settings.'.$name, [])) {
50+
$engine->updateIndexSettings($name, $settings);
51+
}
52+
}
4553

4654
$this->info('Index ["'.$this->argument('name').'"] created successfully.');
4755
} catch (Exception $exception) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Laravel\Scout\Console;
4+
5+
use Exception;
6+
use Illuminate\Console\Command;
7+
use Laravel\Scout\EngineManager;
8+
9+
class SyncIndexSettingsCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'scout:sync-index-settings';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Sync your configured index settings with your search engine (MeiliSearch)';
24+
25+
/**
26+
* Execute the console command.
27+
*
28+
* @param \Laravel\Scout\EngineManager $manager
29+
* @return void
30+
*/
31+
public function handle(EngineManager $manager)
32+
{
33+
$engine = $manager->engine();
34+
35+
$driver = config('scout.driver');
36+
37+
if (! method_exists($engine, 'updateIndexSettings')) {
38+
return $this->error('The "'.$driver.'" engine does not support updating index settings.');
39+
}
40+
41+
try {
42+
$indexes = (array) config('scout.'.$driver.'.index-settings', []);
43+
44+
if (count($indexes)) {
45+
foreach ($indexes as $name => $settings) {
46+
$engine->updateIndexSettings($name, $settings);
47+
48+
$this->info('Settings for the ["'.$name.'"] index synced successfully.');
49+
}
50+
} else {
51+
$this->info('No index settings found for the "'.$driver.'" engine.');
52+
}
53+
} catch (Exception $exception) {
54+
$this->error($exception->getMessage());
55+
}
56+
}
57+
}

src/Engines/MeiliSearchEngine.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,20 @@ public function createIndex($name, array $options = [])
344344
return $this->meilisearch->createIndex($name, $options);
345345
}
346346

347+
/**
348+
* Update an index's settings.
349+
*
350+
* @param string $name
351+
* @param array $options
352+
* @return array
353+
*
354+
* @throws \MeiliSearch\Exceptions\ApiException
355+
*/
356+
public function updateIndexSettings($name, array $options = [])
357+
{
358+
return $this->meilisearch->index($name)->updateSettings($options);
359+
}
360+
347361
/**
348362
* Delete a search index.
349363
*

src/ScoutServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Laravel\Scout\Console\FlushCommand;
88
use Laravel\Scout\Console\ImportCommand;
99
use Laravel\Scout\Console\IndexCommand;
10+
use Laravel\Scout\Console\SyncIndexSettingsCommand;
1011
use MeiliSearch\Client as MeiliSearch;
1112

1213
class ScoutServiceProvider extends ServiceProvider
@@ -45,6 +46,7 @@ public function boot()
4546
FlushCommand::class,
4647
ImportCommand::class,
4748
IndexCommand::class,
49+
SyncIndexSettingsCommand::class,
4850
DeleteIndexCommand::class,
4951
]);
5052

0 commit comments

Comments
 (0)