123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Elasticsearch\SearchAdapter\Aggregation;
- use Magento\Elasticsearch\SearchAdapter\QueryContainer;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\Search\RequestInterface;
- use Magento\Framework\Search\Dynamic\DataProviderInterface;
- use Magento\Elasticsearch\SearchAdapter\Aggregation\Builder\BucketBuilderInterface;
- class Builder
- {
- /**
- * @var DataProviderInterface[]
- */
- protected $dataProviderContainer;
- /**
- * @var BucketBuilderInterface[]
- */
- protected $aggregationContainer;
- /**
- * @var DataProviderFactory
- */
- private $dataProviderFactory;
- /**
- * @var QueryContainer
- */
- private $query = null;
- /**
- * @param DataProviderInterface[] $dataProviderContainer
- * @param BucketBuilderInterface[] $aggregationContainer
- * @param DataProviderFactory|null $dataProviderFactory
- */
- public function __construct(
- array $dataProviderContainer,
- array $aggregationContainer,
- DataProviderFactory $dataProviderFactory = null
- ) {
- $this->dataProviderContainer = array_map(
- function (DataProviderInterface $dataProvider) {
- return $dataProvider;
- },
- $dataProviderContainer
- );
- $this->aggregationContainer = array_map(
- function (BucketBuilderInterface $bucketBuilder) {
- return $bucketBuilder;
- },
- $aggregationContainer
- );
- $this->dataProviderFactory = $dataProviderFactory
- ?: ObjectManager::getInstance()->get(DataProviderFactory::class);
- }
- /**
- * Builds aggregations from the search request.
- *
- * This method iterates through buckets and builds all aggregations one by one, passing buckets and relative
- * data into bucket aggregation builders which are responsible for aggregation calculation.
- *
- * @param RequestInterface $request
- * @param array $queryResult
- * @return array
- * @throws \LogicException thrown by DataProviderFactory for validation issues
- * @see \Magento\Elasticsearch\SearchAdapter\Aggregation\DataProviderFactory
- */
- public function build(RequestInterface $request, array $queryResult)
- {
- $aggregations = [];
- $buckets = $request->getAggregation();
- $dataProvider = $this->dataProviderFactory->create(
- $this->dataProviderContainer[$request->getIndex()],
- $this->query
- );
- foreach ($buckets as $bucket) {
- $bucketAggregationBuilder = $this->aggregationContainer[$bucket->getType()];
- $aggregations[$bucket->getName()] = $bucketAggregationBuilder->build(
- $bucket,
- $request->getDimensions(),
- $queryResult,
- $dataProvider
- );
- }
- $this->query = null;
- return $aggregations;
- }
- /**
- * Sets the QueryContainer instance to the internal property in order to use it in build process
- *
- * @param QueryContainer $query
- * @return $this
- */
- public function setQuery(QueryContainer $query)
- {
- $this->query = $query;
- return $this;
- }
- }
|