Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Algolia settings sync #889

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@
'algolia' => [
'id' => env('ALGOLIA_APP_ID', ''),
'secret' => env('ALGOLIA_SECRET', ''),
'index-settings' => [
// 'users' => [
// 'searchableAttributes' => ['id', 'name', 'email'],
// 'attributesForFaceting'=> ['filterOnly(email)'],
// ],
],
],

/*
Expand Down
5 changes: 3 additions & 2 deletions src/Console/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/Console/SyncIndexSettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.');
}

Expand All @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions src/Contracts/UpdatesIndexSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Laravel\Scout\Contracts;

interface UpdatesIndexSettings
{
/**
* Update the index settings for the given index.
*
* @return void
*/
public function updateIndexSettings(string $name, array $settings = []);

/**
* Configure the soft delete filter within the given settings.
*
* @return array
*/
public function configureSoftDeleteFilter(array $settings = []);
}
10 changes: 10 additions & 0 deletions src/Engines/Algolia3Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,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);
}
}
10 changes: 10 additions & 0 deletions src/Engines/Algolia4Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,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);
}
}
22 changes: 21 additions & 1 deletion src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 17 additions & 8 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand Down