1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | * |
---|
31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
33 | * @link http://www.solarium-project.org/ |
---|
34 | * |
---|
35 | * @package Solarium |
---|
36 | * @subpackage Result |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Select query result |
---|
41 | * |
---|
42 | * This is the standard resulttype for a select query. Example usage: |
---|
43 | * <code> |
---|
44 | * // total solr results |
---|
45 | * $result->getNumFound(); |
---|
46 | * |
---|
47 | * // results fetched |
---|
48 | * count($result); |
---|
49 | * |
---|
50 | * // get a single facet by key |
---|
51 | * $result->getFacet('category'); |
---|
52 | * |
---|
53 | * // iterate over fetched docs |
---|
54 | * foreach ($result AS $doc) { |
---|
55 | * .... |
---|
56 | * } |
---|
57 | * </code> |
---|
58 | * |
---|
59 | * @package Solarium |
---|
60 | * @subpackage Result |
---|
61 | */ |
---|
62 | class Solarium_Result_Select extends Solarium_Result_QueryType |
---|
63 | implements IteratorAggregate, Countable |
---|
64 | { |
---|
65 | |
---|
66 | /** |
---|
67 | * Solr numFound |
---|
68 | * |
---|
69 | * This is NOT the number of document fetched from Solr! |
---|
70 | * |
---|
71 | * @var int |
---|
72 | */ |
---|
73 | protected $_numfound; |
---|
74 | |
---|
75 | /** |
---|
76 | * Document instances array |
---|
77 | * |
---|
78 | * @var array |
---|
79 | */ |
---|
80 | protected $_documents; |
---|
81 | |
---|
82 | /** |
---|
83 | * Component results |
---|
84 | */ |
---|
85 | protected $_components; |
---|
86 | |
---|
87 | /** |
---|
88 | * Status code returned by Solr |
---|
89 | * |
---|
90 | * @var int |
---|
91 | */ |
---|
92 | protected $_status; |
---|
93 | |
---|
94 | /** |
---|
95 | * Solr index queryTime |
---|
96 | * |
---|
97 | * This doesn't include things like the HTTP responsetime. Purely the Solr |
---|
98 | * query execution time. |
---|
99 | * |
---|
100 | * @var int |
---|
101 | */ |
---|
102 | protected $_queryTime; |
---|
103 | |
---|
104 | /** |
---|
105 | * Get Solr status code |
---|
106 | * |
---|
107 | * This is not the HTTP status code! The normal value for success is 0. |
---|
108 | * |
---|
109 | * @return int |
---|
110 | */ |
---|
111 | public function getStatus() |
---|
112 | { |
---|
113 | $this->_parseResponse(); |
---|
114 | |
---|
115 | return $this->_status; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Get Solr query time |
---|
120 | * |
---|
121 | * This doesn't include things like the HTTP responsetime. Purely the Solr |
---|
122 | * query execution time. |
---|
123 | * |
---|
124 | * @return int |
---|
125 | */ |
---|
126 | public function getQueryTime() |
---|
127 | { |
---|
128 | $this->_parseResponse(); |
---|
129 | |
---|
130 | return $this->_queryTime; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * get Solr numFound |
---|
135 | * |
---|
136 | * Returns the total number of documents found by Solr (this is NOT the |
---|
137 | * number of document fetched from Solr!) |
---|
138 | * |
---|
139 | * @return int |
---|
140 | */ |
---|
141 | public function getNumFound() |
---|
142 | { |
---|
143 | $this->_parseResponse(); |
---|
144 | |
---|
145 | return $this->_numfound; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Get all documents |
---|
150 | * |
---|
151 | * @return array |
---|
152 | */ |
---|
153 | public function getDocuments() |
---|
154 | { |
---|
155 | $this->_parseResponse(); |
---|
156 | |
---|
157 | return $this->_documents; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * IteratorAggregate implementation |
---|
162 | * |
---|
163 | * @return ArrayIterator |
---|
164 | */ |
---|
165 | public function getIterator() |
---|
166 | { |
---|
167 | $this->_parseResponse(); |
---|
168 | |
---|
169 | return new ArrayIterator($this->_documents); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Countable implementation |
---|
174 | * |
---|
175 | * @return int |
---|
176 | */ |
---|
177 | public function count() |
---|
178 | { |
---|
179 | $this->_parseResponse(); |
---|
180 | |
---|
181 | return count($this->_documents); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Get all component results |
---|
186 | * |
---|
187 | * @return array |
---|
188 | */ |
---|
189 | public function getComponents() |
---|
190 | { |
---|
191 | $this->_parseResponse(); |
---|
192 | |
---|
193 | return $this->_components; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * Get a component result by key |
---|
198 | * |
---|
199 | * @param string $key |
---|
200 | * @return Solarium_Result_Select_Component |
---|
201 | */ |
---|
202 | public function getComponent($key) |
---|
203 | { |
---|
204 | $this->_parseResponse(); |
---|
205 | |
---|
206 | if (isset($this->_components[$key])) { |
---|
207 | return $this->_components[$key]; |
---|
208 | } else { |
---|
209 | return null; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * Get morelikethis component result |
---|
215 | * |
---|
216 | * This is a convenience method that maps presets to getComponent |
---|
217 | * |
---|
218 | * @return Solarium_Result_Select_MoreLikeThis |
---|
219 | */ |
---|
220 | public function getMoreLikeThis() |
---|
221 | { |
---|
222 | return $this->getComponent(Solarium_Query_Select::COMPONENT_MORELIKETHIS); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Get highlighting component result |
---|
227 | * |
---|
228 | * This is a convenience method that maps presets to getComponent |
---|
229 | * |
---|
230 | * @return Solarium_Result_Select_Highlighting |
---|
231 | */ |
---|
232 | public function getHighlighting() |
---|
233 | { |
---|
234 | return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING); |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * Get grouping component result |
---|
239 | * |
---|
240 | * This is a convenience method that maps presets to getComponent |
---|
241 | * |
---|
242 | * @return Solarium_Result_Select_Grouping |
---|
243 | */ |
---|
244 | public function getGrouping() |
---|
245 | { |
---|
246 | return $this->getComponent(Solarium_Query_Select::COMPONENT_GROUPING); |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * Get facetset component result |
---|
251 | * |
---|
252 | * This is a convenience method that maps presets to getComponent |
---|
253 | * |
---|
254 | * @return Solarium_Result_Select_FacetSet |
---|
255 | */ |
---|
256 | public function getFacetSet() |
---|
257 | { |
---|
258 | return $this->getComponent(Solarium_Query_Select::COMPONENT_FACETSET); |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * Get spellcheck component result |
---|
263 | * |
---|
264 | * This is a convenience method that maps presets to getComponent |
---|
265 | * |
---|
266 | * @return Solarium_Result_Select_Spellcheck |
---|
267 | */ |
---|
268 | public function getSpellcheck() |
---|
269 | { |
---|
270 | return $this->getComponent(Solarium_Query_Select::COMPONENT_SPELLCHECK); |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * Get stats component result |
---|
275 | * |
---|
276 | * This is a convenience method that maps presets to getComponent |
---|
277 | * |
---|
278 | * @return Solarium_Result_Select_Stats |
---|
279 | */ |
---|
280 | public function getStats() |
---|
281 | { |
---|
282 | return $this->getComponent(Solarium_Query_Select::COMPONENT_STATS); |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * Get debug component result |
---|
287 | * |
---|
288 | * This is a convenience method that maps presets to getComponent |
---|
289 | * |
---|
290 | * @return Solarium_Result_Select_Debug |
---|
291 | */ |
---|
292 | public function getDebug() |
---|
293 | { |
---|
294 | return $this->getComponent(Solarium_Query_Select::COMPONENT_DEBUG); |
---|
295 | } |
---|
296 | } |
---|