1 | <?php |
||
14 | class ResultSet implements \Iterator, \Countable, \ArrayAccess |
||
15 | { |
||
16 | /** |
||
17 | * Current position. |
||
18 | * |
||
19 | * @var int Current position |
||
20 | */ |
||
21 | private $_position = 0; |
||
22 | |||
23 | /** |
||
24 | * Query. |
||
25 | * |
||
26 | * @var Query Query object |
||
27 | */ |
||
28 | private $_query; |
||
29 | |||
30 | /** |
||
31 | * Response. |
||
32 | * |
||
33 | * @var Response Response object |
||
34 | */ |
||
35 | private $_response; |
||
36 | |||
37 | /** |
||
38 | * Results. |
||
39 | * |
||
40 | * @var Result[] Results |
||
41 | */ |
||
42 | private $_results = []; |
||
43 | |||
44 | /** |
||
45 | * Constructs ResultSet object. |
||
46 | * |
||
47 | * @param Response $response Response object |
||
48 | * @param Query $query Query object |
||
49 | * @param Result[] $results |
||
50 | */ |
||
51 | public function __construct(Response $response, Query $query, $results) |
||
57 | |||
58 | /** |
||
59 | * Returns all results. |
||
60 | * |
||
61 | * @return Result[] Results |
||
62 | */ |
||
63 | public function getResults() |
||
67 | |||
68 | /** |
||
69 | * Returns all Documents. |
||
70 | * |
||
71 | * @return array Documents \Elastica\Document |
||
72 | */ |
||
73 | public function getDocuments() |
||
82 | |||
83 | /** |
||
84 | * Returns true if the response contains suggestion results; false otherwise. |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | public function hasSuggests() |
||
94 | |||
95 | /** |
||
96 | * Return all suggests. |
||
97 | * |
||
98 | * @return array suggest results |
||
99 | */ |
||
100 | public function getSuggests() |
||
101 | { |
||
102 | $data = $this->_response->getData(); |
||
103 | |||
104 | return $data['suggest'] ?? []; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns whether aggregations exist. |
||
109 | * |
||
110 | * @return bool Aggregation existence |
||
111 | */ |
||
112 | public function hasAggregations() |
||
118 | |||
119 | /** |
||
120 | * Returns all aggregation results. |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | public function getAggregations() |
||
125 | { |
||
126 | $data = $this->_response->getData(); |
||
127 | |||
128 | return $data['aggregations'] ?? []; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Retrieve a specific aggregation from this result set. |
||
133 | * |
||
134 | * @param string $name the name of the desired aggregation |
||
135 | * |
||
136 | * @throws Exception\InvalidException if an aggregation by the given name cannot be found |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | public function getAggregation($name) |
||
149 | |||
150 | /** |
||
151 | * Returns the total number of found hits. |
||
152 | * |
||
153 | * @return int Total hits |
||
154 | */ |
||
155 | public function getTotalHits() |
||
156 | { |
||
157 | $data = $this->_response->getData(); |
||
158 | |||
159 | return (int) ($data['hits']['total'] ?? 0); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Returns the max score of the results found. |
||
164 | * |
||
165 | * @return float Max Score |
||
166 | */ |
||
167 | public function getMaxScore() |
||
168 | { |
||
169 | $data = $this->_response->getData(); |
||
170 | |||
171 | return (float) ($data['hits']['max_score'] ?? 0); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns the total number of ms for this search to complete. |
||
176 | * |
||
177 | * @return int Total time |
||
178 | */ |
||
179 | public function getTotalTime() |
||
180 | { |
||
181 | $data = $this->_response->getData(); |
||
182 | |||
183 | return $data['took'] ?? 0; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns true if the query has timed out. |
||
188 | * |
||
189 | * @return bool Timed out |
||
190 | */ |
||
191 | public function hasTimedOut() |
||
197 | |||
198 | /** |
||
199 | * Returns response object. |
||
200 | * |
||
201 | * @return Response Response object |
||
202 | */ |
||
203 | public function getResponse() |
||
207 | |||
208 | /** |
||
209 | * @return Query |
||
210 | */ |
||
211 | public function getQuery() |
||
215 | |||
216 | /** |
||
217 | * Returns size of current set. |
||
218 | * |
||
219 | * @return int Size of set |
||
220 | */ |
||
221 | public function count() |
||
225 | |||
226 | /** |
||
227 | * Returns size of current suggests. |
||
228 | * |
||
229 | * @return int Size of suggests |
||
230 | */ |
||
231 | public function countSuggests() |
||
235 | |||
236 | /** |
||
237 | * Returns the current object of the set. |
||
238 | * |
||
239 | * @return \Elastica\Result|bool Set object or false if not valid (no more entries) |
||
240 | */ |
||
241 | public function current() |
||
249 | |||
250 | /** |
||
251 | * Sets pointer (current) to the next item of the set. |
||
252 | */ |
||
253 | public function next() |
||
259 | |||
260 | /** |
||
261 | * Returns the position of the current entry. |
||
262 | * |
||
263 | * @return int Current position |
||
264 | */ |
||
265 | public function key() |
||
269 | |||
270 | /** |
||
271 | * Check if an object exists at the current position. |
||
272 | * |
||
273 | * @return bool True if object exists |
||
274 | */ |
||
275 | public function valid() |
||
279 | |||
280 | /** |
||
281 | * Resets position to 0, restarts iterator. |
||
282 | */ |
||
283 | public function rewind() |
||
287 | |||
288 | /** |
||
289 | * Whether a offset exists. |
||
290 | * |
||
291 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
292 | * |
||
293 | * @param int $offset |
||
294 | * |
||
295 | * @return bool true on success or false on failure. |
||
296 | */ |
||
297 | public function offsetExists($offset) |
||
301 | |||
302 | /** |
||
303 | * Offset to retrieve. |
||
304 | * |
||
305 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
306 | * |
||
307 | * @param int $offset |
||
308 | * |
||
309 | * @throws Exception\InvalidException If offset doesn't exist |
||
310 | * |
||
311 | * @return Result|null |
||
312 | */ |
||
313 | public function offsetGet($offset) |
||
321 | |||
322 | /** |
||
323 | * Offset to set. |
||
324 | * |
||
325 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
326 | * |
||
327 | * @param int $offset |
||
328 | * @param Result $value |
||
329 | * |
||
330 | * @throws Exception\InvalidException |
||
331 | */ |
||
332 | public function offsetSet($offset, $value) |
||
344 | |||
345 | /** |
||
346 | * Offset to unset. |
||
347 | * |
||
348 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
349 | * |
||
350 | * @param int $offset |
||
351 | */ |
||
352 | public function offsetUnset($offset) |
||
356 | } |
||
357 |