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

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