Build a aggregation object from the search engine response.

@param array $rawAggregations ES aggregations response.

@return \Magento\Framework\Search\Response\Aggregation
189991
public function create(array $rawAggregations)
    {
        $buckets = $this->getBuckets($rawAggregations);

        return $this->aggregationFactory->create(['buckets' => $buckets]);
    }
1146071
protected function getAggregate(callable $prepareAggregate)
    {
        $query = clone $this;
        $query->select = [];
        $query->order = [];
        $query = $query
            ->offset(null)
            ->limit(null)
            ->apply($prepareAggregate)
            ->apply($this->database->getTablePrefixer());

        $compiled = $this->database->getGrammar()->compileSelect($query);

        return current($this->database->selectFirst($compiled->getSQL(), $compiled->getBindings()));
    }
576238
private function addAggregationFilters()
    {
        foreach ($this->_data as $requestName => $requestConfig) {
            if (isset($requestConfig['aggregations'])) {
                $aggregations = [];

                foreach ($requestConfig['aggregations'] as $aggName => $aggConfig) {
                    $aggregations[$aggName] = $this->replaceAggregationFilter($aggConfig);
                }

                $this->_data[$requestName]['aggregations'] = $aggregations;
            }
        }
    }
763489
private function addAggregationProviders()
    {
        foreach ($this->_data as $requestName => $requestConfig) {
            if (isset($requestConfig['aggregationsProviders'])) {
                $providers = [];

                foreach ($requestConfig['aggregationsProviders'] as $providerName => $providerClass) {
                    $providers[$providerName] = $this->objectManager->create($providerClass, ['requestName' => $requestName]);
                }

                $this->_data[$requestName]['aggregationsProviders'] = $providers;
            }
        }
    }
714314
public function buildAggregations(array $buckets = [])
    {
        $aggregations = [];

        foreach ($buckets as $bucket) {
            $bucketType = $bucket->getType();
            $builder    = $this->getBuilder($bucketType);
            $aggregation     = $builder->buildBucket($bucket);
            $subAggregations = $aggregation['aggregations'] ?? [];

            if (!empty($bucket->getChildBuckets())) {
                $subAggregations = array_merge($subAggregations, $this->buildAggregations($bucket->getChildBuckets()));
            }

            foreach ($bucket->getMetrics() as $metric) {
                $subAggregations[$metric->getName()] = [$metric->getType() => ['field' => $metric->getField()]];
            }

            foreach ($bucket->getPipelines() as $pipeline) {
                $pipelineType    = $pipeline->getType();
                $pipelineBuilder = $this->getPipelineBuilder($pipelineType);
                $pipelineAgg     = $pipelineBuilder->buildPipeline($pipeline);
                $subAggregations[$pipeline->getName()] = $pipelineAgg;
            }

            if (!empty($subAggregations)) {
                $aggregation['aggregations'] = $subAggregations;
            }

            if ($bucket->isNested()) {
                if ($bucket->getNestedFilter()) {
                    $aggregation = [
                        'filter'       => $this->queryBuilder->buildQuery($bucket->getNestedFilter()),
                        'aggregations' => [$bucket->getName() => $aggregation],
                    ];
                }

                $aggregation = [
                    'nested'       => ['path' => $bucket->getNestedPath()],
                    'aggregations' => [$bucket->getName() => $aggregation],
                ];
            }

            if ($bucket->getFilter()) {
                $aggregation = [
                    'filter'       => $this->queryBuilder->buildQuery($bucket->getFilter()),
                    'aggregations' => [$bucket->getName() => $aggregation],
                ];
            }

            $aggregations[$bucket->getName()] = $aggregation;
        }

        return $aggregations;
    }
1038727
public function getAggregation($name)
    {
        $aggregations = $this->getAggregations();
        if (!isset($aggregations[$name])) {
            throw new InvalidCallException("Aggregation '{$name}' does not present.");
        }
        return $aggregations[$name];
    }
1177120
public function addAggregate($agg) {
        $aggregate = new Aggregate($agg);
        if ($this->searchParams->aggregates == null) {
            $this->searchParams->aggregates = array();
        }
        $this->searchParams->aggregates[] = $aggregate;
    }
793274
private function buildAggQueryData($query)
    {
        $ret = [];

        foreach ($this->aggregations as $agg) {
            $additions = $agg->buildQueryData($this, $query);
            if ($additions) {
                $ret = array_merge($ret, $additions);
            }
        }

        return $ret;
    }
1301416
public function createAggregation(string $aggregation)
    {
        $schema = $this->schema[DocumentEntity::SH_AGGREGATIONS][$aggregation];

        //Let's create selector
        $selector = $this->odm->selector($schema[1]);

        //Ensure selection query
        $selector = $this->configureSelector($selector, $schema);

        if ($schema[0] == DocumentEntity::ONE) {
            return $selector->findOne();
        }

        return $selector;
    }
