1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica; |
4
|
|
|
|
5
|
|
|
use Elastica\Exception\InvalidException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Elastica result set. |
9
|
|
|
* |
10
|
|
|
* List of all hits that are returned for a search on elasticsearch |
11
|
|
|
* Result set implements iterator |
12
|
|
|
* |
13
|
|
|
* @author Nicolas Ruflin <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ResultSet implements \Iterator, \Countable, \ArrayAccess |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Results. |
19
|
|
|
* |
20
|
|
|
* @var Result[] Results |
21
|
|
|
*/ |
22
|
|
|
private $_results = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Current position. |
26
|
|
|
* |
27
|
|
|
* @var int Current position |
28
|
|
|
*/ |
29
|
|
|
private $_position = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Response. |
33
|
|
|
* |
34
|
|
|
* @var \Elastica\Response Response object |
35
|
|
|
*/ |
36
|
|
|
private $_response; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Query. |
40
|
|
|
* |
41
|
|
|
* @var \Elastica\Query Query object |
42
|
|
|
*/ |
43
|
|
|
private $_query; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructs ResultSet object. |
47
|
|
|
* |
48
|
|
|
* @param Response $response Response object |
49
|
|
|
* @param Query $query Query object |
50
|
|
|
* @param Result[] $results |
51
|
|
|
*/ |
52
|
|
|
public function __construct(Response $response, Query $query, $results) |
53
|
|
|
{ |
54
|
|
|
$this->_query = $query; |
55
|
|
|
$this->_response = $response; |
56
|
|
|
$this->_results = $results; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns all results. |
61
|
|
|
* |
62
|
|
|
* @return Result[] Results |
63
|
|
|
*/ |
64
|
|
|
public function getResults() |
65
|
|
|
{ |
66
|
|
|
return $this->_results; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns all Documents. |
71
|
|
|
* |
72
|
|
|
* @return array Documents \Elastica\Document |
73
|
|
|
*/ |
74
|
|
|
public function getDocuments() |
75
|
|
|
{ |
76
|
|
|
$documents = array(); |
77
|
|
|
foreach ($this->_results as $doc) { |
78
|
|
|
$documents[] = $doc->getDocument(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $documents; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns true if the response contains suggestion results; false otherwise. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasSuggests() |
90
|
|
|
{ |
91
|
|
|
$data = $this->_response->getData(); |
92
|
|
|
|
93
|
|
|
return isset($data['suggest']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return all suggests. |
98
|
|
|
* |
99
|
|
|
* @return array suggest results |
100
|
|
|
*/ |
101
|
|
|
public function getSuggests() |
102
|
|
|
{ |
103
|
|
|
$data = $this->_response->getData(); |
104
|
|
|
|
105
|
|
|
return isset($data['suggest']) ? $data['suggest'] : array(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns whether aggregations exist. |
110
|
|
|
* |
111
|
|
|
* @return bool Aggregation existence |
112
|
|
|
*/ |
113
|
|
|
public function hasAggregations() |
114
|
|
|
{ |
115
|
|
|
$data = $this->_response->getData(); |
116
|
|
|
|
117
|
|
|
return isset($data['aggregations']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns all aggregation results. |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getAggregations() |
126
|
|
|
{ |
127
|
|
|
$data = $this->_response->getData(); |
128
|
|
|
|
129
|
|
|
return isset($data['aggregations']) ? $data['aggregations'] : array(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieve a specific aggregation from this result set. |
134
|
|
|
* |
135
|
|
|
* @param string $name the name of the desired aggregation |
136
|
|
|
* |
137
|
|
|
* @throws Exception\InvalidException if an aggregation by the given name cannot be found |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getAggregation($name) |
142
|
|
|
{ |
143
|
|
|
$data = $this->_response->getData(); |
144
|
|
|
|
145
|
|
|
if (isset($data['aggregations']) && isset($data['aggregations'][$name])) { |
146
|
|
|
return $data['aggregations'][$name]; |
147
|
|
|
} |
148
|
|
|
throw new InvalidException("This result set does not contain an aggregation named {$name}."); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the total number of found hits. |
153
|
|
|
* |
154
|
|
|
* @return int Total hits |
155
|
|
|
*/ |
156
|
|
|
public function getTotalHits() |
157
|
|
|
{ |
158
|
|
|
return isset($result['hits']['total']) ? (int) $result['hits']['total'] : 0; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the max score of the results found. |
163
|
|
|
* |
164
|
|
|
* @return float Max Score |
165
|
|
|
*/ |
166
|
|
|
public function getMaxScore() |
167
|
|
|
{ |
168
|
|
|
return isset($result['hits']['max_score']) ? (float) $result['hits']['max_score'] : 0; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns the total number of ms for this search to complete. |
173
|
|
|
* |
174
|
|
|
* @return int Total time |
175
|
|
|
*/ |
176
|
|
|
public function getTotalTime() |
177
|
|
|
{ |
178
|
|
|
return isset($result['took']) ? $result['took'] : 0; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns true if the query has timed out. |
183
|
|
|
* |
184
|
|
|
* @return bool Timed out |
185
|
|
|
*/ |
186
|
|
|
public function hasTimedOut() |
187
|
|
|
{ |
188
|
|
|
return !empty($result['timed_out']); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns response object. |
193
|
|
|
* |
194
|
|
|
* @return \Elastica\Response Response object |
195
|
|
|
*/ |
196
|
|
|
public function getResponse() |
197
|
|
|
{ |
198
|
|
|
return $this->_response; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return \Elastica\Query |
203
|
|
|
*/ |
204
|
|
|
public function getQuery() |
205
|
|
|
{ |
206
|
|
|
return $this->_query; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns size of current set. |
211
|
|
|
* |
212
|
|
|
* @return int Size of set |
213
|
|
|
*/ |
214
|
|
|
public function count() |
215
|
|
|
{ |
216
|
|
|
return sizeof($this->_results); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns size of current suggests. |
221
|
|
|
* |
222
|
|
|
* @return int Size of suggests |
223
|
|
|
*/ |
224
|
|
|
public function countSuggests() |
225
|
|
|
{ |
226
|
|
|
return sizeof($this->getSuggests()); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns the current object of the set. |
231
|
|
|
* |
232
|
|
|
* @return \Elastica\Result|bool Set object or false if not valid (no more entries) |
233
|
|
|
*/ |
234
|
|
|
public function current() |
235
|
|
|
{ |
236
|
|
|
if ($this->valid()) { |
237
|
|
|
return $this->_results[$this->key()]; |
238
|
|
|
} else { |
239
|
|
|
return false; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Sets pointer (current) to the next item of the set. |
245
|
|
|
*/ |
246
|
|
|
public function next() |
247
|
|
|
{ |
248
|
|
|
++$this->_position; |
249
|
|
|
|
250
|
|
|
return $this->current(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns the position of the current entry. |
255
|
|
|
* |
256
|
|
|
* @return int Current position |
257
|
|
|
*/ |
258
|
|
|
public function key() |
259
|
|
|
{ |
260
|
|
|
return $this->_position; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Check if an object exists at the current position. |
265
|
|
|
* |
266
|
|
|
* @return bool True if object exists |
267
|
|
|
*/ |
268
|
|
|
public function valid() |
269
|
|
|
{ |
270
|
|
|
return isset($this->_results[$this->key()]); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Resets position to 0, restarts iterator. |
275
|
|
|
*/ |
276
|
|
|
public function rewind() |
277
|
|
|
{ |
278
|
|
|
$this->_position = 0; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Whether a offset exists. |
283
|
|
|
* |
284
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
285
|
|
|
* |
286
|
|
|
* @param int $offset |
287
|
|
|
* |
288
|
|
|
* @return bool true on success or false on failure. |
289
|
|
|
*/ |
290
|
|
|
public function offsetExists($offset) |
291
|
|
|
{ |
292
|
|
|
return isset($this->_results[$offset]); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Offset to retrieve. |
297
|
|
|
* |
298
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
299
|
|
|
* |
300
|
|
|
* @param int $offset |
301
|
|
|
* |
302
|
|
|
* @throws Exception\InvalidException If offset doesn't exist |
303
|
|
|
* |
304
|
|
|
* @return Result|null |
305
|
|
|
*/ |
306
|
|
|
public function offsetGet($offset) |
307
|
|
|
{ |
308
|
|
|
if ($this->offsetExists($offset)) { |
309
|
|
|
return $this->_results[$offset]; |
310
|
|
|
} else { |
311
|
|
|
throw new InvalidException('Offset does not exist.'); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Offset to set. |
317
|
|
|
* |
318
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
319
|
|
|
* |
320
|
|
|
* @param int $offset |
321
|
|
|
* @param Result $value |
322
|
|
|
* |
323
|
|
|
* @throws Exception\InvalidException |
324
|
|
|
*/ |
325
|
|
|
public function offsetSet($offset, $value) |
326
|
|
|
{ |
327
|
|
|
if (!($value instanceof Result)) { |
328
|
|
|
throw new InvalidException('ResultSet is a collection of Result only.'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if (!isset($this->_results[$offset])) { |
332
|
|
|
throw new InvalidException('Offset does not exist.'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$this->_results[$offset] = $value; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Offset to unset. |
340
|
|
|
* |
341
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
342
|
|
|
* |
343
|
|
|
* @param int $offset |
344
|
|
|
*/ |
345
|
|
|
public function offsetUnset($offset) |
346
|
|
|
{ |
347
|
|
|
unset($this->_results[$offset]); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.