source: sandbox/expresso-solr/expressoMail1_2/solrclient/library/Solarium/Query/Select/Component/FacetSet.php @ 7588

Revision 7588, 11.4 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
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 * MoreLikeThis component
41 *
42 * @link http://wiki.apache.org/solr/MoreLikeThis
43 *
44 * @package Solarium
45 * @subpackage Query
46 */
47class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Component
48{
49
50    /**
51     * Facet type field
52     */
53    const FACET_FIELD = 'field';
54
55    /**
56     * Facet type query
57     */
58    const FACET_QUERY = 'query';
59
60    /**
61     * Facet type multiquery
62     */
63    const FACET_MULTIQUERY = 'multiquery';
64
65    /**
66     * Facet type range
67     */
68    const FACET_RANGE = 'range';
69
70    /**
71     * Facet type mapping
72     *
73     * @var array
74     */
75    protected $_facetTypes = array(
76        self::FACET_FIELD => 'Solarium_Query_Select_Component_Facet_Field',
77        self::FACET_QUERY => 'Solarium_Query_Select_Component_Facet_Query',
78        self::FACET_MULTIQUERY => 'Solarium_Query_Select_Component_Facet_MultiQuery',
79        self::FACET_RANGE => 'Solarium_Query_Select_Component_Facet_Range',
80    );
81
82    /**
83     * Component type
84     *
85     * @var string
86     */
87    protected $_type = Solarium_Query_Select::COMPONENT_FACETSET;
88
89    /**
90     * Default options
91     *
92     * @var array
93     */
94    protected $_options = array();
95
96    /**
97     * Facets
98     *
99     * @var array
100     */
101    protected $_facets = array();
102
103    /**
104     * Initialize options
105     *
106     * Several options need some extra checks or setup work, for these options
107     * the setters are called.
108     *
109     * @return void
110     */
111    protected function _init()
112    {
113        if (isset($this->_options['facet'])) {
114            foreach ($this->_options['facet'] AS $key => $config) {
115                if (!isset($config['key'])) {
116                    $config['key'] = $key;
117                }
118
119                $this->addFacet($config);
120            }
121        }
122    }
123
124    /**
125     * Limit the terms for faceting by a prefix
126     *
127     * This is a global value for all facets in this facetset
128     *
129     * @param string $prefix
130     * @return Solarium_Query_Select_Component_FacetSet Provides fluent interface
131     */
132    public function setPrefix($prefix)
133    {
134        return $this->_setOption('prefix', $prefix);
135    }
136
137    /**
138     * Get the facet prefix
139     *
140     * This is a global value for all facets in this facetset
141     *
142     * @return string
143     */
144    public function getPrefix()
145    {
146        return $this->getOption('prefix');
147    }
148
149    /**
150     * Set the facet sort order
151     *
152     * Use one of the SORT_* constants as the value
153     *
154     * This is a global value for all facets in this facetset
155     *
156     * @param string $sort
157     * @return Solarium_Query_Select_Component_FacetSet Provides fluent interface
158     */
159    public function setSort($sort)
160    {
161        return $this->_setOption('sort', $sort);
162    }
163
164    /**
165     * Get the facet sort order
166     *
167     * This is a global value for all facets in this facetset
168     *
169     * @return string
170     */
171    public function getSort()
172    {
173        return $this->getOption('sort');
174    }
175
176    /**
177     * Set the facet limit
178     *
179     *  This is a global value for all facets in this facetset
180     *
181     * @param int $limit
182     * @return Solarium_Query_Select_Component_FacetSet Provides fluent interface
183     */
184    public function setLimit($limit)
185    {
186        return $this->_setOption('limit', $limit);
187    }
188
189    /**
190     * Get the facet limit
191     *
192     * This is a global value for all facets in this facetset
193     *
194     * @return string
195     */
196    public function getLimit()
197    {
198        return $this->getOption('limit');
199    }
200
201    /**
202     * Set the facet mincount
203     *
204     * This is a global value for all facets in this facetset
205     *
206     * @param int $minCount
207     * @return Solarium_Query_Select_Component_FacetSet Provides fluent interface
208     */
209    public function setMinCount($minCount)
210    {
211        return $this->_setOption('mincount', $minCount);
212    }
213
214    /**
215     * Get the facet mincount
216     *
217     * This is a global value for all facets in this facetset
218     *
219     * @return int
220     */
221    public function getMinCount()
222    {
223        return $this->getOption('mincount');
224    }
225
226    /**
227     * Set the missing count option
228     *
229     * This is a global value for all facets in this facetset
230     *
231     * @param boolean $missing
232     * @return Solarium_Query_Select_Component_FacetSet Provides fluent interface
233     */
234    public function setMissing($missing)
235    {
236        return $this->_setOption('missing', $missing);
237    }
238
239    /**
240     * Get the facet missing option
241     *
242     * This is a global value for all facets in this facetset
243     *
244     * @return boolean
245     */
246    public function getMissing()
247    {
248        return $this->getOption('missing');
249    }
250
251    /**
252     * Add a facet
253     *
254     * @param Solarium_Query_Select_Component_Facet|array $facet
255     * @return Solarium_Query Provides fluent interface
256     */
257    public function addFacet($facet)
258    {
259        if (is_array($facet)) {
260            $facet = $this->createFacet($facet['type'], $facet, false);
261        }
262
263        $key = $facet->getKey();
264
265        if (0 === strlen($key)) {
266            throw new Solarium_Exception('A facet must have a key value');
267        }
268
269        //double add calls for the same facet are ignored, but non-unique keys cause an exception
270        //@todo add trigger_error with a notice for double add calls?
271        if (array_key_exists($key, $this->_facets) && $this->_facets[$key] !== $facet) {
272            throw new Solarium_Exception('A facet must have a unique key value within a query');
273        } else {
274             $this->_facets[$key] = $facet;
275        }
276
277        return $this;
278    }
279
280    /**
281     * Add multiple facets
282     *
283     * @param array $facets
284     * @return Solarium_Query Provides fluent interface
285     */
286    public function addFacets(array $facets)
287    {
288        foreach ($facets AS $key => $facet) {
289
290            // in case of a config array: add key to config
291            if (is_array($facet) && !isset($facet['key'])) {
292                $facet['key'] = $key;
293            }
294
295            $this->addFacet($facet);
296        }
297
298        return $this;
299    }
300
301    /**
302     * Get a facet
303     *
304     * @param string $key
305     * @return string
306     */
307    public function getFacet($key)
308    {
309        if (isset($this->_facets[$key])) {
310            return $this->_facets[$key];
311        } else {
312            return null;
313        }
314    }
315
316    /**
317     * Get all facets
318     *
319     * @return array
320     */
321    public function getFacets()
322    {
323        return $this->_facets;
324    }
325
326    /**
327     * Remove a single facet
328     *
329     * You can remove a facet by passing it's key or the facet instance
330     *
331     * @param string|Solarium_Query_Select_Component_Facet $facet
332     * @return Solarium_Query Provides fluent interface
333     */
334    public function removeFacet($facet)
335    {
336        if (is_object($facet)) {
337            $facet = $facet->getKey();
338        }
339
340        if (isset($this->_facets[$facet])) {
341            unset($this->_facets[$facet]);
342        }
343
344        return $this;
345    }
346
347    /**
348     * Remove all facets
349     *
350     * @return Solarium_Query Provides fluent interface
351     */
352    public function clearFacets()
353    {
354        $this->_facets = array();
355        return $this;
356    }
357
358    /**
359     * Set multiple facets
360     *
361     * This overwrites any existing facets
362     *
363     * @param array $facets
364     */
365    public function setFacets($facets)
366    {
367        $this->clearFacets();
368        $this->addFacets($facets);
369    }
370
371    /**
372     * Create a facet instance
373     *
374     * If you supply a string as the first arguments ($options) it will be used as the key for the facet
375     * and it will be added to this query.
376     * If you supply an options array/object that contains a key the facet will also be added to the query.
377     *
378     * When no key is supplied the facet cannot be added, in that case you will need to add it manually
379     * after setting the key, by using the addFacet method.
380     *
381     * @param string $type
382     * @param array|object|null $options
383     * @param boolean $add
384     * @return Solarium_Query_Select_Component_Facet
385     */
386    public function createFacet($type, $options = null, $add = true)
387    {
388        $type = strtolower($type);
389
390        if (!isset($this->_facetTypes[$type])) {
391            throw new Solarium_Exception("Facettype unknown: " . $type);
392        }
393
394        $class = $this->_facetTypes[$type];
395
396        if (is_string($options)) {
397            $facet = new $class;
398            $facet->setKey($options);
399        } else {
400            $facet = new $class($options);
401        }
402
403        if ($add && $facet->getKey() !== null) {
404            $this->addFacet($facet);
405        }
406
407        return $facet;
408    }
409
410    /**
411     * Get a facet field instance
412     *
413     * @param mixed $options
414     * @return Solarium_Query_Select_Component_Facet_Field
415     */
416    public function createFacetField($options = null)
417    {
418        return $this->createFacet(self::FACET_FIELD, $options);
419    }
420
421    /**
422     * Get a facet query instance
423     *
424     * @param mixed $options
425     * @return Solarium_Query_Select_Component_Facet_Query
426     */
427    public function createFacetQuery($options = null)
428    {
429        return $this->createFacet(self::FACET_QUERY, $options);
430    }
431
432    /**
433     * Get a facet multiquery instance
434     *
435     * @param mixed $options
436     * @return Solarium_Query_Select_Component_Facet_MultiQuery
437     */
438    public function createFacetMultiQuery($options = null)
439    {
440        return $this->createFacet(self::FACET_MULTIQUERY, $options);
441    }
442
443    /**
444     * Get a facet range instance
445     *
446     * @param mixed $options
447     * @return Solarium_Query_Select_Component_Facet_Range
448     */
449    public function createFacetRange($options = null)
450    {
451        return $this->createFacet(self::FACET_RANGE, $options);
452    }
453
454}
Note: See TracBrowser for help on using the repository browser.