Skip to content

Commit 3b038ed

Browse files
author
Petr Gala
committed
Add support for filter in aggregation collection
1 parent 08c2483 commit 3b038ed

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Aggregation/LeafAggregationCollection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ public function toArray() : array
6262

6363
foreach ($this->aggregations as $aggregation) {
6464
if ($aggregation instanceof \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection) {
65-
$array[$this->key()]['aggs'] = $aggregation->toArray();
66-
65+
$array[$this->key()] = [
66+
'aggs' => $aggregation->toArray(),
67+
'filter' => $this->filter->toArray(),
68+
];
6769
} else {
6870
$array[$this->key()] = $array + $aggregation->toArray();
6971
}

src/Response/ResultMapper.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,27 @@ private function mapAggregation(
182182
, array $aggregationArray
183183
) : \Spameri\ElasticQuery\Response\Result\Aggregation
184184
{
185+
$i = 0;
185186
$buckets = [];
186187
$aggregations = [];
187-
foreach ($aggregationArray['buckets'] as $bucketPosition => $bucket) {
188-
$buckets[] = $this->mapBucket($bucketPosition, $bucket);
188+
189+
if (isset($aggregationArray['buckets'])) {
190+
foreach ($aggregationArray['buckets'] as $bucketPosition => $bucket) {
191+
$buckets[] = $this->mapBucket($bucketPosition, $bucket);
192+
}
193+
}
194+
195+
if (isset($aggregationArray['doc_count']) && $aggregationArray['doc_count'] > 0) {
196+
foreach ($aggregationArray as $aggregationName => $aggregation) {
197+
if ( ! \is_array($aggregation)) {
198+
continue;
199+
}
200+
$aggregations[] = $this->mapAggregation($aggregationName, $i, $aggregation);
201+
$i++;
202+
}
189203
}
190204

191205
if (isset($aggregationArray['aggregations'])) {
192-
$i = 0;
193206
foreach ($aggregationArray['aggregations'] as $aggregationName => $aggregation) {
194207
$aggregations[] = $this->mapAggregation($aggregationName, $i, $aggregation);
195208
$i++;

0 commit comments

Comments
 (0)