Skip to content

Commit 0fc4c25

Browse files
authored
Merge pull request #155 from delyriand/fix/153-add-prefix-option
Add prefix option for names of indexes and aliases
2 parents d735fd2 + 13efed5 commit 0fc4c25

File tree

10 files changed

+98
-12
lines changed

10 files changed

+98
-12
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ server.stop: ## Stop the local webserver
202202
es.reindex: ## Reindex elasticsearch
203203
${CONSOLE} monsieurbiz:search:populate
204204

205+
consume.reindex: ## Consume reindex messages during 10min
206+
${CONSOLE} messenger:consume async_search --time-limit=600 -vv
207+
205208
doctrine.diff: ## Doctrine diff
206209
${CONSOLE} doctrine:migration:diff
207210

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
imports:
22
- { resource: "@MonsieurBizSyliusSearchPlugin/Resources/config/config.yaml" }
3+
4+
monsieurbiz_sylius_search:
5+
documents:
6+
monsieurbiz_product:
7+
prefix: 'myprefix' # define a custom index prefix

doc/Tips.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tips
2+
3+
## Add prefix for index names and aliases
4+
5+
In the `config/packages/monsieurbiz_sylius_search_plugin.yaml` file, add the `prefix` node for documents that need a prefix in the names of indexes and aliases.
6+
7+
Example, for the products index:
8+
9+
```diff
10+
imports:
11+
- { resource: "@MonsieurBizSyliusSearchPlugin/Resources/config/config.yaml" }
12+
13+
+monsieurbiz_sylius_search:
14+
+ documents:
15+
+ monsieurbiz_product:
16+
+ prefix: 'myproject' # define a custom index prefix
17+
```

src/DependencyInjection/Configuration.php

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function getConfigTreeBuilder(): TreeBuilder
3535
->defaultValue([])
3636
->arrayPrototype()
3737
->children()
38+
->scalarNode('prefix')->defaultValue(null)->end()
3839
->scalarNode('document_class')->defaultValue(Documentable::class)->end()
3940
->scalarNode('instant_search_enabled')->defaultValue(false)->end()
4041
->scalarNode('source')->isRequired()->cannotBeEmpty()->end()

src/DependencyInjection/DocumentableRegistryPass.php

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use InvalidArgumentException;
1717
use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\DocumentableInterface;
18+
use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\PrefixedDocumentableInterface;
1819
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
2021
use Symfony\Component\DependencyInjection\Definition;
@@ -56,6 +57,9 @@ public function process(ContainerBuilder $container): void
5657
$documentableDefinition->addTag('monsieurbiz.search.documentable');
5758
$documentableDefinition->addMethodCall('setMappingProvider', [new Reference($documentableConfiguration['mapping_provider'])]);
5859
$documentableDefinition->addMethodCall('setDatasource', [new Reference($documentableConfiguration['datasource'])]);
60+
if ($this->isPrefixedDocumentableClass($documentableClass) && isset($documentableConfiguration['prefix'])) {
61+
$documentableDefinition->addMethodCall('setPrefix', [$documentableConfiguration['prefix']]);
62+
}
5963

6064
// Add documentable into registry
6165
$registry->addMethodCall('register', [$documentableServiceId, new Reference($documentableServiceId)]);
@@ -81,4 +85,11 @@ private function validateDocumentableResource(string $class): void
8185
throw new InvalidArgumentException(sprintf('Class "%s" must implement "%s" to be registered as a Documentable.', $class, DocumentableInterface::class));
8286
}
8387
}
88+
89+
private function isPrefixedDocumentableClass(string $class): bool
90+
{
91+
$interfaces = (array) (class_implements($class) ?? []);
92+
93+
return \in_array(PrefixedDocumentableInterface::class, $interfaces, true);
94+
}
8495
}

src/Index/Indexer.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ public function indexByDocuments(DocumentableInterface $documentable, array $doc
8080

8181
return;
8282
}
83-
$indexName = $this->getIndexName($documentable, $locale);
83+
$index = $this->clientFactory->getIndex($documentable, $locale);
8484
foreach ($documents as $document) {
8585
if (null !== $locale && $document instanceof TranslatableInterface) {
8686
$document->setCurrentLocale($locale);
8787
}
8888
$dto = $this->autoMapper->map($document, $documentable->getTargetClass());
8989
// @phpstan-ignore-next-line
90-
$indexer->scheduleIndex($indexName, new Document((string) $document->getId(), $dto));
90+
$indexer->scheduleIndex($index, new Document((string) $document->getId(), $dto));
9191
}
9292
}
9393

@@ -107,12 +107,12 @@ public function deleteByDocuments(DocumentableInterface $documentable, array $do
107107
return;
108108
}
109109

