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

Revision 7588, 16.3 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 * Highlighting component
41 *
42 * @link http://wiki.apache.org/solr/HighlightingParameters
43 *
44 * @package Solarium
45 * @subpackage Query
46 */
47class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select_Component
48{
49    /**
50     * Value for fragmenter option gap
51     */
52    const FRAGMENTER_GAP = 'gap';
53
54    /**
55     * Value for fragmenter option regex
56     */
57    const FRAGMENTER_REGEX = 'regex';
58
59    /**
60     * Component type
61     *
62     * @var string
63     */
64    protected $_type = Solarium_Query_Select::COMPONENT_HIGHLIGHTING;
65
66    /**
67     * Array of fields for highlighting
68     *
69     * @var array
70     */
71    protected $_fields = array();
72
73    /**
74     * Initialize options
75     *
76     * The field option needs setup work
77     *
78     * @return void
79     */
80    protected function _init()
81    {
82        foreach ($this->_options AS $name => $value) {
83            switch ($name) {
84                case 'field':
85                    $this->addFields($value);
86                    break;
87            }
88        }
89    }
90
91    /**
92     * Get a field options object
93     *
94     * @param string $name
95     * @param boolean $autocreate
96     * @return Solarium_Query_Select_Component_Highlighting_Field
97     */
98    public function getField($name, $autocreate = true)
99    {
100        if (isset($this->_fields[$name])) {
101            return $this->_fields[$name];
102        } else if ($autocreate) {
103            $this->addField($name);
104            return $this->_fields[$name];
105        } else {
106            return null;
107        }
108    }
109
110    /**
111     * Add a field for highlighting
112     *
113     * @param string|array|Solarium_Query_Select_Component_Highlighting_Field $field
114     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
115     */
116    public function addField($field)
117    {
118        // autocreate object for string input
119        if (is_string($field)) {
120            $field = new Solarium_Query_Select_Component_Highlighting_Field(array('name' => $field));
121        } else if (is_array($field)) {
122            $field = new Solarium_Query_Select_Component_Highlighting_Field($field);
123        }
124
125        // validate field
126        if ($field->getName() === null) {
127            throw new Solarium_Exception('To add a highlighting field it needs to have at least a "name" setting');
128        }
129
130        $this->_fields[$field->getName()] = $field;
131        return $this;
132    }
133
134    /**
135     * Add multiple fields for highlighting
136     *
137     * @param string|array $fields can be an array of object instances or a string with comma
138     * separated fieldnames
139     *
140     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
141     */
142    public function addFields($fields)
143    {
144        if (is_string($fields)) {
145            $fields = explode(',', $fields);
146            $fields = array_map('trim', $fields);
147        }
148
149        foreach ($fields AS $key => $field) {
150
151            // in case of a config array without key: add key to config
152            if (is_array($field) && !isset($field['name'])) {
153                $field['name'] = $key;
154            }
155
156            $this->addField($field);
157        }
158
159        return $this;
160    }
161
162    /**
163     * Remove a highlighting field
164     *
165     * @param string $field
166     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
167     */
168    public function removeField($field)
169    {
170        if (isset($this->_fields[$field])) {
171           unset($this->_fields[$field]);
172        }
173
174        return $this;
175    }
176
177    /**
178     * Remove all fields
179     *
180     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
181     */
182    public function clearFields()
183    {
184        $this->_fields = array();
185        return $this;
186    }
187
188    /**
189     * Get the list of fields
190     *
191     * @return array
192     */
193    public function getFields()
194    {
195        return $this->_fields;
196    }
197
198    /**
199     * Set multiple fields
200     *
201     * This overwrites any existing fields
202     *
203     * @param array $fields
204     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
205     */
206    public function setFields($fields)
207    {
208        $this->clearFields();
209        $this->addFields($fields);
210
211        return $this;
212    }
213
214    /**
215     * Set snippets option
216     *
217     * Maximum number of snippets per field
218     *
219     * @param int $maximum
220     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
221     */
222    public function setSnippets($maximum)
223    {
224        return $this->_setOption('snippets', $maximum);
225    }
226
227    /**
228     * Get snippets option
229     *
230     * @return int|null
231     */
232    public function getSnippets()
233    {
234        return $this->getOption('snippets');
235    }
236
237    /**
238     * Set fragsize option
239     *
240     * The size, in characters, of fragments to consider for highlighting
241     *
242     * @param int $size
243     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
244     */
245    public function setFragSize($size)
246    {
247        return $this->_setOption('fragsize', $size);
248    }
249
250    /**
251     * Get fragsize option
252     *
253     * @return int|null
254     */
255    public function getFragSize()
256    {
257        return $this->getOption('fragsize');
258    }
259
260    /**
261     * Set mergeContiguous option
262     *
263     * Collapse contiguous fragments into a single fragment
264     *
265     * @param boolean $merge
266     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
267     */
268    public function setMergeContiguous($merge)
269    {
270        return $this->_setOption('mergecontiguous', $merge);
271    }
272
273    /**
274     * Get mergeContiguous option
275     *
276     * @return boolean|null
277     */
278    public function getMergeContiguous()
279    {
280        return $this->getOption('mergecontiguous');
281    }
282
283    /**
284     * Set requireFieldMatch option
285     *
286     * @param boolean $require
287     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
288     */
289    public function setRequireFieldMatch($require)
290    {
291        return $this->_setOption('requirefieldmatch', $require);
292    }
293
294    /**
295     * Get requireFieldMatch option
296     *
297     * @return boolean|null
298     */
299    public function getRequireFieldMatch()
300    {
301        return $this->getOption('requirefieldmatch');
302    }
303
304    /**
305     * Set maxAnalyzedChars option
306     *
307     * How many characters into a document to look for suitable snippets
308     *
309     * @param int $chars
310     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
311     */
312    public function setMaxAnalyzedChars($chars)
313    {
314        return $this->_setOption('maxanalyzedchars', $chars);
315    }
316
317    /**
318     * Get maxAnalyzedChars option
319     *
320     * @return int|null
321     */
322    public function getMaxAnalyzedChars()
323    {
324        return $this->getOption('maxanalyzedchars');
325    }
326
327    /**
328     * Set alternatefield option
329     *
330     * @param string $field
331     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
332     */
333    public function setAlternateField($field)
334    {
335        return $this->_setOption('alternatefield', $field);
336    }
337
338    /**
339     * Get alternatefield option
340     *
341     * @return string|null
342     */
343    public function getAlternateField()
344    {
345        return $this->getOption('alternatefield');
346    }
347
348    /**
349     * Set maxAlternateFieldLength option
350     *
351     * @param int $length
352     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
353     */
354    public function setMaxAlternateFieldLength($length)
355    {
356        return $this->_setOption('maxalternatefieldlength', $length);
357    }
358
359    /**
360     * Get maxAlternateFieldLength option
361     *
362     * @return int|null
363     */
364    public function getMaxAlternateFieldLength()
365    {
366        return $this->getOption('maxalternatefieldlength');
367    }
368
369    /**
370     * Set formatter option
371     *
372     * @param string $formatter
373     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
374     */
375    public function setFormatter($formatter = 'simple')
376    {
377        return $this->_setOption('formatter', $formatter);
378    }
379
380    /**
381     * Get formatter option
382     *
383     * @return string|null
384     */
385    public function getFormatter()
386    {
387        return $this->getOption('formatter');
388    }
389
390    /**
391     * Set simple prefix option
392     *
393     * Solr option h1.simple.pre
394     *
395     * @param string $prefix
396     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
397     */
398    public function setSimplePrefix($prefix)
399    {
400        return $this->_setOption('simpleprefix', $prefix);
401    }
402
403    /**
404     * Get simple prefix option
405     *
406     * Solr option hl.simple.pre
407     *
408     * @return string|null
409     */
410    public function getSimplePrefix()
411    {
412        return $this->getOption('simpleprefix');
413    }
414
415    /**
416     * Set simple postfix option
417     *
418     * Solr option h1.simple.post
419     *
420     * @param string $postfix
421     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
422     */
423    public function setSimplePostfix($postfix)
424    {
425        return $this->_setOption('simplepostfix', $postfix);
426    }
427
428    /**
429     * Get simple postfix option
430     *
431     * Solr option hl.simple.post
432     *
433     * @return string|null
434     */
435    public function getSimplePostfix()
436    {
437        return $this->getOption('simplepostfix');
438    }
439
440    /**
441     * Set fragmenter option
442     *
443     * Use one of the constants as value.
444     *
445     * @param string $fragmenter
446     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
447     */
448    public function setFragmenter($fragmenter)
449    {
450        return $this->_setOption('fragmenter', $fragmenter);
451    }
452
453    /**
454     * Get fragmenter option
455     *
456     * @return string|null
457     */
458    public function getFragmenter()
459    {
460        return $this->getOption('fragmenter');
461    }
462
463    /**
464     * Set fraglistbuilder option
465     *
466     * @param string $builder
467     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
468     */
469    public function setFragListBuilder($builder)
470    {
471        return $this->_setOption('fraglistbuilder', $builder);
472    }
473
474    /**
475     * Get fraglistbuilder option
476     *
477     * @return string|null
478     */
479    public function getFragListBuilder()
480    {
481        return $this->getOption('fraglistbuilder');
482    }
483
484    /**
485     * Set fragmentsbuilder option
486     *
487     * @param string $builder
488     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
489     */
490    public function setFragmentsBuilder($builder)
491    {
492        return $this->_setOption('fragmentsbuilder', $builder);
493    }
494
495    /**
496     * Get fragmentsbuilder option
497     *
498     * @return string|null
499     */
500    public function getFragmentsBuilder()
501    {
502        return $this->getOption('fragmentsbuilder');
503    }
504
505    /**
506     * Set useFastVectorHighlighter option
507     *
508     * @param boolean $use
509     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
510     */
511    public function setUseFastVectorHighlighter($use)
512    {
513        return $this->_setOption('usefastvectorhighlighter', $use);
514    }
515
516    /**
517     * Get useFastVectorHighlighter option
518     *
519     * @return boolean|null
520     */
521    public function getUseFastVectorHighlighter()
522    {
523        return $this->getOption('usefastvectorhighlighter');
524    }
525
526    /**
527     * Set usePhraseHighlighter option
528     *
529     * @param boolean $use
530     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
531     */
532    public function setUsePhraseHighlighter($use)
533    {
534        return $this->_setOption('usephrasehighlighter', $use);
535    }
536
537    /**
538     * Get usePhraseHighlighter option
539     *
540     * @return boolean|null
541     */
542    public function getUsePhraseHighlighter()
543    {
544        return $this->getOption('usephrasehighlighter');
545    }
546
547    /**
548     * Set HighlightMultiTerm option
549     *
550     * @param boolean $highlight
551     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
552     */
553    public function setHighlightMultiTerm($highlight)
554    {
555        return $this->_setOption('highlightmultiterm', $highlight);
556    }
557
558    /**
559     * Get HighlightMultiTerm option
560     *
561     * @return boolean|null
562     */
563    public function getHighlightMultiTerm()
564    {
565        return $this->getOption('highlightmultiterm');
566    }
567
568    /**
569     * Set RegexSlop option
570     *
571     * @param float $slop
572     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
573     */
574    public function setRegexSlop($slop)
575    {
576        return $this->_setOption('regexslop', $slop);
577    }
578
579    /**
580     * Get RegexSlop option
581     *
582     * @return float|null
583     */
584    public function getRegexSlop()
585    {
586        return $this->getOption('regexslop');
587    }
588
589    /**
590     * Set RegexPattern option
591     *
592     * @param string $pattern
593     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
594     */
595    public function setRegexPattern($pattern)
596    {
597        return $this->_setOption('regexpattern', $pattern);
598    }
599
600    /**
601     * Get RegexPattern option
602     *
603     * @return string|null
604     */
605    public function getRegexPattern()
606    {
607        return $this->getOption('regexpattern');
608    }
609
610    /**
611     * Set RegexMaxAnalyzedChars option
612     *
613     * @param int $chars
614     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
615     */
616    public function setRegexMaxAnalyzedChars($chars)
617    {
618        return $this->_setOption('regexmaxanalyzedchars', $chars);
619    }
620
621    /**
622     * Get RegexMaxAnalyzedChars option
623     *
624     * @return int|null
625     */
626    public function getRegexMaxAnalyzedChars()
627    {
628        return $this->getOption('regexmaxanalyzedchars');
629    }
630
631    /**
632     * Set highlight query option
633     *
634     * Overrides the q parameter for highlighting
635     *
636     * @param string $query
637     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
638     */
639    public function setQuery($query)
640    {
641        return $this->_setOption('query', $query);
642    }
643
644    /**
645     * Get query option
646     *
647     * @return string|null
648     */
649    public function getQuery()
650    {
651        return $this->getOption('query');
652    }
653
654    /**
655     * Set phraselimit option
656     *
657     * @param int $maximum
658     * @return Solarium_Query_Select_Component_Highlighting Provides fluent interface
659     */
660    public function setPhraseLimit($maximum)
661    {
662        return $this->_setOption('phraselimit', $maximum);
663    }
664
665    /**
666     * Get phraselimit option
667     *
668     * @return int|null
669     */
670    public function getPhraseLimit()
671    {
672        return $this->getOption('phraselimit');
673    }
674
675}
Note: See TracBrowser for help on using the repository browser.