src/Elasticsearch/Framework/DataAbstractionLayer/ElasticsearchEntityAggregator.php line 32

  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(private readonly ElasticsearchHelper $helper, private readonly Client $client, private readonly EntityAggregatorInterface $decorated, private readonly AbstractElasticsearchAggregationHydrator $hydrator, private readonly EventDispatcherInterface $eventDispatcher)
  22.     {
  23.     }
  24.     public function aggregate(EntityDefinition $definitionCriteria $criteriaContext $context): AggregationResultCollection
  25.     {
  26.         if (!$this->helper->allowSearch($definition$context$criteria)) {
  27.             return $this->decorated->aggregate($definition$criteria$context);
  28.         }
  29.         $search $this->createSearch($definition$criteria$context);
  30.         $this->eventDispatcher->dispatch(
  31.             new ElasticsearchEntityAggregatorSearchEvent($search$definition$criteria$context)
  32.         );
  33.         try {
  34.             $result $this->client->search([
  35.                 'index' => $this->helper->getIndexName($definition$context->getLanguageId()),
  36.                 'body' => $search->toArray(),
  37.             ]);
  38.         } catch (\Throwable $e) {
  39.             $this->helper->logAndThrowException($e);
  40.             return $this->decorated->aggregate($definition$criteria$context);
  41.         }
  42.         $result $this->hydrator->hydrate($definition$criteria$context$result);
  43.         $result->addState(self::RESULT_STATE);
  44.         return $result;
  45.     }
  46.     private function createSearch(EntityDefinition $definitionCriteria $criteriaContext $context): Search
  47.     {
  48.         $search = new Search();
  49.         $this->helper->addFilters($definition$criteria$search$context);
  50.         $this->helper->addQueries($definition$criteria$search$context);
  51.         $this->helper->addAggregations($definition$criteria$search$context);
  52.         $this->helper->addTerm($criteria$search$context$definition);
  53.         $this->helper->handleIds($definition$criteria$search$context);
  54.         $search->setSize(0);
  55.         return $search;
  56.     }
  57. }