|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class SearchResults implements \Iterator |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var bool |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $hasTimedOut; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $maxScore; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $totalCount; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $totalTime; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Hit[] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $hits = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_index = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $aggregations = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $data |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(array $data) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->totalTime = isset($data['took']) ? $data['took'] : null; |
|
49
|
|
|
$this->hasTimedOut = isset($data['timed_out']) ? $data['timed_out'] : null; |
|
50
|
|
|
$this->aggregations = isset($data['aggregations']) ? $data['aggregations'] : []; |
|
51
|
|
|
|
|
52
|
|
|
if (isset($data['hits'])) { |
|
53
|
|
|
$this->totalCount = $data['hits']['total']; |
|
54
|
|
|
$this->maxScore = $data['hits']['max_score']; |
|
55
|
|
|
$this->buildHits($data['hits']['hits']); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $hitsData |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function buildHits(array $hitsData) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->hits = array_map( |
|
65
|
|
|
function(array $hitData) { |
|
66
|
|
|
return new Hit($hitData); |
|
67
|
|
|
}, |
|
68
|
|
|
array_values($hitsData) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return boolean |
|
74
|
|
|
*/ |
|
75
|
|
|
public function hasTimedOut() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->hasTimedOut; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getMaxScore() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->maxScore; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getTotalCount() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->totalCount; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return int |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getTotalTime() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->totalTime; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return Hit[] |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getHits() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->hits; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $name |
|
114
|
|
|
* @return array|null |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getAggregation($name) |
|
117
|
|
|
{ |
|
118
|
|
|
return isset($this->aggregations[$name]) ? |
|
119
|
|
|
$this->aggregations[$name] : null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public function hasAggregations() |
|
126
|
|
|
{ |
|
127
|
|
|
return !empty($this->aggregations); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getAggregations() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->aggregations; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* return int |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getCount() |
|
142
|
|
|
{ |
|
143
|
|
|
return count($this->hits); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* {@inheritdoc} |
|
148
|
|
|
*/ |
|
149
|
|
|
public function rewind() |
|
150
|
|
|
{ |
|
151
|
|
|
$this->_index = 0; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function current() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->hits[$this->_index]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
public function key() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->_index; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
|
|
public function next() |
|
174
|
|
|
{ |
|
175
|
|
|
++$this->_index; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritdoc} |
|
180
|
|
|
*/ |
|
181
|
|
|
public function valid() |
|
182
|
|
|
{ |
|
183
|
|
|
return array_key_exists($this->_index, $this->hits); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|