vendor/shopware/elasticsearch/Framework/DataAbstractionLayer/ElasticsearchEntityAggregator.php line 81

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Elasticsearch\Framework\DataAbstractionLayer;
  3. use Elasticsearch\Client;
  4. use ONGR\ElasticsearchDSL\Search;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Shopware\Elasticsearch\Framework\DataAbstractionLayer\Event\ElasticsearchEntityAggregatorSearchEvent;
  12. use Shopware\Elasticsearch\Framework\ElasticsearchHelper;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. #[Package('core')]
  15. class ElasticsearchEntityAggregator implements EntityAggregatorInterface
  16. {
  17.     public const RESULT_STATE 'loaded-by-elastic';
  18.     private ElasticsearchHelper $helper;
  19.     private Client $client;
  20.     private EntityAggregatorInterface $decorated;
  21.     private AbstractElasticsearchAggregationHydrator $hydrator;
  22.     private EventDispatcherInterface $eventDispatcher;
  23.     /**
  24.      * @internal
  25.      */
  26.     public function __construct(
  27.         ElasticsearchHelper $helper,
  28.         Client $client,
  29.         EntityAggregatorInterface $decorated,
  30.         AbstractElasticsearchAggregationHydrator $hydrator,
  31.         EventDispatcherInterface $eventDispatcher
  32.     ) {
  33.         $this->helper $helper;
  34.         $this->client $client;
  35.         $this->decorated $decorated;
  36.         $this->hydrator $hydrator;
  37.         $this->eventDispatcher $eventDispatcher;
  38.     }
  39.     public function aggregate(EntityDefinition $definitionCriteria $criteriaContext $context): AggregationResultCollection
  40.     {
  41.         if (!$this->helper->allowSearch($definition$context$criteria)) {
  42.             return $this->decorated->aggregate($definition$criteria$context);
  43.         }
  44.         $search $this->createSearch($definition$criteria$context);
  45.         $this->eventDispatcher->dispatch(
  46.             new ElasticsearchEntityAggregatorSearchEvent($search$definition$criteria$context)
  47.         );
  48.         try {
  49.             $result $this->client->search([
  50.                 'index' => $this->helper->getIndexName($definition$context->getLanguageId()),
  51.                 'body' => $search->toArray(),
  52.             ]);
  53.         } catch (\Throwable $e) {
  54.             $this->helper->logAndThrowException($e);
  55.             return $this->decorated->aggregate($definition$criteria$context);
  56.         }
  57.         $result $this->hydrator->hydrate($definition$criteria$context$result);
  58.         $result->addState(self::RESULT_STATE);
  59.         return $result;
  60.     }
  61.     private function createSearch(EntityDefinition $definitionCriteria $criteriaContext $context): Search
  62.     {
  63.         $search = new Search();
  64.         $this->helper->addFilters($definition$criteria$search$context);
  65.         $this->helper->addQueries($definition$criteria$search$context);
  66.         $this->helper->addAggregations($definition$criteria$search$context);
  67.         $this->helper->addTerm($criteria$search$context$definition);
  68.         $this->helper->handleIds($definition$criteria$search$context);
  69.         $search->setSize(0);
  70.         return $search;
  71.     }
  72. }