Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace IQDEV\ElasticSearch\Search\Aggs;
use IQDEV\ElasticSearch\Esable;
use IQDEV\ElasticSearch\Search\BoolQuery\Query;
use IQDEV\ElasticSearch\Search\Nested;
final class Aggs implements Esable
{
private ?AggsCollection $aggs = null;
private ?Query $query = null;
private ?Nested $nested = null;
private ?Terms $terms = null;
private ?ExtremumTerms $extremumTerms = null;
private string $key;
public function __construct(string $key)
{
$this->key = $key;
}
/**
* @param Aggs|null $aggs
* @return Aggs
*/
public function addAggs(?Aggs $aggs): self
{
if ($this->aggs === null) {
$this->aggs = new AggsCollection();
}
$this->aggs->add($aggs);
return $this;
}
/**
* @param Query|null $query
* @return Aggs
*/
public function setQuery(?Query $query): self
{
$this->query = $query;
return $this;
}
/**
* @param Nested|null $nested
* @return Aggs
*/
public function setNested(?Nested $nested): self
{
$this->nested = $nested;
return $this;
}
/**
* @param Terms|null $terms
* @return Aggs
*/
public function setTerms(?Terms $terms): self
{
$this->terms = $terms;
return $this;
}
/**
* @param ExtremumTerms|null $terms
* @return Aggs
*/
public function setExtremumTerms(?ExtremumTerms $terms): self
{
$this->extremumTerms = $terms;
return $this;
}
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
public function es(): array
{
$agg = [];
if ($this->aggs) {
$agg['aggs'] = array_merge($agg, $this->aggs->es()['aggs']);
}
if (isset($this->query) && $this->query->isEmpty() === false) {
$agg['filter'] = $this->query->es()[$this->query->getType()];
}
if ($this->nested) {
$agg['nested'] = $this->nested->es()['nested'];
}
if ($this->terms) {
$agg['terms'] = $this->terms->es()['terms'];
}
if ($this->extremumTerms) {
$agg = array_merge($agg, $this->extremumTerms->es());
}
return $agg;
}
}