Skip to content

Commit

Permalink
Remove the idea of replicas since Meilisearch doesn't use this concept
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jul 1, 2024
1 parent 981c46e commit 3807950
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 146 deletions.
16 changes: 0 additions & 16 deletions src/Indexer/DefaultIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Setono\SyliusMeilisearchPlugin\Repository\IndexableResourceRepositoryInterface;
use Setono\SyliusMeilisearchPlugin\Resolver\IndexName\IndexNameResolverInterface;
use Setono\SyliusMeilisearchPlugin\Settings\IndexSettings;
use Setono\SyliusMeilisearchPlugin\Settings\SortableReplica;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -226,21 +225,6 @@ protected function prepareIndex(string $indexName, IndexSettings $indexSettings)
/** @psalm-suppress MixedAssignment,MixedArrayAccess */
$indexSettings->ranking = $index->getSettings()['ranking'];

foreach ($indexSettings->replicas as $replica) {
if (!$replica instanceof SortableReplica) {
continue;
}

$replicaIndex = $this->client->initIndex($replica->name);
Assert::isInstanceOf($replicaIndex, SearchIndex::class);

$replicaIndexSettings = clone $indexSettings;
$replicaIndexSettings->replicas = [];
array_unshift($replicaIndexSettings->ranking, $replica->ranking());

$replicaIndex->setSettings($replicaIndexSettings->toArray());
}

return $index;
}
}
28 changes: 1 addition & 27 deletions src/Provider/IndexSettings/IndexSettingsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,13 @@
namespace Setono\SyliusMeilisearchPlugin\Provider\IndexSettings;

use Setono\SyliusMeilisearchPlugin\IndexScope\IndexScope;
use Setono\SyliusMeilisearchPlugin\Resolver\IndexName\IndexNameResolverInterface;
use Setono\SyliusMeilisearchPlugin\Resolver\ReplicaIndexName\ReplicaIndexNameResolverInterface;
use Setono\SyliusMeilisearchPlugin\Settings\IndexSettings;
use Setono\SyliusMeilisearchPlugin\Settings\SortableReplica;

final class IndexSettingsProvider implements IndexSettingsProviderInterface
{
public function __construct(
private readonly IndexNameResolverInterface $indexNameResolver,
private readonly ReplicaIndexNameResolverInterface $replicaIndexNameResolver,
)
{
}

// todo implement an easier way to set settings decoupled from the document. This could be by dispatching an IndexSettingsEvent
public function getSettings(IndexScope $indexScope): IndexSettings
{
$settings = $indexScope->index->document::getDefaultSettings($indexScope);
$indexName = $this->indexNameResolver->resolveFromIndexScope($indexScope);

foreach ($settings->replicas as &$replica) {
$replica = $this->replicaIndexNameResolver->resolveFromIndexNameAndExistingValue($indexName, (string) $replica);
}
unset($replica);

foreach ($indexScope->index->document::getSortableAttributes() as $attribute => $order) {
$settings->replicas[] = new SortableReplica(
$this->replicaIndexNameResolver->resolveFromIndexNameAndSortableAttribute($indexName, $attribute, $order),
$attribute,
$order,
);
}

return $settings;
return $indexScope->index->document::getDefaultSettings($indexScope);
}
}
21 changes: 0 additions & 21 deletions src/Resolver/ReplicaIndexName/ReplicaIndexNameResolver.php

This file was deleted.

This file was deleted.

