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

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Elasticsearch\Framework\DataAbstractionLayer;
  3. use OpenSearch\Client;
  4. use OpenSearchDSL\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.     final public const RESULT_STATE 'loaded-by-elastic';
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(
  22.         private readonly ElasticsearchHelper $helper,
  23.         private readonly Client $client,
  24.         private readonly EntityAggregatorInterface $decorated,
  25.         private readonly AbstractElasticsearchAggregationHydrator $hydrator,
  26.         private readonly EventDispatcherInterface $eventDispatcher
  27.     ) {
  28.     }
  29.     public function aggregate(EntityDefinition $definitionCriteria $criteriaContext $context): AggregationResultCollection
  30.     {
  31.         if (!$this->helper->allowSearch($definition$context$criteria)) {
  32.             return $this->decorated->aggregate($definition$criteria$context);
  33.         }
  34.         $search $this->createSearch($definition$criteria$context);
  35.         $this->eventDispatcher->dispatch(
  36.             new ElasticsearchEntityAggregatorSearchEvent($search$definition$criteria$context)
  37.         );
  38.         try {
  39.             $result $this->client->search([
  40.                 'index' => $this->helper->getIndexName($definition$context->getLanguageId()),
  41.                 'body' => $search->toArray(),
  42.             ]);
  43.         } catch (\Throwable $e) {
  44.             $this->helper->logAndThrowException($e);
  45.             return $this->decorated->aggregate($definition$criteria$context);
  46.         }
  47.         $result $this->hydrator->hydrate($definition$criteria$context$result);
  48.         $result->addState(self::RESULT_STATE);
  49.         return $result;
  50.     }
  51.     private function createSearch(EntityDefinition $definitionCriteria $criteriaContext $context): Search
  52.     {
  53.         $search = new Search();
  54.         $this->helper->addFilters($definition$criteria$search$context);
  55.         $this->helper->addQueries($definition$criteria$search$context);
  56.         $this->helper->addAggregations($definition$criteria$search$context);
  57.         $this->helper->addTerm($criteria$search$context$definition);
  58.         $this->helper->handleIds($definition$criteria$search$context);
  59.         $search->setSize(0);
  60.         return $search;
  61.     }
  62. }