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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation\Metric; |
15
|
|
|
|
16
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
17
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; |
18
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
19
|
|
|
use stdClass; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Top hits aggregation. |
23
|
|
|
* |
24
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html |
25
|
|
|
*/ |
26
|
|
|
class TopHitsAggregation extends AbstractAggregation |
27
|
|
|
{ |
28
|
|
|
use MetricTrait; |
29
|
|
|
|
30
|
|
|
private array $sorts = []; |
|
|
|
|
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
private string $name, |
34
|
|
|
private ?int $size = null, |
35
|
|
|
private ?int $from = null, |
36
|
|
|
?BuilderInterface $sort = null |
37
|
|
|
) { |
38
|
|
|
parent::__construct($name); |
39
|
|
|
$this->setFrom($from); |
40
|
|
|
$this->setSize($size); |
41
|
|
|
$this->addSort($sort); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getFrom(): ?int |
45
|
|
|
{ |
46
|
|
|
return $this->from; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setFrom(?int $from): static |
50
|
|
|
{ |
51
|
|
|
$this->from = $from; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getSorts(): array |
57
|
|
|
{ |
58
|
|
|
return $this->sorts; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setSorts(array $sorts): static |
62
|
|
|
{ |
63
|
|
|
$this->sorts = $sorts; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function addSort(?BuilderInterface $sort): void |
69
|
|
|
{ |
70
|
|
|
$this->sorts[] = $sort; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setSize(?int $size): static |
74
|
|
|
{ |
75
|
|
|
$this->size = $size; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getSize(): ?int |
81
|
|
|
{ |
82
|
|
|
return $this->size; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getType(): string |
86
|
|
|
{ |
87
|
|
|
return 'top_hits'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getArray(): array|stdClass |
91
|
|
|
{ |
92
|
|
|
$sortsOutput = null; |
93
|
|
|
$addedSorts = array_filter($this->getSorts()); |
94
|
|
|
|
95
|
|
|
if ($addedSorts) { |
96
|
|
|
$sortsOutput = []; |
97
|
|
|
foreach ($addedSorts as $sort) { |
98
|
|
|
$sortsOutput[] = $sort->toArray(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$output = array_filter( |
103
|
|
|
[ |
104
|
|
|
'sort' => $sortsOutput, |
105
|
|
|
'size' => $this->getSize(), |
106
|
|
|
'from' => $this->getFrom(), |
107
|
|
|
], |
108
|
|
|
fn(mixed $val): bool => (($val || is_array($val) || ($val || is_numeric($val)))) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $output ?? new stdClass(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getSort(): ?BuilderInterface |
115
|
|
|
{ |
116
|
|
|
if (isset($this->sorts[0])) { |
117
|
|
|
return $this->sorts[0]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setSort(BuilderInterface $sort): static |
124
|
|
|
{ |
125
|
|
|
$this->sort = $sort; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|