|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Aggregation; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Exception\InvalidException; |
|
6
|
|
|
use Elastica\NameableInterface; |
|
7
|
|
|
use Elastica\Param; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractAggregation extends Param implements NameableInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected const METADATA_KEY = 'meta'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string The name of this aggregation |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $_name; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array Subaggregations belonging to this aggregation |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $_aggs = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $name the name of this aggregation |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(string $name) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setName($name); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set the name of this aggregation. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $name |
|
35
|
|
|
* |
|
36
|
|
|
* @return $this |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setName(string $name): NameableInterface |
|
39
|
|
|
{ |
|
40
|
|
|
$this->_name = $name; |
|
41
|
|
|
|
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Retrieve the name of this aggregation. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getName(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->_name; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Retrieve all subaggregations belonging to this aggregation. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getAggs(): array |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->_aggs; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add a sub-aggregation. |
|
67
|
|
|
* |
|
68
|
|
|
* @param AbstractAggregation $aggregation |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \Elastica\Exception\InvalidException |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addAggregation(AbstractAggregation $aggregation): self |
|
75
|
|
|
{ |
|
76
|
|
|
if ($aggregation instanceof GlobalAggregation) { |
|
77
|
|
|
throw new InvalidException('Global aggregators can only be placed as top level aggregators'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->_aggs[] = $aggregation; |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Add metadata to the aggregation. |
|
87
|
|
|
* |
|
88
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html |
|
89
|
|
|
* @see \Elastica\Aggregation\AbstractAggregation::getMeta() |
|
90
|
|
|
* @see \Elastica\Aggregation\AbstractAggregation::clearMeta() |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $meta Metadata to be attached to the aggregation |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setMeta(array $meta): self |
|
97
|
|
|
{ |
|
98
|
|
|
if (empty($meta)) { |
|
99
|
|
|
return $this->clearMeta(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->_setRawParam(self::METADATA_KEY, $meta); |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Retrieve the currently configured metadata for the aggregation. |
|
109
|
|
|
* |
|
110
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html |
|
111
|
|
|
* @see \Elastica\Aggregation\AbstractAggregation::setMeta() |
|
112
|
|
|
* @see \Elastica\Aggregation\AbstractAggregation::clearMeta() |
|
113
|
|
|
* |
|
114
|
|
|
* @return array|null |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getMeta(): ?array |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->_rawParams[self::METADATA_KEY] ?? null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Clears any previously set metadata for this aggregation. |
|
123
|
|
|
* |
|
124
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html |
|
125
|
|
|
* @see \Elastica\Aggregation\AbstractAggregation::setMeta() |
|
126
|
|
|
* @see \Elastica\Aggregation\AbstractAggregation::getMeta() |
|
127
|
|
|
* |
|
128
|
|
|
* @return $this |
|
129
|
|
|
*/ |
|
130
|
|
|
public function clearMeta(): self |
|
131
|
|
|
{ |
|
132
|
|
|
unset($this->_rawParams[self::METADATA_KEY]); |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
public function toArray(): array |
|
141
|
|
|
{ |
|
142
|
|
|
$array = parent::toArray(); |
|
143
|
|
|
|
|
144
|
|
|
if (\array_key_exists('global_aggregation', $array)) { |
|
145
|
|
|
// compensate for class name GlobalAggregation |
|
146
|
|
|
$array = ['global' => new \stdClass()]; |
|
147
|
|
|
} |
|
148
|
|
|
if (\sizeof($this->_aggs)) { |
|
149
|
|
|
$array['aggs'] = $this->_convertArrayable($this->_aggs); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $array; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|