110-
$indexName = $this->getIndexName($documentable, $locale);
110+
$index = $this->clientFactory->getIndex($documentable, $locale);
111111
foreach ($documents as $document) {
112112
if (null !== $locale && $document instanceof TranslatableInterface) {
113113
$document->setCurrentLocale($locale);
114114
}
115-
$indexer->scheduleDelete($indexName, (string) $document->getId());
115+
$indexer->scheduleDelete($index, (string) $document->getId());
116116
}
117117
}
118118

@@ -169,11 +169,6 @@ private function indexDocumentable(DocumentableInterface $documentable, ?string
169169
$indexBuilder->purgeOldIndices($indexName);
170170
}
171171

172-
private function getIndexName(DocumentableInterface $documentable, ?string $locale = null): string
173-
{
174-
return $documentable->getIndexCode() . strtolower(null !== $locale ? '_' . $locale : '');
175-
}
176-
177172
/**
178173
* Convert proxies classes to the entity one.
179174
*

src/Model/Documentable/Documentable.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use Sylius\Component\Resource\Model\TranslatableInterface;
1717

18-
class Documentable implements DocumentableInterface
18+
class Documentable implements PrefixedDocumentableInterface
1919
{
2020
use DocumentableDatasourceTrait;
2121

@@ -34,6 +34,8 @@ class Documentable implements DocumentableInterface
3434

3535
private array $limits;
3636

37+
private ?string $prefix = null;
38+
3739
public function __construct(
3840
string $indexCode,
3941
string $sourceClass,
@@ -83,4 +85,14 @@ public function getLimits(?string $queryType = null): array
8385

8486
return $this->limits[$queryType] ?? [];
8587
}
88+
89+
public function getPrefix(): string
90+
{
91+
return $this->prefix ?? '';
92+
}
93+
94+
public function setPrefix(string $prefix): void
95+
{
96+
$this->prefix = $prefix;
97+
}
8698
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz' Search plugin for Sylius.
5+
*
6+
* (c) Monsieur Biz <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace MonsieurBiz\SyliusSearchPlugin\Model\Documentable;
15+
16+
interface PrefixedDocumentableInterface extends DocumentableInterface
17+
{
18+
public function getPrefix(): string;
19+
20+
public function setPrefix(string $prefix): void;
21+
}

src/Resources/config/monsieurbiz_search.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
monsieurbiz_sylius_search:
22
documents:
33
monsieurbiz_product:
4+
#prefix: '…' # define a custom index prefix on index names and aliases
45
#document_class: '…' # by default MonsieurBiz\SyliusSearchPlugin\Model\Documentable\Documentable
56
instant_search_enabled: true # by default false
67
limits:

src/Search/ClientFactory.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
namespace MonsieurBiz\SyliusSearchPlugin\Search;
1515

16+
use Elastica\Index;
1617
use JoliCode\Elastically\Client;
1718
use JoliCode\Elastically\Factory;
1819
use JoliCode\Elastically\IndexBuilder;
1920
use JoliCode\Elastically\Indexer;
2021
use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\DocumentableInterface;
22+
use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\PrefixedDocumentableInterface;
2123
use Symfony\Component\Serializer\SerializerInterface;
2224

2325
class ClientFactory
@@ -58,16 +60,34 @@ public function getIndexName(DocumentableInterface $documentable, ?string $local
5860
return $documentable->getIndexCode() . strtolower(null !== $locale ? '_' . $locale : '');
5961
}
6062

61-
private function getConfig(DocumentableInterface $documentable, ?string $localeCode): array
63+
/**
64+
* This method allows to find the name of the index with the prefix,
65+
* because the methods of the JoliCode\ElasticallyIndexer class do not add
66+
* it automatically.
67+
*/
68+
public function getIndex(DocumentableInterface $documentable, ?string $locale): Index
6269
{
63-
$indexName = $this->getIndexName($documentable, $localeCode);
70+
$indexName = $this->getIndexName($documentable, $locale);
71+
$factory = new Factory($this->getConfig($documentable, $locale, $indexName));
72+
$client = $factory->buildClient();
73+
74+
return $client->getIndex($indexName);
75+
}
76+
77+
private function getConfig(DocumentableInterface $documentable, ?string $localeCode, ?string $indexName = null): array
78+
{
79+
$indexName = $indexName ?? $this->getIndexName($documentable, $localeCode);
6480
$additionalConfig = [
6581
Factory::CONFIG_INDEX_CLASS_MAPPING => [
6682
$indexName => $documentable->getTargetClass(),
6783
],
6884
Factory::CONFIG_MAPPINGS_PROVIDER => $documentable->getMappingProvider(),
6985
Factory::CONFIG_SERIALIZER => $this->serializer,
7086
];
87+
$prefix = $documentable instanceof PrefixedDocumentableInterface ? trim($documentable->getPrefix()) : '';
88+
if ('' !== $prefix) {
89+
$additionalConfig[Factory::CONFIG_INDEX_PREFIX] = $prefix;
90+
}
7191

7292
return array_merge($this->config, $additionalConfig);
7393
}

0 commit comments

Comments
 (0)