Skip to content

Commit be78202

Browse files
committed
Aggregations
- fixed filter - getter in collection - buckets from named aggregations
1 parent 06be8a5 commit be78202

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

src/Aggregation/LeafAggregationCollection.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ public function toArray() : array
6262

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

72+
if ($this->filter) {
73+
$array[$this->key()]['filter'] = $this->filter->toArray();
74+
}
75+
7476
return $array;
7577
}
7678

src/Response/Result/Aggregation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function position() : int
5050
}
5151

5252

53-
public function bucketCollection() : \Spameri\ElasticQuery\Response\Result\Aggregation\BucketCollection
53+
public function buckets() : \Spameri\ElasticQuery\Response\Result\Aggregation\BucketCollection
5454
{
5555
return $this->bucketCollection;
5656
}

src/Response/Result/AggregationCollection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,18 @@ public function getIterator() : \ArrayIterator
2525
return new \ArrayIterator($this->aggregations);
2626
}
2727

28+
29+
public function getAggregation(
30+
string $name
31+
): ?\Spameri\ElasticQuery\Response\Result\Aggregation
32+
{
33+
foreach ($this->aggregations as $aggregation) {
34+
if ($aggregation->name() === $name) {
35+
return $aggregation;
36+
}
37+
}
38+
39+
return NULL;
40+
}
41+
2842
}

src/Response/ResultMapper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ private function mapAggregation(
192192
}
193193
}
194194

195+
if (isset($aggregationArray[$name]['buckets'])) {
196+
foreach ($aggregationArray[$name]['buckets'] as $bucketPosition => $bucket) {
197+
$buckets[] = $this->mapBucket($bucketPosition, $bucket);
198+
}
199+
}
200+
195201
if (isset($aggregationArray['doc_count']) && $aggregationArray['doc_count'] > 0) {
196202
foreach ($aggregationArray as $aggregationName => $aggregation) {
197203
if ( ! \is_array($aggregation)) {
@@ -230,7 +236,9 @@ private function mapBucket(
230236
return new \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket(
231237
$bucketArray['key'] ?? $bucketPosition,
232238
$bucketArray['doc_count'],
233-
\is_int($bucketPosition) ? $bucketPosition : NULL
239+
\is_int($bucketPosition) ? $bucketPosition : NULL,
240+
$bucketArray['from'] ?? NULL,
241+
$bucketArray['to'] ?? NULL
234242
);
235243
}
236244

tests/SpameriTests/ElasticQuery/Response/Result.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Result extends \Tester\TestCase
9393
\Tester\Assert::same(0, $aggregation->position());
9494
\Tester\Assert::same('guessedRight', $aggregation->name());
9595
/** @var \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket $bucket */
96-
foreach ($aggregation->bucketCollection() as $bucket) {
96+
foreach ($aggregation->buckets() as $bucket) {
9797
if ($bucket->position() === 0) {
9898
\Tester\Assert::same(0, $bucket->position());
9999
\Tester\Assert::same('7kASSmUBq9pZLj7-1Uv4', $bucket->key());

0 commit comments

Comments
 (0)