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\Metric; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
15
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; |
16
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Top hits aggregation. |
20
|
|
|
* |
21
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html |
22
|
|
|
*/ |
23
|
|
|
class TopHitsAggregation extends AbstractAggregation |
24
|
|
|
{ |
25
|
|
|
use MetricTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int Number of top matching hits to return per bucket. |
29
|
|
|
*/ |
30
|
|
|
private $size; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int The offset from the first result you want to fetch. |
34
|
|
|
*/ |
35
|
|
|
private $from; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var BuilderInterface How the top matching hits should be sorted. |
39
|
|
|
*/ |
40
|
|
|
private $sort; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor for top hits. |
44
|
|
|
* |
45
|
|
|
* @param string $name Aggregation name. |
46
|
|
|
* @param null|int $size Number of top matching hits to return per bucket. |
47
|
|
|
* @param null|int $from The offset from the first result you want to fetch. |
48
|
|
|
* @param null|BuilderInterface $sort How the top matching hits should be sorted. |
49
|
|
|
*/ |
50
|
|
|
public function __construct($name, $size = null, $from = null, $sort = null) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($name); |
53
|
|
|
$this->setFrom($from); |
54
|
|
|
$this->setSize($size); |
55
|
|
|
$this->setSort($sort); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return from. |
60
|
|
|
* |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function getFrom() |
64
|
|
|
{ |
65
|
|
|
return $this->from; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set from. |
70
|
|
|
* |
71
|
|
|
* @param int $from |
72
|
|
|
*/ |
73
|
|
|
public function setFrom($from) |
74
|
|
|
{ |
75
|
|
|
$this->from = $from; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return sort. |
80
|
|
|
* |
81
|
|
|
* @return BuilderInterface |
82
|
|
|
*/ |
83
|
|
|
public function getSort() |
84
|
|
|
{ |
85
|
|
|
return $this->sort; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set sort. |
90
|
|
|
* |
91
|
|
|
* @param BuilderInterface $sort |
92
|
|
|
*/ |
93
|
|
|
public function setSort($sort) |
94
|
|
|
{ |
95
|
|
|
$this->sort = $sort; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set size. |
100
|
|
|
* |
101
|
|
|
* @param int $size |
102
|
|
|
*/ |
103
|
|
|
public function setSize($size) |
104
|
|
|
{ |
105
|
|
|
$this->size = $size; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return size. |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function getSize() |
114
|
|
|
{ |
115
|
|
|
return $this->size; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getType() |
122
|
|
|
{ |
123
|
|
|
return 'top_hits'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getArray() |
130
|
|
|
{ |
131
|
|
|
$output = array_filter( |
132
|
|
|
[ |
133
|
|
|
'sort' => $this->getSort() ? $this->getSort()->toArray() : null, |
134
|
|
|
'size' => $this->getSize(), |
135
|
|
|
'from' => $this->getFrom(), |
136
|
|
|
], |
137
|
|
|
function ($val) { |
138
|
|
|
return (($val || is_array($val) || ($val || is_numeric($val)))); |
139
|
|
|
} |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$output = $this->processArray($output); |
143
|
|
|
|
144
|
|
|
return empty($output) ? new \stdClass() : $output; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):