Skip to content

Commit 480adfa

Browse files
feat: Algolia settings sync (#889)
* feat: Algolia settings sync * style: fix styleci issues * fix: add index-settings key to algolia config * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 94532fa commit 480adfa

File tree

8 files changed

+90
-13
lines changed

8 files changed

+90
-13
lines changed

config/scout.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@
115115
'algolia' => [
116116
'id' => env('ALGOLIA_APP_ID', ''),
117117
'secret' => env('ALGOLIA_SECRET', ''),
118+
'index-settings' => [
119+
// 'users' => [
120+
// 'searchableAttributes' => ['id', 'name', 'email'],
121+
// 'attributesForFaceting'=> ['filterOnly(email)'],
122+
// ],
123+
],
118124
],
119125

120126
/*

src/Console/IndexCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Console\Command;
77
use Illuminate\Database\Eloquent\SoftDeletes;
88
use Illuminate\Support\Str;
9+
use Laravel\Scout\Contracts\UpdatesIndexSettings;
910
use Laravel\Scout\EngineManager;
1011
use Symfony\Component\Console\Attribute\AsCommand;
1112

@@ -53,7 +54,7 @@ public function handle(EngineManager $manager)
5354

5455
$engine->createIndex($name, $options);
5556

56-
if (method_exists($engine, 'updateIndexSettings')) {
57+
if ($engine instanceof UpdatesIndexSettings) {
5758
$driver = config('scout.driver');
5859

5960
$class = isset($model) ? get_class($model) : null;
@@ -65,7 +66,7 @@ public function handle(EngineManager $manager)
6566
if (isset($model) &&
6667
config('scout.soft_delete', false) &&
6768
in_array(SoftDeletes::class, class_uses_recursive($model))) {
68-
$settings['filterableAttributes'][] = '__soft_deleted';
69+
$settings = $engine->configureSoftDeleteFilter($settings);
6970
}
7071

7172
if ($settings) {

src/Console/SyncIndexSettingsCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Console\Command;
77
use Illuminate\Database\Eloquent\SoftDeletes;
88
use Illuminate\Support\Str;
9+
use Laravel\Scout\Contracts\UpdatesIndexSettings;
910
use Laravel\Scout\EngineManager;
1011
use Symfony\Component\Console\Attribute\AsCommand;
1112

@@ -38,7 +39,7 @@ public function handle(EngineManager $manager)
3839

3940
$driver = config('scout.driver');
4041

41-
if (! method_exists($engine, 'updateIndexSettings')) {
42+
if (! $engine instanceof UpdatesIndexSettings) {
4243
return $this->error('The "'.$driver.'" engine does not support updating index settings.');
4344
}
4445

@@ -60,7 +61,7 @@ public function handle(EngineManager $manager)
6061
if (isset($model) &&
6162
config('scout.soft_delete', false) &&
6263
in_array(SoftDeletes::class, class_uses_recursive($model))) {
63-
$settings['filterableAttributes'][] = '__soft_deleted';
64+
$settings = $engine->configureSoftDeleteFilter($settings);
6465
}
6566

6667
$engine->updateIndexSettings($indexName = $this->indexName($name), $settings);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Laravel\Scout\Contracts;
4+
5+
interface UpdatesIndexSettings
6+
{
7+
/**
8+
* Update the index settings for the given index.
9+
*
10+
* @return void
11+
*/
12+
public function updateIndexSettings(string $name, array $settings = []);
13+
14+
/**
15+
* Configure the soft delete filter within the given settings.
16+
*
17+
* @return array
18+
*/
19+
public function configureSoftDeleteFilter(array $settings = []);
20+
}

src/Engines/Algolia3Engine.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,14 @@ protected function performSearch(Builder $builder, array $options = [])
166166

