custom/plugins/SwagEnterpriseSearchPlatform/src/Indexing/SynonymUpdateSubscriber.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\EnterpriseSearch\Indexing;
  3. use Elasticsearch\Client;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  10. use Shopware\Elasticsearch\Framework\ElasticsearchHelper;
  11. use Shopware\Elasticsearch\Framework\ElasticsearchRegistry;
  12. use Swag\EnterpriseSearch\Synonym\SynonymProvider;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class SynonymUpdateSubscriber implements EventSubscriberInterface
  15. {
  16.     private $languageRepository;
  17.     private $synonymProvider;
  18.     private $helper;
  19.     private $client;
  20.     private $registry;
  21.     /**
  22.      * @var array<int, string>
  23.      */
  24.     private $languages;
  25.     public function __construct(
  26.         ElasticsearchRegistry $registry,
  27.         ElasticsearchHelper $helper,
  28.         EntityRepositoryInterface $languageRepository,
  29.         Client $client,
  30.         SynonymProvider $synonymProvider
  31.     ) {
  32.         $this->registry $registry;
  33.         $this->helper $helper;
  34.         $this->languageRepository $languageRepository;
  35.         $this->client $client;
  36.         $this->synonymProvider $synonymProvider;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             'synonym.written' => 'onSynonymWritten',
  42.         ];
  43.     }
  44.     public function onSynonymWritten(): void
  45.     {
  46.         if (empty($this->languages)) {
  47.             $this->languages $this->getLanguages();
  48.         }
  49.         $indices $this->client->indices();
  50.         $definitions $this->registry->getDefinitions();
  51.         foreach ($this->languages as $languageId) {
  52.             foreach ($definitions as $definition) {
  53.                 $alias $this->helper->getIndexName($definition->getEntityDefinition(), $languageId);
  54.                 $config $this->getConfigTemplate($languageId);
  55.                 $indices->close(['index' => $alias]);
  56.                 $indices->putSettings(['index' => $alias'body' => $config]);
  57.                 $indices->open(['index' => $alias]);
  58.             }
  59.         }
  60.     }
  61.     private function getConfigTemplate(string $languageId): array
  62.     {
  63.         return [
  64.             'settings' => [
  65.                 'analysis' => [
  66.                     'filter' => [
  67.                         'synonym' => [
  68.                             'synonyms' => $this->synonymProvider->getSynonyms($languageId),
  69.                         ],
  70.                     ],
  71.                 ],
  72.             ],
  73.         ];
  74.     }
  75.     private function getLanguages(): array
  76.     {
  77.         $criteria = new Criteria();
  78.         $context Context::createDefaultContext();
  79.         $criteria->addFilter(new NotFilter(NotFilter::CONNECTION_AND, [
  80.             new EqualsFilter('salesChannelDomains.id'null),
  81.         ]));
  82.         $criteria->addSorting(new FieldSorting('id'));
  83.         return $this->languageRepository->searchIds($criteria$context)->getIds();
  84.     }
  85. }