1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Es\Dsl\Dsl; |
3
|
|
|
|
4
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
5
|
|
|
use Triadev\Es\Dsl\Dsl\Aggregation\Bucketing; |
6
|
|
|
use Triadev\Es\Dsl\Dsl\Aggregation\Metric; |
7
|
|
|
use Triadev\Es\Dsl\Dsl\Aggregation\Pipeline; |
8
|
|
|
|
9
|
|
|
class Aggregation |
10
|
|
|
{ |
11
|
|
|
/** @var \ONGR\ElasticsearchDSL\Search */ |
12
|
|
|
private $search; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Aggregation constructor. |
16
|
|
|
* @param \ONGR\ElasticsearchDSL\Search $search |
17
|
|
|
*/ |
18
|
41 |
|
public function __construct(\ONGR\ElasticsearchDSL\Search $search) |
19
|
|
|
{ |
20
|
41 |
|
$this->search = $search; |
21
|
41 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Bucketing |
25
|
|
|
* |
26
|
|
|
* @param \Closure $bucketing |
27
|
|
|
* @return Aggregation |
28
|
|
|
*/ |
29
|
17 |
|
public function bucketing(\Closure $bucketing) : Aggregation |
30
|
|
|
{ |
31
|
17 |
|
$bucketingBuilder = new Bucketing(); |
32
|
17 |
|
$bucketing($bucketingBuilder); |
33
|
|
|
|
34
|
17 |
|
$this->append($bucketingBuilder->getAggregations()); |
35
|
17 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Metric |
40
|
|
|
* |
41
|
|
|
* @param \Closure $metric |
42
|
|
|
* @return Aggregation |
43
|
|
|
*/ |
44
|
12 |
|
public function metric(\Closure $metric) : Aggregation |
45
|
|
|
{ |
46
|
12 |
|
$metricBuilder = new Metric(); |
47
|
12 |
|
$metric($metricBuilder); |
48
|
|
|
|
49
|
12 |
|
$this->append($metricBuilder->getAggregations()); |
50
|
12 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Pipeline |
55
|
|
|
* |
56
|
|
|
* @param \Closure $pipeline |
57
|
|
|
* @return Aggregation |
58
|
|
|
*/ |
59
|
12 |
|
public function pipeline(\Closure $pipeline) : Aggregation |
60
|
|
|
{ |
61
|
12 |
|
$pipelineBuilder = new Pipeline(); |
62
|
12 |
|
$pipeline($pipelineBuilder); |
63
|
|
|
|
64
|
12 |
|
$this->append($pipelineBuilder->getAggregations()); |
65
|
12 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param AbstractAggregation[] $aggs |
70
|
|
|
* @return Aggregation |
71
|
|
|
*/ |
72
|
41 |
|
private function append(array $aggs) : Aggregation |
73
|
|
|
{ |
74
|
41 |
|
foreach ($aggs as $agg) { |
75
|
41 |
|
if ($agg instanceof AbstractAggregation) { |
76
|
41 |
|
$this->search->addAggregation($agg); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
41 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|