167167
return $algolia->search($builder->query, $options);
168168
}
169+
170+
/**
171+
* Update the index settings for the given index.
172+
*
173+
* @return void
174+
*/
175+
public function updateIndexSettings(string $name, array $settings = [])
176+
{
177+
$this->algolia->initIndex($name)->setSettings($settings);
178+
}
169179
}

src/Engines/Algolia4Engine.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,14 @@ protected function performSearch(Builder $builder, array $options = [])
162162
$queryParams
163163
);
164164
}
165+
166+
/**
167+
* Update the index settings for the given index.
168+
*
169+
* @return void
170+
*/
171+
public function updateIndexSettings(string $name, array $settings = [])
172+
{
173+
$this->algolia->setSettings($name, $settings);
174+
}
165175
}

src/Engines/AlgoliaEngine.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
use Illuminate\Database\Eloquent\SoftDeletes;
77
use Illuminate\Support\LazyCollection;
88
use Laravel\Scout\Builder;
9+
use Laravel\Scout\Contracts\UpdatesIndexSettings;
910

1011
/**
1112
* @template TAlgoliaClient of object
1213
*/
13-
abstract class AlgoliaEngine extends Engine
14+
abstract class AlgoliaEngine extends Engine implements UpdatesIndexSettings
1415
{
1516
/**
1617
* The Algolia client.
@@ -82,6 +83,13 @@ abstract public function deleteIndex($name);
8283
*/
8384
abstract public function flush($model);
8485

86+
/**
87+
* Update the index settings for the given index.
88+
*
89+
* @return void
90+
*/
91+
abstract public function updateIndexSettings(string $name, array $settings = []);
92+
8593
/**
8694
* Perform the given search on the engine.
8795
*
@@ -245,6 +253,18 @@ public function createIndex($name, array $options = [])
245253
throw new Exception('Algolia indexes are created automatically upon adding objects.');
246254
}
247255

256+
/**
257+
* Configure the soft delete filter within the given settings.
258+
*
259+
* @return array
260+
*/
261+
public function configureSoftDeleteFilter(array $settings = [])
262+
{
263+
$settings['attributesForFaceting'][] = 'filterOnly(__soft_deleted)';
264+
265+
return $settings;
266+
}
267+
248268
/**
249269
* Determine if the given model uses soft deletes.
250270
*

src/Engines/MeilisearchEngine.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use Illuminate\Support\LazyCollection;
66
use Laravel\Scout\Builder;
7+
use Laravel\Scout\Contracts\UpdatesIndexSettings;
78
use Laravel\Scout\Jobs\RemoveableScoutCollection;
89
use Meilisearch\Client as MeilisearchClient;
910
use Meilisearch\Contracts\IndexesQuery;
1011
use Meilisearch\Meilisearch;
1112
use Meilisearch\Search\SearchResult;
1213

13-
class MeilisearchEngine extends Engine
14+
class MeilisearchEngine extends Engine implements UpdatesIndexSettings
1415
{
1516
/**
1617
* The Meilisearch client.
@@ -383,17 +384,25 @@ public function createIndex($name, array $options = [])
383384
}
384385

385386
/**
386-
* Update an index's settings.
387+
* Update the index settings for the given index.
387388
*
388-
* @param string $name
389-
* @param array $options
390-
* @return array
389+
* @return void
390+
*/
391+
public function updateIndexSettings($name, array $settings = [])
392+
{
393+
$this->meilisearch->index($name)->updateSettings($settings);
394+
}
395+
396+
/**
397+
* Configure the soft delete filter within the given settings.
391398
*
392-
* @throws \Meilisearch\Exceptions\ApiException
399+
* @return array
393400
*/
394-
public function updateIndexSettings($name, array $options = [])
401+
public function configureSoftDeleteFilter(array $settings = [])
395402
{
396-
return $this->meilisearch->index($name)->updateSettings($options);
403+
$settings['filterableAttributes'][] = '__soft_deleted';
404+
405+
return $settings;
397406
}
398407

399408
/**

0 commit comments

Comments
 (0)