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 Query |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Grouping component |
---|
41 | * |
---|
42 | * Also known as Result Grouping or Field Collapsing. |
---|
43 | * See the Solr wiki for more info about this functionality |
---|
44 | * |
---|
45 | * @link http://wiki.apache.org/solr/FieldCollapsing |
---|
46 | * |
---|
47 | * @since 2.1.0 |
---|
48 | * |
---|
49 | * @package Solarium |
---|
50 | * @subpackage Query |
---|
51 | */ |
---|
52 | class Solarium_Query_Select_Component_Grouping extends Solarium_Query_Select_Component |
---|
53 | { |
---|
54 | |
---|
55 | /** |
---|
56 | * Value for format grouped |
---|
57 | */ |
---|
58 | const FORMAT_GROUPED = 'grouped'; |
---|
59 | |
---|
60 | /** |
---|
61 | * Value for format simple |
---|
62 | */ |
---|
63 | const FORMAT_SIMPLE = 'simple'; |
---|
64 | |
---|
65 | /** |
---|
66 | * Component type |
---|
67 | * |
---|
68 | * @var string |
---|
69 | */ |
---|
70 | protected $_type = Solarium_Query_Select::COMPONENT_GROUPING; |
---|
71 | |
---|
72 | /** |
---|
73 | * Fields for grouping |
---|
74 | * |
---|
75 | * @var array |
---|
76 | */ |
---|
77 | protected $_fields = array(); |
---|
78 | |
---|
79 | /** |
---|
80 | * Queries for grouping |
---|
81 | * |
---|
82 | * @var array |
---|
83 | */ |
---|
84 | protected $_queries = array(); |
---|
85 | |
---|
86 | /** |
---|
87 | * Initialize options |
---|
88 | * |
---|
89 | * Several options need some extra checks or setup work, for these options |
---|
90 | * the setters are called. |
---|
91 | * |
---|
92 | * @return void |
---|
93 | */ |
---|
94 | protected function _init() |
---|
95 | { |
---|
96 | foreach ($this->_options AS $name => $value) { |
---|
97 | switch ($name) { |
---|
98 | case 'queries': |
---|
99 | $this->setQueries($value); |
---|
100 | break; |
---|
101 | case 'fields': |
---|
102 | $this->setFields($value); |
---|
103 | break; |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Add a grouping field |
---|
110 | * |
---|
111 | * Group based on the unique values of a field |
---|
112 | * |
---|
113 | * @param string $field |
---|
114 | * @return Solarium_Field_Select_Component_Grouping fluent interface |
---|
115 | */ |
---|
116 | public function addField($field) |
---|
117 | { |
---|
118 | $this->_fields[] = $field; |
---|
119 | |
---|
120 | return $this; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Add multiple grouping fields |
---|
125 | * |
---|
126 | * You can use an array or a comma separated string as input |
---|
127 | * |
---|
128 | * @param array|string $fields |
---|
129 | * @return Solarium_Field_Select_Component_Grouping Provides fluent interface |
---|
130 | */ |
---|
131 | public function addFields($fields) |
---|
132 | { |
---|
133 | if (is_string($fields)) { |
---|
134 | $fields = explode(',', $fields); |
---|
135 | $fields = array_map('trim', $fields); |
---|
136 | } |
---|
137 | |
---|
138 | $this->_fields = array_merge($this->_fields, $fields); |
---|
139 | |
---|
140 | return $this; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Get all fields |
---|
145 | * |
---|
146 | * @return array |
---|
147 | */ |
---|
148 | public function getFields() |
---|
149 | { |
---|
150 | return $this->_fields; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Remove all fields |
---|
155 | * |
---|
156 | * @return Solarium_Field_Select_Component_Grouping fluent interface |
---|
157 | */ |
---|
158 | public function clearFields() |
---|
159 | { |
---|
160 | $this->_fields = array(); |
---|
161 | return $this; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Set multiple fields |
---|
166 | * |
---|
167 | * This overwrites any existing fields |
---|
168 | * |
---|
169 | * @param array $fields |
---|
170 | */ |
---|
171 | public function setFields($fields) |
---|
172 | { |
---|
173 | $this->clearFields(); |
---|
174 | $this->addFields($fields); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Add a grouping query |
---|
179 | * |
---|
180 | * Group documents that match the given query |
---|
181 | * |
---|
182 | * @param string $query |
---|
183 | * @return Solarium_Query_Select_Component_Grouping fluent interface |
---|
184 | */ |
---|
185 | public function addQuery($query) |
---|
186 | { |
---|
187 | $this->_queries[] = $query; |
---|
188 | |
---|
189 | return $this; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Add multiple grouping queries |
---|
194 | * |
---|
195 | * @param array|string $queries |
---|
196 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
197 | */ |
---|
198 | public function addQueries($queries) |
---|
199 | { |
---|
200 | if(!is_array($queries)) $queries = array($queries); |
---|
201 | |
---|
202 | $this->_queries = array_merge($this->_queries, $queries); |
---|
203 | |
---|
204 | return $this; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Get all queries |
---|
209 | * |
---|
210 | * @return array |
---|
211 | */ |
---|
212 | public function getQueries() |
---|
213 | { |
---|
214 | return $this->_queries; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Remove all queries |
---|
219 | * |
---|
220 | * @return Solarium_Query_Select_Component_Grouping fluent interface |
---|
221 | */ |
---|
222 | public function clearQueries() |
---|
223 | { |
---|
224 | $this->_queries = array(); |
---|
225 | return $this; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Set multiple queries |
---|
230 | * |
---|
231 | * This overwrites any existing queries |
---|
232 | * |
---|
233 | * @param array $queries |
---|
234 | */ |
---|
235 | public function setQueries($queries) |
---|
236 | { |
---|
237 | $this->clearQueries(); |
---|
238 | $this->addQueries($queries); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Set limit option |
---|
243 | * |
---|
244 | * The number of results (documents) to return for each group |
---|
245 | * |
---|
246 | * @param int $limit |
---|
247 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
248 | */ |
---|
249 | public function setLimit($limit) |
---|
250 | { |
---|
251 | return $this->_setOption('limit', $limit); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Get limit option |
---|
256 | * |
---|
257 | * @return string|null |
---|
258 | */ |
---|
259 | public function getLimit() |
---|
260 | { |
---|
261 | return $this->getOption('limit'); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * Set offset option |
---|
266 | * |
---|
267 | * The offset into the document list of each group. |
---|
268 | * |
---|
269 | * @param int $offset |
---|
270 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
271 | */ |
---|
272 | public function setOffset($offset) |
---|
273 | { |
---|
274 | return $this->_setOption('offset', $offset); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * Get offset option |
---|
279 | * |
---|
280 | * @return string|null |
---|
281 | */ |
---|
282 | public function getOffset() |
---|
283 | { |
---|
284 | return $this->getOption('offset'); |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * Set sort option |
---|
289 | * |
---|
290 | * How to sort documents within a single group |
---|
291 | * |
---|
292 | * @param string $sort |
---|
293 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
294 | */ |
---|
295 | public function setSort($sort) |
---|
296 | { |
---|
297 | return $this->_setOption('sort', $sort); |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * Get sort option |
---|
302 | * |
---|
303 | * @return string|null |
---|
304 | */ |
---|
305 | public function getSort() |
---|
306 | { |
---|
307 | return $this->getOption('sort'); |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * Set mainresult option |
---|
312 | * |
---|
313 | * If true, the result of the first field grouping command is used as the main |
---|
314 | * result list in the response, using group format 'simple' |
---|
315 | * |
---|
316 | * @param boolean $value |
---|
317 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
318 | */ |
---|
319 | public function setMainResult($value) |
---|
320 | { |
---|
321 | return $this->_setOption('mainresult', $value); |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * Get mainresult option |
---|
326 | * |
---|
327 | * @return boolean|null |
---|
328 | */ |
---|
329 | public function getMainResult() |
---|
330 | { |
---|
331 | return $this->getOption('mainresult'); |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | * Set numberofgroups option |
---|
336 | * |
---|
337 | * If true, includes the number of groups that have matched the query. |
---|
338 | * |
---|
339 | * @param boolean $value |
---|
340 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
341 | */ |
---|
342 | public function setNumberOfGroups($value) |
---|
343 | { |
---|
344 | return $this->_setOption('numberofgroups', $value); |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * Get numberofgroups option |
---|
349 | * |
---|
350 | * @return boolean|null |
---|
351 | */ |
---|
352 | public function getNumberOfGroups() |
---|
353 | { |
---|
354 | return $this->getOption('numberofgroups'); |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * Set cachepercentage option |
---|
359 | * |
---|
360 | * If > 0 enables grouping cache. Grouping is executed actual two searches. |
---|
361 | * This option caches the second search. A value of 0 disables grouping caching. |
---|
362 | * |
---|
363 | * Tests have shown that this cache only improves search time with boolean queries, |
---|
364 | * wildcard queries and fuzzy queries. For simple queries like a term query or |
---|
365 | * a match all query this cache has a negative impact on performance |
---|
366 | * |
---|
367 | * @param integer $value |
---|
368 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
369 | */ |
---|
370 | public function setCachePercentage($value) |
---|
371 | { |
---|
372 | return $this->_setOption('cachepercentage', $value); |
---|
373 | } |
---|
374 | |
---|
375 | /** |
---|
376 | * Get cachepercentage option |
---|
377 | * |
---|
378 | * @return integer|null |
---|
379 | */ |
---|
380 | public function getCachePercentage() |
---|
381 | { |
---|
382 | return $this->getOption('cachepercentage'); |
---|
383 | } |
---|
384 | |
---|
385 | /** |
---|
386 | * Set truncate option |
---|
387 | * |
---|
388 | * If true, facet counts are based on the most relevant document of each group matching the query. |
---|
389 | * Same applies for StatsComponent. Default is false. Only available from Solr 3.4 |
---|
390 | * |
---|
391 | * @param boolean $value |
---|
392 | * @return Solarium_Query_Select_Component_Grouping Provides fluent interface |
---|
393 | */ |
---|
394 | public function setTruncate($value) |
---|
395 | { |
---|
396 | return $this->_setOption('truncate', $value); |
---|
397 | } |
---|
398 | |
---|
399 | /** |
---|
400 | * Get truncate option |
---|
401 | * |
---|
402 | * @return boolean|null |
---|
403 | */ |
---|
404 | public function getTruncate() |
---|
405 | { |
---|
406 | return $this->getOption('truncate'); |
---|
407 | } |
---|
408 | |
---|
409 | } |
---|