|
13 | 13 |
|
14 | 14 | namespace MonsieurBiz\SyliusSearchPlugin\Search;
|
15 | 15 |
|
| 16 | +use Elastica\Index; |
16 | 17 | use JoliCode\Elastically\Client;
|
17 | 18 | use JoliCode\Elastically\Factory;
|
18 | 19 | use JoliCode\Elastically\IndexBuilder;
|
19 | 20 | use JoliCode\Elastically\Indexer;
|
20 | 21 | use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\DocumentableInterface;
|
| 22 | +use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\PrefixedDocumentableInterface; |
21 | 23 | use Symfony\Component\Serializer\SerializerInterface;
|
22 | 24 |
|
23 | 25 | class ClientFactory
|
@@ -58,16 +60,34 @@ public function getIndexName(DocumentableInterface $documentable, ?string $local
|
58 | 60 | return $documentable->getIndexCode() . strtolower(null !== $locale ? '_' . $locale : '');
|
59 | 61 | }
|
60 | 62 |
|
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 |
62 | 69 | {
|
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); |
64 | 80 | $additionalConfig = [
|
65 | 81 | Factory::CONFIG_INDEX_CLASS_MAPPING => [
|
66 | 82 | $indexName => $documentable->getTargetClass(),
|
67 | 83 | ],
|
68 | 84 | Factory::CONFIG_MAPPINGS_PROVIDER => $documentable->getMappingProvider(),
|
69 | 85 | Factory::CONFIG_SERIALIZER => $this->serializer,
|
70 | 86 | ];
|
| 87 | + $prefix = $documentable instanceof PrefixedDocumentableInterface ? trim($documentable->getPrefix()) : ''; |
| 88 | + if ('' !== $prefix) { |
| 89 | + $additionalConfig[Factory::CONFIG_INDEX_PREFIX] = $prefix; |
| 90 | + } |
71 | 91 |
|
72 | 92 | return array_merge($this->config, $additionalConfig);
|
73 | 93 | }
|
|
0 commit comments