From 3b038edb2a0eaaadafb985e0d96545d6b98db01c Mon Sep 17 00:00:00 2001 From: Petr Gala Date: Fri, 6 Dec 2019 12:32:43 +0100 Subject: [PATCH] Add support for filter in aggregation collection --- src/Aggregation/LeafAggregationCollection.php | 6 ++++-- src/Response/ResultMapper.php | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Aggregation/LeafAggregationCollection.php b/src/Aggregation/LeafAggregationCollection.php index 038a742..c10ced2 100644 --- a/src/Aggregation/LeafAggregationCollection.php +++ b/src/Aggregation/LeafAggregationCollection.php @@ -62,8 +62,10 @@ public function toArray() : array foreach ($this->aggregations as $aggregation) { if ($aggregation instanceof \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection) { - $array[$this->key()]['aggs'] = $aggregation->toArray(); - + $array[$this->key()] = [ + 'aggs' => $aggregation->toArray(), + 'filter' => $this->filter->toArray(), + ]; } else { $array[$this->key()] = $array + $aggregation->toArray(); } diff --git a/src/Response/ResultMapper.php b/src/Response/ResultMapper.php index 0536d9f..eb5e6a3 100644 --- a/src/Response/ResultMapper.php +++ b/src/Response/ResultMapper.php @@ -182,14 +182,27 @@ private function mapAggregation( , array $aggregationArray ) : \Spameri\ElasticQuery\Response\Result\Aggregation { + $i = 0; $buckets = []; $aggregations = []; - foreach ($aggregationArray['buckets'] as $bucketPosition => $bucket) { - $buckets[] = $this->mapBucket($bucketPosition, $bucket); + + if (isset($aggregationArray['buckets'])) { + foreach ($aggregationArray['buckets'] as $bucketPosition => $bucket) { + $buckets[] = $this->mapBucket($bucketPosition, $bucket); + } + } + + if (isset($aggregationArray['doc_count']) && $aggregationArray['doc_count'] > 0) { + foreach ($aggregationArray as $aggregationName => $aggregation) { + if ( ! \is_array($aggregation)) { + continue; + } + $aggregations[] = $this->mapAggregation($aggregationName, $i, $aggregation); + $i++; + } } if (isset($aggregationArray['aggregations'])) { - $i = 0; foreach ($aggregationArray['aggregations'] as $aggregationName => $aggregation) { $aggregations[] = $this->mapAggregation($aggregationName, $i, $aggregation); $i++;