|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Level23\Druid\Aggregations; |
|
5
|
|
|
|
|
6
|
|
|
use Level23\Druid\Filters\FilterInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class FilteredAggregator |
|
10
|
|
|
* |
|
11
|
|
|
* A filtered aggregator wraps any given aggregator, |
|
12
|
|
|
* but only aggregates the values for which the given dimension filter matches. |
|
13
|
|
|
* |
|
14
|
|
|
* This makes it possible to compute the results of a filtered and an unfiltered |
|
15
|
|
|
* aggregation simultaneously, without having to issue multiple queries, |
|
16
|
|
|
* and use both results as part of post-aggregations. |
|
17
|
|
|
* |
|
18
|
|
|
* Note: If only the filtered results are required, |
|
19
|
|
|
* consider putting the filter on the query itself, which will be much faster since |
|
20
|
|
|
* it does not require scanning all the data. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Level23\Druid\Aggregations |
|
23
|
|
|
*/ |
|
24
|
|
|
class FilteredAggregator implements AggregatorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
protected AggregatorInterface $aggregator; |
|
27
|
|
|
|
|
28
|
|
|
protected FilterInterface $filter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* CountAggregator constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Level23\Druid\Filters\FilterInterface $filter |
|
34
|
|
|
* @param \Level23\Druid\Aggregations\AggregatorInterface $aggregator |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function __construct(FilterInterface $filter, AggregatorInterface $aggregator) |
|
37
|
|
|
{ |
|
38
|
3 |
|
$this->filter = $filter; |
|
39
|
3 |
|
$this->aggregator = $aggregator; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Return the aggregator as it can be used in a druid query. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array<string,string|array<mixed>> |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function toArray(): array |
|
48
|
|
|
{ |
|
49
|
2 |
|
return [ |
|
50
|
2 |
|
'type' => 'filtered', |
|
51
|
2 |
|
'filter' => $this->filter->toArray(), |
|
52
|
2 |
|
'aggregator' => $this->aggregator->toArray(), |
|
53
|
2 |
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
} |