diff --git a/config/scout.php b/config/scout.php index 68218c2f..e8a3bdda 100644 --- a/config/scout.php +++ b/config/scout.php @@ -115,6 +115,12 @@ 'algolia' => [ 'id' => env('ALGOLIA_APP_ID', ''), 'secret' => env('ALGOLIA_SECRET', ''), + 'index-settings' => [ + // 'users' => [ + // 'searchableAttributes' => ['id', 'name', 'email'], + // 'attributesForFaceting'=> ['filterOnly(email)'], + // ], + ], ], /* diff --git a/src/Console/IndexCommand.php b/src/Console/IndexCommand.php index 7d0acb3b..1bc19635 100644 --- a/src/Console/IndexCommand.php +++ b/src/Console/IndexCommand.php @@ -6,6 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Str; +use Laravel\Scout\Contracts\UpdatesIndexSettings; use Laravel\Scout\EngineManager; use Symfony\Component\Console\Attribute\AsCommand; @@ -53,7 +54,7 @@ public function handle(EngineManager $manager) $engine->createIndex($name, $options); - if (method_exists($engine, 'updateIndexSettings')) { + if ($engine instanceof UpdatesIndexSettings) { $driver = config('scout.driver'); $class = isset($model) ? get_class($model) : null; @@ -65,7 +66,7 @@ public function handle(EngineManager $manager) if (isset($model) && config('scout.soft_delete', false) && in_array(SoftDeletes::class, class_uses_recursive($model))) { - $settings['filterableAttributes'][] = '__soft_deleted'; + $settings = $engine->configureSoftDeleteFilter($settings); } if ($settings) { diff --git a/src/Console/SyncIndexSettingsCommand.php b/src/Console/SyncIndexSettingsCommand.php index bd03d00e..ada4b8b1 100644 --- a/src/Console/SyncIndexSettingsCommand.php +++ b/src/Console/SyncIndexSettingsCommand.php @@ -6,6 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Str; +use Laravel\Scout\Contracts\UpdatesIndexSettings; use Laravel\Scout\EngineManager; use Symfony\Component\Console\Attribute\AsCommand; @@ -38,7 +39,7 @@ public function handle(EngineManager $manager) $driver = config('scout.driver'); - if (! method_exists($engine, 'updateIndexSettings')) { + if (! $engine instanceof UpdatesIndexSettings) { return $this->error('The "'.$driver.'" engine does not support updating index settings.'); } @@ -60,7 +61,7 @@ public function handle(EngineManager $manager) if (isset($model) && config('scout.soft_delete', false) && in_array(SoftDeletes::class, class_uses_recursive($model))) { - $settings['filterableAttributes'][] = '__soft_deleted'; + $settings = $engine->configureSoftDeleteFilter($settings); } $engine->updateIndexSettings($indexName = $this->indexName($name), $settings); diff --git a/src/Contracts/UpdatesIndexSettings.php b/src/Contracts/UpdatesIndexSettings.php new file mode 100644 index 00000000..a37de00b --- /dev/null +++ b/src/Contracts/UpdatesIndexSettings.php @@ -0,0 +1,20 @@ +search($builder->query, $options); } + + /** + * Update the index settings for the given index. + * + * @return void + */ + public function updateIndexSettings(string $name, array $settings = []) + { + $this->algolia->initIndex($name)->setSettings($settings); + } } diff --git a/src/Engines/Algolia4Engine.php b/src/Engines/Algolia4Engine.php index 17bbda1c..90a78cb6 100644 --- a/src/Engines/Algolia4Engine.php +++ b/src/Engines/Algolia4Engine.php @@ -162,4 +162,14 @@ protected function performSearch(Builder $builder, array $options = []) $queryParams ); } + + /** + * Update the index settings for the given index. + * + * @return void + */ + public function updateIndexSettings(string $name, array $settings = []) + { + $this->algolia->setSettings($name, $settings); + } } diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index 23d2f7cc..dcde78eb 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -6,11 +6,12 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\LazyCollection; use Laravel\Scout\Builder; +use Laravel\Scout\Contracts\UpdatesIndexSettings; /** * @template TAlgoliaClient of object */ -abstract class AlgoliaEngine extends Engine +abstract class AlgoliaEngine extends Engine implements UpdatesIndexSettings { /** * The Algolia client. @@ -82,6 +83,13 @@ abstract public function deleteIndex($name); */ abstract public function flush($model); + /** + * Update the index settings for the given index. + * + * @return void + */ + abstract public function updateIndexSettings(string $name, array $settings = []); + /** * Perform the given search on the engine. * @@ -245,6 +253,18 @@ public function createIndex($name, array $options = []) throw new Exception('Algolia indexes are created automatically upon adding objects.'); } + /** + * Configure the soft delete filter within the given settings. + * + * @return array + */ + public function configureSoftDeleteFilter(array $settings = []) + { + $settings['attributesForFaceting'][] = 'filterOnly(__soft_deleted)'; + + return $settings; + } + /** * Determine if the given model uses soft deletes. * diff --git a/src/Engines/MeilisearchEngine.php b/src/Engines/MeilisearchEngine.php index dcde66ab..7890a842 100644 --- a/src/Engines/MeilisearchEngine.php +++ b/src/Engines/MeilisearchEngine.php @@ -4,13 +4,14 @@ use Illuminate\Support\LazyCollection; use Laravel\Scout\Builder; +use Laravel\Scout\Contracts\UpdatesIndexSettings; use Laravel\Scout\Jobs\RemoveableScoutCollection; use Meilisearch\Client as MeilisearchClient; use Meilisearch\Contracts\IndexesQuery; use Meilisearch\Meilisearch; use Meilisearch\Search\SearchResult; -class MeilisearchEngine extends Engine +class MeilisearchEngine extends Engine implements UpdatesIndexSettings { /** * The Meilisearch client. @@ -383,17 +384,25 @@ public function createIndex($name, array $options = []) } /** - * Update an index's settings. + * Update the index settings for the given index. * - * @param string $name - * @param array $options - * @return array + * @return void + */ + public function updateIndexSettings($name, array $settings = []) + { + $this->meilisearch->index($name)->updateSettings($settings); + } + + /** + * Configure the soft delete filter within the given settings. * - * @throws \Meilisearch\Exceptions\ApiException + * @return array */ - public function updateIndexSettings($name, array $options = []) + public function configureSoftDeleteFilter(array $settings = []) { - return $this->meilisearch->index($name)->updateSettings($options); + $settings['filterableAttributes'][] = '__soft_deleted'; + + return $settings; } /**