Completed
Push — master ( e62898...ae80d3 )
by Nicolas
01:44
created

ResultSet::getTotalHitsRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * Current position.
19
     *
20
     * @var int Current position
21
     */
22
    private $_position = 0;
23
24
    /**
25
     * Query.
26
     *
27
     * @var Query Query object
28
     */
29
    private $_query;
30
31
    /**
32
     * Response.
33
     *
34
     * @var Response Response object
35
     */
36
    private $_response;
37
38
    /**
39
     * Results.
40
     *
41
     * @var Result[] Results
42
     */
43
    private $_results = [];
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 = [];
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 $data['suggest'] ?? [];
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 $data['aggregations'] ?? [];
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
        $data = $this->_response->getData();
159
160
        return (int) ($data['hits']['total']['value'] ?? 0);
161
    }
162
163
    /**
164
     * Returns the total number relation of found hits.
165
     */
166
    public function getTotalHitsRelation(): string
167
    {
168
        $data = $this->_response->getData();
169
170
        return $data['hits']['total']['relation'] ?? 'eq';
171
    }
172
173
    /**
174
     * Returns the max score of the results found.
175
     *
176
     * @return float Max Score
177
     */
178
    public function getMaxScore()
179
    {
180
        $data = $this->_response->getData();
181
182
        return (float) ($data['hits']['max_score'] ?? 0);
183
    }
184
185
    /**
186
     * Returns the total number of ms for this search to complete.
187
     *
188
     * @return int Total time
189
     */
190
    public function getTotalTime()
191
    {
192
        $data = $this->_response->getData();
193
194
        return $data['took'] ?? 0;
195
    }
196
197
    /**
198
     * Returns true if the query has timed out.
199
     *
200
     * @return bool Timed out
201
     */
202
    public function hasTimedOut()
203
    {
204
        $data = $this->_response->getData();
205
206
        return !empty($data['timed_out']);
207
    }
208
209
    /**
210
     * Returns response object.
211
     *
212
     * @return Response Response object
213
     */
214
    public function getResponse()
215
    {
216
        return $this->_response;
217
    }
218
219
    /**
220
     * @return Query
221
     */
222
    public function getQuery()
223
    {
224
        return $this->_query;
225
    }
226
227
    /**
228
     * Returns size of current set.
229
     *
230
     * @return int Size of set
231
     */
232
    public function count()
233
    {
234
        return \count($this->_results);
235
    }
236
237
    /**
238
     * Returns size of current suggests.
239
     *
240
     * @return int Size of suggests
241
     */
242
    public function countSuggests()
243
    {
244
        return \sizeof($this->getSuggests());
245
    }
246
247
    /**
248
     * Returns the current object of the set.
249
     *
250
     * @return \Elastica\Result Set object
251
     */
252
    public function current()
253
    {
254
        return $this->_results[$this->key()];
255
    }
256
257
    /**
258
     * Sets pointer (current) to the next item of the set.
259
     */
260
    public function next()
261
    {
262
        ++$this->_position;
263
    }
264
265
    /**
266
     * Returns the position of the current entry.
267
     *
268
     * @return int Current position
269
     */
270
    public function key()
271
    {
272
        return $this->_position;
273
    }
274
275
    /**
276
     * Check if an object exists at the current position.
277
     *
278
     * @return bool True if object exists
279
     */
280
    public function valid()
281
    {
282
        return isset($this->_results[$this->key()]);
283
    }
284
285
    /**
286
     * Resets position to 0, restarts iterator.
287
     */
288
    public function rewind()
289
    {
290
        $this->_position = 0;
291
    }
292
293
    /**
294
     * Whether a offset exists.
295
     *
296
     * @see http://php.net/manual/en/arrayaccess.offsetexists.php
297
     *
298
     * @param int $offset
299
     *
300
     * @return bool true on success or false on failure
301
     */
302
    public function offsetExists($offset)
303
    {
304
        return isset($this->_results[$offset]);
305
    }
306
307
    /**
308
     * Offset to retrieve.
309
     *
310
     * @see http://php.net/manual/en/arrayaccess.offsetget.php
311
     *
312
     * @param int $offset
313
     *
314
     * @throws Exception\InvalidException If offset doesn't exist
315
     *
316
     * @return Result
317
     */
318
    public function offsetGet($offset)
319
    {
320
        if ($this->offsetExists($offset)) {
321
            return $this->_results[$offset];
322
        }
323
324
        throw new InvalidException('Offset does not exist.');
325
    }
326
327
    /**
328
     * Offset to set.
329
     *
330
     * @see http://php.net/manual/en/arrayaccess.offsetset.php
331
     *
332
     * @param int    $offset
333
     * @param Result $value
334
     *
335
     * @throws Exception\InvalidException
336
     */
337
    public function offsetSet($offset, $value)
338
    {
339
        if (!($value instanceof Result)) {
340
            throw new InvalidException('ResultSet is a collection of Result only.');
341
        }
342
343
        if (!isset($this->_results[$offset])) {
344
            throw new InvalidException('Offset does not exist.');
345
        }
346
347
        $this->_results[$offset] = $value;
348
    }
349
350
    /**
351
     * Offset to unset.
352
     *
353
     * @see http://php.net/manual/en/arrayaccess.offsetunset.php
354
     *
355
     * @param int $offset
356
     */
357
    public function offsetUnset($offset)
358
    {
359
        unset($this->_results[$offset]);
360
    }
361
}
362