import
[web.mtrack] / inc / lib / Zend / Search / Lucene / Search / QueryEntry / Term.php
1 <?php
2 /**
3  * Zend Framework
4  *
5  * LICENSE
6  *
7  * This source file is subject to the new BSD license that is bundled
8  * with this package in the file LICENSE.txt.
9  * It is also available through the world-wide-web at this URL:
10  * http://framework.zend.com/license/new-bsd
11  * If you did not receive a copy of the license and are unable to
12  * obtain it through the world-wide-web, please send an email
13  * to license@zend.com so we can send you a copy immediately.
14  *
15  * @category   Zend
16  * @package    Zend_Search_Lucene
17  * @subpackage Search
18  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
20  * @version    $Id: Term.php 16971 2009-07-22 18:05:45Z mikaelkael $
21  */
22
23 /** Zend_Search_Lucene_Index_Term */
24 require_once 'Zend/Search/Lucene/Index/Term.php';
25
26 /** Zend_Search_Lucene_Search_QueryEntry */
27 require_once 'Zend/Search/Lucene/Search/QueryEntry.php';
28
29 /** Zend_Search_Lucene_Analysis_Analyzer */
30 require_once 'Zend/Search/Lucene/Analysis/Analyzer.php';
31
32 /**
33  * @category   Zend
34  * @package    Zend_Search_Lucene
35  * @subpackage Search
36  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
38  */
39 class Zend_Search_Lucene_Search_QueryEntry_Term extends Zend_Search_Lucene_Search_QueryEntry
40 {
41     /**
42      * Term value
43      *
44      * @var string
45      */
46     private $_term;
47
48     /**
49      * Field
50      *
51      * @var string|null
52      */
53     private $_field;
54
55
56     /**
57      * Fuzzy search query
58      *
59      * @var boolean
60      */
61     private $_fuzzyQuery = false;
62
63     /**
64      * Similarity
65      *
66      * @var float
67      */
68     private $_similarity = 1.;
69
70
71     /**
72      * Object constractor
73      *
74      * @param string $term
75      * @param string $field
76      */
77     public function __construct($term, $field)
78     {
79         $this->_term  = $term;
80         $this->_field = $field;
81     }
82
83     /**
84      * Process modifier ('~')
85      *
86      * @param mixed $parameter
87      */
88     public function processFuzzyProximityModifier($parameter = null)
89     {
90         $this->_fuzzyQuery = true;
91
92         if ($parameter !== null) {
93             $this->_similarity = $parameter;
94         } else {
95             $this->_similarity = Zend_Search_Lucene_Search_Query_Fuzzy::DEFAULT_MIN_SIMILARITY;
96         }
97     }
98
99     /**
100      * Transform entry to a subquery
101      *
102      * @param string $encoding
103      * @return Zend_Search_Lucene_Search_Query
104      * @throws Zend_Search_Lucene_Search_QueryParserException
105      */
106     public function getQuery($encoding)
107     {
108         if ($this->_fuzzyQuery) {
109                 $query = new Zend_Search_Lucene_Search_Query_Preprocessing_Fuzzy($this->_term,
110                                                                                  $encoding,
111                                                                                  ($this->_field !== null)?
112                                                                                   iconv($encoding, 'UTF-8', $this->_field) :
113                                                                                   null,
114                                                                                  $this->_similarity
115                                                                                  );
116             $query->setBoost($this->_boost);
117             return $query;
118         }
119
120
121         $query = new Zend_Search_Lucene_Search_Query_Preprocessing_Term($this->_term,
122                                                                         $encoding,
123                                                                         ($this->_field !== null)?
124                                                                               iconv($encoding, 'UTF-8', $this->_field) :
125                                                                               null
126                                                                         );
127         $query->setBoost($this->_boost);
128         return $query;
129     }
130 }