From f598ae822f94b0cf46cce8b4a6bd866452aa9a8d Mon Sep 17 00:00:00 2001 From: Joost de Bruijn Date: Sun, 1 Dec 2024 15:14:17 +0000 Subject: [PATCH 1/4] feat: Algolia settings sync --- src/Console/IndexCommand.php | 5 +++-- src/Console/SyncIndexSettingsCommand.php | 5 +++-- src/Contracts/UpdatesIndexSettings.php | 10 ++++++++++ src/Engines/Algolia3Engine.php | 12 ++++++++++++ src/Engines/Algolia4Engine.php | 12 ++++++++++++ src/Engines/AlgoliaEngine.php | 25 +++++++++++++++++++++++- src/Engines/MeilisearchEngine.php | 16 ++++++++++++++- 7 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/Contracts/UpdatesIndexSettings.php 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..80d128b4 --- /dev/null +++ b/src/Contracts/UpdatesIndexSettings.php @@ -0,0 +1,10 @@ +clearObjects(); } + /** + * Create a search index. + * + * @return mixed + * + * @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException + */ + public function updateIndexSettings(string $name, array $options = []) + { + return $this->algolia->initIndex($name)->setSettings($options); + } + /** * Perform the given search on the engine. * diff --git a/src/Engines/Algolia4Engine.php b/src/Engines/Algolia4Engine.php index 2327e224..d2b1d7df 100644 --- a/src/Engines/Algolia4Engine.php +++ b/src/Engines/Algolia4Engine.php @@ -135,6 +135,18 @@ public function flush($model) $this->algolia->clearObjects($model->indexableAs()); } + /** + * Create a search index. + * + * @return mixed + * + * @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException + */ + public function updateIndexSettings(string $name, array $options = []) + { + return $this->algolia->setSettings($name, $options); + } + /** * Perform the given search on the engine. * diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index 23d2f7cc..2edbdc85 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,28 @@ abstract public function deleteIndex($name); */ abstract public function flush($model); + /** + * Create a search index. + * + * @return mixed + * + * @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException + */ + abstract public function updateIndexSettings(string $name, array $options = []); + + /** + * Configures the soft delete filter on the index settings. + * + * @param array $settings + * @return array + */ + public function configureSoftDeleteFilter(array $settings = []): array + { + $settings['attributesForFaceting'][] = 'filterOnly(__soft_deleted)'; + + return $settings; + } + /** * Perform the given search on the engine. * diff --git a/src/Engines/MeilisearchEngine.php b/src/Engines/MeilisearchEngine.php index dcde66ab..a4256d6e 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. @@ -396,6 +397,19 @@ public function updateIndexSettings($name, array $options = []) return $this->meilisearch->index($name)->updateSettings($options); } + /** + * Configures the soft delete filter on the index settings. + * + * @param array $settings + * @return array + */ + public function configureSoftDeleteFilter(array $settings = []): array + { + $settings['filterableAttributes'][] = '__soft_deleted'; + + return $settings; + } + /** * Delete a search index. * From 3b45b4707191315c87a93ef0f72ea25bc6752cc8 Mon Sep 17 00:00:00 2001 From: Joost de Bruijn Date: Sun, 1 Dec 2024 15:21:24 +0000 Subject: [PATCH 2/4] style: fix styleci issues --- src/Contracts/UpdatesIndexSettings.php | 2 +- src/Engines/AlgoliaEngine.php | 2 +- src/Engines/MeilisearchEngine.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Contracts/UpdatesIndexSettings.php b/src/Contracts/UpdatesIndexSettings.php index 80d128b4..425e212f 100644 --- a/src/Contracts/UpdatesIndexSettings.php +++ b/src/Contracts/UpdatesIndexSettings.php @@ -7,4 +7,4 @@ interface UpdatesIndexSettings public function updateIndexSettings(string $name, array $options = []); public function configureSoftDeleteFilter(array $settings = []): array; -} \ No newline at end of file +} diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index 2edbdc85..2df43f7c 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -95,7 +95,7 @@ abstract public function updateIndexSettings(string $name, array $options = []); /** * Configures the soft delete filter on the index settings. * - * @param array $settings + * @param array $settings * @return array */ public function configureSoftDeleteFilter(array $settings = []): array diff --git a/src/Engines/MeilisearchEngine.php b/src/Engines/MeilisearchEngine.php index a4256d6e..bd4903f1 100644 --- a/src/Engines/MeilisearchEngine.php +++ b/src/Engines/MeilisearchEngine.php @@ -400,7 +400,7 @@ public function updateIndexSettings($name, array $options = []) /** * Configures the soft delete filter on the index settings. * - * @param array $settings + * @param array $settings * @return array */ public function configureSoftDeleteFilter(array $settings = []): array From 40894752599a91f873214bcfdf5ca4b02c1a7a61 Mon Sep 17 00:00:00 2001 From: Joost de Bruijn Date: Sun, 1 Dec 2024 15:42:41 +0000 Subject: [PATCH 3/4] fix: add index-settings key to algolia config --- config/scout.php | 6 ++++++ 1 file changed, 6 insertions(+) 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)'], + // ], + ], ], /* From 0d5de5fdf4f04fe5f77c54d1c640c185992f7f85 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Dec 2024 15:41:31 -0600 Subject: [PATCH 4/4] formatting --- src/Contracts/UpdatesIndexSettings.php | 14 +++++++++-- src/Engines/Algolia3Engine.php | 22 ++++++++--------- src/Engines/Algolia4Engine.php | 22 ++++++++--------- src/Engines/AlgoliaEngine.php | 33 ++++++++++++-------------- src/Engines/MeilisearchEngine.php | 17 +++++-------- 5 files changed, 53 insertions(+), 55 deletions(-) diff --git a/src/Contracts/UpdatesIndexSettings.php b/src/Contracts/UpdatesIndexSettings.php index 425e212f..a37de00b 100644 --- a/src/Contracts/UpdatesIndexSettings.php +++ b/src/Contracts/UpdatesIndexSettings.php @@ -4,7 +4,17 @@ interface UpdatesIndexSettings { - public function updateIndexSettings(string $name, array $options = []); + /** + * Update the index settings for the given index. + * + * @return void + */ + public function updateIndexSettings(string $name, array $settings = []); - public function configureSoftDeleteFilter(array $settings = []): array; + /** + * Configure the soft delete filter within the given settings. + * + * @return array + */ + public function configureSoftDeleteFilter(array $settings = []); } diff --git a/src/Engines/Algolia3Engine.php b/src/Engines/Algolia3Engine.php index 329d0caf..d489ef5d 100644 --- a/src/Engines/Algolia3Engine.php +++ b/src/Engines/Algolia3Engine.php @@ -140,18 +140,6 @@ public function flush($model) $index->clearObjects(); } - /** - * Create a search index. - * - * @return mixed - * - * @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException - */ - public function updateIndexSettings(string $name, array $options = []) - { - return $this->algolia->initIndex($name)->setSettings($options); - } - /** * Perform the given search on the engine. * @@ -178,4 +166,14 @@ protected function performSearch(Builder $builder, array $options = []) return $algolia->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 d2b1d7df..547b2874 100644 --- a/src/Engines/Algolia4Engine.php +++ b/src/Engines/Algolia4Engine.php @@ -135,18 +135,6 @@ public function flush($model) $this->algolia->clearObjects($model->indexableAs()); } - /** - * Create a search index. - * - * @return mixed - * - * @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException - */ - public function updateIndexSettings(string $name, array $options = []) - { - return $this->algolia->setSettings($name, $options); - } - /** * Perform the given search on the engine. * @@ -175,4 +163,14 @@ protected function performSearch(Builder $builder, array $options = []) $options ); } + + /** + * 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 2df43f7c..dcde78eb 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -84,26 +84,11 @@ abstract public function deleteIndex($name); abstract public function flush($model); /** - * Create a search index. - * - * @return mixed + * Update the index settings for the given index. * - * @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException + * @return void */ - abstract public function updateIndexSettings(string $name, array $options = []); - - /** - * Configures the soft delete filter on the index settings. - * - * @param array $settings - * @return array - */ - public function configureSoftDeleteFilter(array $settings = []): array - { - $settings['attributesForFaceting'][] = 'filterOnly(__soft_deleted)'; - - return $settings; - } + abstract public function updateIndexSettings(string $name, array $settings = []); /** * Perform the given search on the engine. @@ -268,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 bd4903f1..7890a842 100644 --- a/src/Engines/MeilisearchEngine.php +++ b/src/Engines/MeilisearchEngine.php @@ -384,26 +384,21 @@ 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 - * - * @throws \Meilisearch\Exceptions\ApiException + * @return void */ - public function updateIndexSettings($name, array $options = []) + public function updateIndexSettings($name, array $settings = []) { - return $this->meilisearch->index($name)->updateSettings($options); + $this->meilisearch->index($name)->updateSettings($settings); } /** - * Configures the soft delete filter on the index settings. + * Configure the soft delete filter within the given settings. * - * @param array $settings * @return array */ - public function configureSoftDeleteFilter(array $settings = []): array + public function configureSoftDeleteFilter(array $settings = []) { $settings['filterableAttributes'][] = '__soft_deleted';