final move of files
[web.mtrack] / Zend / Search / Lucene / Search / Query / 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 16541 2009-07-07 06:59:03Z bkarwin $
21  */
22
23
24 /** Zend_Search_Lucene_Search_Query */
25 require_once 'Zend/Search/Lucene/Search/Query.php';
26
27 /** Zend_Search_Lucene_Search_Weight_Term */
28 require_once 'Zend/Search/Lucene/Search/Weight/Term.php';
29
30
31 /**
32  * @category   Zend
33  * @package    Zend_Search_Lucene
34  * @subpackage Search
35  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
37  */
38 class Zend_Search_Lucene_Search_Query_Term extends Zend_Search_Lucene_Search_Query
39 {
40     /**
41      * Term to find.
42      *
43      * @var Zend_Search_Lucene_Index_Term
44      */
45     private $_term;
46
47     /**
48      * Documents vector.
49      *
50      * @var array
51      */
52     private $_docVector = null;
53
54     /**
55      * Term freqs vector.
56      * array(docId => freq, ...)
57      *
58      * @var array
59      */
60     private $_termFreqs;
61
62
63     /**
64      * Zend_Search_Lucene_Search_Query_Term constructor
65      *
66      * @param Zend_Search_Lucene_Index_Term $term
67      * @param boolean $sign
68      */
69     public function __construct(Zend_Search_Lucene_Index_Term $term)
70     {
71         $this->_term = $term;
72     }
73
74     /**
75      * Re-write query into primitive queries in the context of specified index
76      *
77      * @param Zend_Search_Lucene_Interface $index
78      * @return Zend_Search_Lucene_Search_Query
79      */
80     public function rewrite(Zend_Search_Lucene_Interface $index)
81     {
82         if ($this->_term->field != null) {
83             return $this;
84         } else {
85             $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
86             $query->setBoost($this->getBoost());
87
88             foreach ($index->getFieldNames(true) as $fieldName) {
89                 $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);
90
91                 $query->addTerm($term);
92             }
93
94             return $query->rewrite($index);
95         }
96     }
97
98     /**
99      * Optimize query in the context of specified index
100      *
101      * @param Zend_Search_Lucene_Interface $index
102      * @return Zend_Search_Lucene_Search_Query
103      */
104     public function optimize(Zend_Search_Lucene_Interface $index)
105     {
106         // Check, that index contains specified term
107         if (!$index->hasTerm($this->_term)) {
108             return new Zend_Search_Lucene_Search_Query_Empty();
109         }
110
111         return $this;
112     }
113
114
115     /**
116      * Constructs an appropriate Weight implementation for this query.
117      *
118      * @param Zend_Search_Lucene_Interface $reader
119      * @return Zend_Search_Lucene_Search_Weight
120      */
121     public function createWeight(Zend_Search_Lucene_Interface $reader)
122     {
123         $this->_weight = new Zend_Search_Lucene_Search_Weight_Term($this->_term, $this, $reader);
124         return $this->_weight;
125     }
126
127     /**
128      * Execute query in context of index reader
129      * It also initializes necessary internal structures
130      *
131      * @param Zend_Search_Lucene_Interface $reader
132      * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
133      */
134     public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null)
135     {
136         $this->_docVector = array_flip($reader->termDocs($this->_term, $docsFilter));
137         $this->_termFreqs = $reader->termFreqs($this->_term, $docsFilter);
138
139         // Initialize weight if it's not done yet
140         $this->_initWeight($reader);
141     }
142
143     /**
144      * Get document ids likely matching the query
145      *
146      * It's an array with document ids as keys (performance considerations)
147      *
148      * @return array
149      */
150     public function matchedDocs()
151     {
152         return $this->_docVector;
153     }
154
155     /**
156      * Score specified document
157      *
158      * @param integer $docId
159      * @param Zend_Search_Lucene_Interface $reader
160      * @return float
161      */
162     public function score($docId, Zend_Search_Lucene_Interface $reader)
163     {
164         if (isset($this->_docVector[$docId])) {
165             return $reader->getSimilarity()->tf($this->_termFreqs[$docId]) *
166                    $this->_weight->getValue() *
167                    $reader->norm($docId, $this->_term->field) *
168                    $this->getBoost();
169         } else {
170             return 0;
171         }
172     }
173
174     /**
175      * Return query terms
176      *
177      * @return array
178      */
179     public function getQueryTerms()
180     {
181         return array($this->_term);
182     }
183
184     /**
185      * Return query term
186      *
187      * @return Zend_Search_Lucene_Index_Term
188      */
189     public function getTerm()
190     {
191         return $this->_term;
192     }
193
194     /**
195      * Query specific matches highlighting
196      *
197      * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter  Highlighter object (also contains doc for highlighting)
198      */
199     protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter)
200     {
201         $highlighter->highlight($this->_term->text);
202     }
203
204     /**
205      * Print a query
206      *
207      * @return string
208      */
209     public function __toString()
210     {
211         // It's used only for query visualisation, so we don't care about characters escaping
212         if ($this->_term->field !== null) {
213                 $query = $this->_term->field . ':';
214         } else {
215                 $query = '';
216         }
217
218         $query .= $this->_term->text;
219
220         if ($this->getBoost() != 1) {
221             $query = $query . '^' . round($this->getBoost(), 4);
222         }
223
224         return $query;
225     }
226 }
227