411592
protected function buildSelectFromAggregation(Aggregation $aggregation)
    {
        $tableAlias = $this->getAlias();
        $rawColumns = $aggregation->getFields();
        $newSelect = $this->getLookupBuilder()->buildJoin($this, $rawColumns);
        if (false === $newSelect) {
            if (empty($tableAlias) || '*' === $rawColumns) {
                $columns = $rawColumns;
            } else {
                $columns = $tableAlias.'.'.$rawColumns;
            }
        } else {
            list($alias, $joinColumn) = $newSelect;
            $columns = $alias.'.'.$joinColumn;
        }
        $fieldsSql = $this->buildColumns($columns);
        $aggregation->setFieldsSql($fieldsSql);

        return $this->getAdapter()->quoteSql($aggregation->toSQL());
    }
1168793
private function getSubAggregations($rawValue)
    {
        $subAggregations = [];

        foreach ($rawValue as $key => $value) {
            if (is_array($value)) {
                while (is_array($value) && isset($value[$key]) && is_array($value[$key])) {
                    $value = $value[$key];
                }

                if (isset($value['buckets'])) {
                    $subAggregations[$key] = $value;
                }
            }
        }

        return $this->create($subAggregations);
    }
719983
public function getAggregation($name)
    {
        if ($this->aggregations && $this->aggregations->has($name)) {
            return $this->aggregations->get($name);
        } else {
            return null;
        }
    }
149816
public function getAggregation(
        ConnectionInterface $connection,
        $addedAggregateNames = []
    ) {
        if (!$this->annotations) {
            return [];
        }
        $hasLimit = ($this->offset || $this->limit);
        $hasExistingAnnotations = false;
        foreach ($this->annotations as $alias => $annotation) {
            $hasExistingAnnotations = ($hasExistingAnnotations ||
                !in_array($alias, $addedAggregateNames));
        }

        // we have of $hasExistingAnnotations  we need to make the core query a
        // subquery and aggregate over it.
        if ($hasExistingAnnotations || $hasLimit || $this->distinct ||
            is_array($this->groupBy)) {
            $outQuery = new AggregateQuery($this->model);
            $innerQuery = $this->deepClone();

            $innerQuery->selectRelected = false;

            if (!$hasLimit && !$this->distinctFields) {
                // Queries with distinct_fields need ordering and when a limit
                // is applied we must take the slice from the ordered query.
                // Otherwise no need for ordering, so clear.
                $innerQuery->clearOrdering(true);
            }

            if (!$innerQuery->distinct) {
                // if we are using default columns and we already have
                // aggregate annotations existing
                // then we must make sure the inner
                // query is grouped by the main model's primary key. However,
                // clearing the select clause can alter results if distinct is
                // used.
                if ($innerQuery->useDefaultCols && $hasExistingAnnotations) {
                    $innerQuery->groupBy = [
                        $this->getMeta()->primaryKey
                            ->getColExpression($innerQuery->getInitialAlias()),
                    ];
                }
                $innerQuery->useDefaultCols = false;
            }

            // add annotations to the outerquery todo
            foreach ($innerQuery->annotations as $alias => $annotation) {
                if ($annotation->isSummarize()) {
                    $outQuery->annotations[$alias] = $annotation;
                    unset($innerQuery->annotations[$alias]);
                }
            }

            if ($innerQuery->select == [] && !$innerQuery->useDefaultCols) {
                $innerQuery->select = [
                    $this->getMeta()->primaryKey->getColExpression(
                        $innerQuery->getInitialAlias()
                    ),
                ];
            }

            $outQuery->addSubQuery($innerQuery, $connection);
        } else {
            $outQuery = $this;
            $outQuery->select = [];
            $outQuery->useDefaultCols = false;
        }
        $outQuery->clearOrdering(true);
        $outQuery->clearLimits();
        $outQuery->selectRelected = false;

        $results = $outQuery->getSqlCompiler($connection)
            ->executeSql()->fetch();

        $result = [];
        foreach (array_combine(
                     array_keys($outQuery->annotations),
                     array_values($results)
                 ) as $key => $item) {
            $result[$key] = $item;
        }

        return $result;
    }
554696
protected static function parseOrders(array $rawOrders) {

        $dtOrders = [];

        foreach ($rawOrders as $current) {
            $dtOrders[] = static::parseOrder($current);
        }

        return $dtOrders;
    }
1179286
public function toArray(): array
    {
        $result = [
            $this->type => $this->script ? ['script' => $this->script] : array_merge(['field' => $this->field], $this->additionalParams),
        ];

        if ($this->subAggregations) {
            $result['aggs'] = $this->subAggregations;
        }

        return $result;
    }
921469
public function getMessagesByAggregate($aggregateUuid): array
    {
        $messagesByUuid = [];
        /** @var Message $message */
        foreach ($this->messages as $message) {
            if ($message->aggregateUuid === $aggregateUuid) {
                $messagesByUuid[] = $message;
            }
        }

        return $messagesByUuid;
    }
322239
public function rawFilter($filters = [])
    {
        $filters = is_array($filters) ? $filters : func_get_args();

        foreach ($filters as $filter) {
            $this->filters['raw'][] = $filter;
        }

        return $this;
    }