|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\BuilderBag; |
|
15
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
|
16
|
|
|
use ONGR\ElasticsearchDSL\NameAwareTrait; |
|
17
|
|
|
use ONGR\ElasticsearchDSL\NamedBuilderInterface; |
|
18
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
|
19
|
|
|
use stdClass; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* AbstractAggregation class. |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractAggregation implements NamedBuilderInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ParametersTrait; |
|
27
|
|
|
|
|
28
|
|
|
use NameAwareTrait; |
|
29
|
|
|
|
|
30
|
|
|
private ?string $field = null; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
private ?BuilderBag $aggregations = null; |
|
33
|
|
|
|
|
34
|
|
|
abstract protected function supportsNesting(): bool; |
|
35
|
|
|
|
|
36
|
|
|
abstract protected function getArray(): array|stdClass|null; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(string $name) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->setName($name); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setField(?string $field): static |
|
44
|
|
|
{ |
|
45
|
|
|
$this->field = $field; |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getField(): ?string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->field; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function addAggregation(AbstractAggregation $abstractAggregation): static |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$this->aggregations) { |
|
58
|
|
|
$this->aggregations = $this->createBuilderBag(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->aggregations->add($abstractAggregation); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns all sub aggregations. |
|
68
|
|
|
* |
|
69
|
|
|
* @return BuilderBag[]|NamedBuilderInterface[] |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getAggregations(): array |
|
72
|
|
|
{ |
|
73
|
|
|
if ($this->aggregations) { |
|
74
|
|
|
return $this->aggregations->all(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return []; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getAggregation(string $name): AbstractAggregation|BuilderInterface|null |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->aggregations && $this->aggregations->has($name)) { |
|
83
|
|
|
return $this->aggregations->get($name); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function toArray(): array |
|
90
|
|
|
{ |
|
91
|
|
|
$array = $this->getArray(); |
|
92
|
|
|
$result = [ |
|
93
|
|
|
$this->getType() => is_array($array) ? $this->processArray($array) : $array, |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
if ($this->supportsNesting()) { |
|
97
|
|
|
$nestedResult = $this->collectNestedAggregations(); |
|
98
|
|
|
|
|
99
|
|
|
if (!empty($nestedResult)) { |
|
100
|
|
|
$result['aggregations'] = $nestedResult; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $result; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function collectNestedAggregations(): array |
|
108
|
|
|
{ |
|
109
|
|
|
$result = []; |
|
110
|
|
|
/** @var AbstractAggregation $aggregation */ |
|
111
|
|
|
foreach ($this->getAggregations() as $aggregation) { |
|
112
|
|
|
$result[$aggregation->getName()] = $aggregation->toArray(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $result; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function createBuilderBag(): BuilderBag |
|
119
|
|
|
{ |
|
120
|
|
|
return new BuilderBag(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|