22 changes: 6 additions & 16 deletions src/Resolver/SortBy/SortByResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use Setono\SyliusMeilisearchPlugin\Config\Index;
use Setono\SyliusMeilisearchPlugin\Resolver\IndexName\IndexNameResolverInterface;
use Setono\SyliusMeilisearchPlugin\Resolver\ReplicaIndexName\ReplicaIndexNameResolverInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class SortByResolver implements SortByResolverInterface
{
public function __construct(private readonly IndexNameResolverInterface $indexNameResolver, private readonly ReplicaIndexNameResolverInterface $replicaIndexNameResolver, private readonly TranslatorInterface $translator, private readonly LocaleContextInterface $localeContext)
public function __construct(
private readonly IndexNameResolverInterface $indexNameResolver,
private readonly TranslatorInterface $translator,
private readonly LocaleContextInterface $localeContext,
)
{
}

Expand All @@ -22,24 +25,11 @@ public function resolveFromIndexableResource(Index $index, string $locale = null

$indexName = $this->indexNameResolver->resolve($index);

$sortBys = [
return [
new SortBy(
$this->translator->trans('setono_sylius_meilisearch.ui.sort_by.relevance', [], null, $locale),
$indexName,
),
];

foreach ($index->document::getSortableAttributes() as $attribute => $order) {
$sortBys[] = new SortBy(
$this->translator->trans(sprintf('setono_sylius_meilisearch.ui.sort_by.%s_%s', $attribute, $order), [], null, $locale),
$this->replicaIndexNameResolver->resolveFromIndexNameAndSortableAttribute(
$indexName,
$attribute,
$order,
),
);
}

return $sortBys;
}
}
5 changes: 1 addition & 4 deletions src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@

<!-- Index settings providers -->
<service id="setono_sylius_meilisearch.provider.index_settings"
class="Setono\SyliusMeilisearchPlugin\Provider\IndexSettings\IndexSettingsProvider">
<argument type="service" id="setono_sylius_meilisearch.resolver.index_name"/>
<argument type="service" id="setono_sylius_meilisearch.resolver.replica_index_name"/>
</service>
class="Setono\SyliusMeilisearchPlugin\Provider\IndexSettings\IndexSettingsProvider"/>
</services>
</container>
4 changes: 0 additions & 4 deletions src/Resources/config/services/resolver.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
<argument>%kernel.environment%</argument>
</service>

<service id="setono_sylius_meilisearch.resolver.replica_index_name"
class="Setono\SyliusMeilisearchPlugin\Resolver\ReplicaIndexName\ReplicaIndexNameResolver"/>

<service id="setono_sylius_meilisearch.resolver.sort_by"
class="Setono\SyliusMeilisearchPlugin\Resolver\SortBy\SortByResolver">
<argument type="service" id="setono_sylius_meilisearch.resolver.index_name"/>
<argument type="service" id="setono_sylius_meilisearch.resolver.replica_index_name"/>
<argument type="service" id="translator"/>
<argument type="service" id="sylius.context.locale"/>
</service>
Expand Down
2 changes: 0 additions & 2 deletions src/SetonoSyliusMeilisearchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Setono\CompositeCompilerPass\CompositeCompilerPass;
use Setono\SyliusMeilisearchPlugin\DependencyInjection\Compiler\RegisterIndexesPass;
use Setono\SyliusMeilisearchPlugin\DependencyInjection\Compiler\RegisterTwigVariablePass;
use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -19,7 +18,6 @@ public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new RegisterTwigVariablePass());
$container->addCompilerPass(new RegisterIndexesPass());

// Register services in composite services
Expand Down
22 changes: 0 additions & 22 deletions src/Settings/IndexSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,6 @@ class IndexSettings extends Settings
/** @var list<string> */
public array $customRanking = [];

/**
* If you add replica indexes they will automatically be prefixed with the primary index name
*
* Example:
* Your primary index name: prod__products__fashion_web__en_us__usd
* You set $replicas = ['a_b']
*
* When sent to Algolia, $replicas will be ['prod__products__fashion_web__en_us__usd__a_b']
*
* ----
*
* Also notice that if you have implemented \Setono\SyliusMeilisearchPlugin\Document\Document::getSortableAttributes()
* the $replicas will be automatically populated by the plugin. You don't have to do anything.
*
* Example:
* Your document's getSortableAttributes method returns ['price' => 'asc'] then the $replicas will automatically
* be set to [prod__products__fashion_web__en_us__usd__price_asc]
*
* @var list<string|SortableReplica>
*/
public array $replicas = [];

public ?int $paginationLimitedTo = null;

/** @var list<string> */
Expand Down
22 changes: 0 additions & 22 deletions src/Settings/SortableReplica.php

This file was deleted.

0 comments on commit 3807950

Please sign in to comment.