import
[web.mtrack] / inc / lib / Zend / Search / Lucene / Search / Query / Range.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: Range.php 16971 2009-07-22 18:05:45Z mikaelkael $
21  */
22
23
24 /** Zend_Search_Lucene_Search_Query */
25 require_once 'Zend/Search/Lucene/Search/Query.php';
26
27 /** Zend_Search_Lucene_Search_Query_MultiTerm */
28 require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.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_Range extends Zend_Search_Lucene_Search_Query
39 {
40     /**
41      * Lower term.
42      *
43      * @var Zend_Search_Lucene_Index_Term
44      */
45     private $_lowerTerm;
46
47     /**
48      * Upper term.
49      *
50      * @var Zend_Search_Lucene_Index_Term
51      */
52     private $_upperTerm;
53
54
55     /**
56      * Search field
57      *
58      * @var string
59      */
60     private $_field;
61
62     /**
63      * Inclusive
64      *
65      * @var boolean
66      */
67     private $_inclusive;
68
69     /**
70      * Matched terms.
71      *
72      * Matched terms list.
73      * It's filled during the search (rewrite operation) and may be used for search result
74      * post-processing
75      *
76      * Array of Zend_Search_Lucene_Index_Term objects
77      *
78      * @var array
79      */
80     private $_matches = null;
81
82
83     /**
84      * Zend_Search_Lucene_Search_Query_Range constructor.
85      *
86      * @param Zend_Search_Lucene_Index_Term|null $lowerTerm
87      * @param Zend_Search_Lucene_Index_Term|null $upperTerm
88      * @param boolean $inclusive
89      * @throws Zend_Search_Lucene_Exception
90      */
91     public function __construct($lowerTerm, $upperTerm, $inclusive)
92     {
93         if ($lowerTerm === null  &&  $upperTerm === null) {
94             require_once 'Zend/Search/Lucene/Exception.php';
95             throw new Zend_Search_Lucene_Exception('At least one term must be non-null');
96         }
97         if ($lowerTerm !== null  &&  $upperTerm !== null  &&  $lowerTerm->field != $upperTerm->field) {
98             require_once 'Zend/Search/Lucene/Exception.php';
99             throw new Zend_Search_Lucene_Exception('Both terms must be for the same field');
100         }
101
102         $this->_field     = ($lowerTerm !== null)? $lowerTerm->field : $upperTerm->field;
103         $this->_lowerTerm = $lowerTerm;
104         $this->_upperTerm = $upperTerm;
105         $this->_inclusive = $inclusive;
106     }
107
108     /**
109      * Get query field name
110      *
111      * @return string|null
112      */
113     public function getField()
114     {
115         return $this->_field;
116     }
117
118     /**
119      * Get lower term
120      *
121      * @return Zend_Search_Lucene_Index_Term|null
122      */
123     public function getLowerTerm()
124     {
125         return $this->_lowerTerm;
126     }
127
128     /**
129      * Get upper term
130      *
131      * @return Zend_Search_Lucene_Index_Term|null
132      */
133     public function getUpperTerm()
134     {
135         return $this->_upperTerm;
136     }
137
138     /**
139      * Get upper term
140      *
141      * @return boolean
142      */
143     public function isInclusive()
144     {
145         return $this->_inclusive;
146     }
147
148     /**
149      * Re-write query into primitive queries in the context of specified index
150      *
151      * @param Zend_Search_Lucene_Interface $index
152      * @return Zend_Search_Lucene_Search_Query
153      */
154     public function rewrite(Zend_Search_Lucene_Interface $index)
155     {
156         $this->_matches = array();
157
158         if ($this->_field === null) {
159             // Search through all fields
160             $fields = $index->getFieldNames(true /* indexed fields list */);
161         } else {
162             $fields = array($this->_field);
163         }
164
165         $maxTerms = Zend_Search_Lucene::getTermsPerQueryLimit();
166         foreach ($fields as $field) {
167             $index->resetTermsStream();
168
169             if ($this->_lowerTerm !== null) {
170                 $lowerTerm = new Zend_Search_Lucene_Index_Term($this->_lowerTerm->text, $field);
171
172                 $index->skipTo($lowerTerm);
173
174                 if (!$this->_inclusive  &&
175                     $index->currentTerm() == $lowerTerm) {
176                     // Skip lower term
177                     $index->nextTerm();
178                 }
179             } else {
180                 $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
181             }
182
183
184             if ($this->_upperTerm !== null) {
185                 // Walk up to the upper term
186                 $upperTerm = new Zend_Search_Lucene_Index_Term($this->_upperTerm->text, $field);
187
188                 while ($index->currentTerm() !== null          &&
189                        $index->currentTerm()->field == $field  &&
190 //                       $index->currentTerm()->text  <  $upperTerm->text
191 // WEZ: this actually needs to be lexigraphically searched
192                        strcmp($index->currentTerm()->text, $upperTerm->text) < 0
193                                            ) {
194                     $this->_matches[] = $index->currentTerm();
195
196                     if ($maxTerms != 0  &&  count($this->_matches) > $maxTerms) {
197                         require_once 'Zend/Search/Lucene/Exception.php';
198                         throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
199                     }
200
201                     $index->nextTerm();
202                 }
203
204                 if ($this->_inclusive  &&  $index->currentTerm() == $upperTerm) {
205                     // Include upper term into result
206                     $this->_matches[] = $upperTerm;
207                 }
208             } else {
209                 // Walk up to the end of field data
210                 while ($index->currentTerm() !== null  &&  $index->currentTerm()->field == $field) {
211                     $this->_matches[] = $index->currentTerm();
212
213                     if ($maxTerms != 0  &&  count($this->_matches) > $maxTerms) {
214                         require_once 'Zend/Search/Lucene/Exception.php';
215                         throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
216                     }
217
218                     $index->nextTerm();
219                 }
220             }
221
222             $index->closeTermsStream();
223         }
224
225         if (count($this->_matches) == 0) {
226             return new Zend_Search_Lucene_Search_Query_Empty();
227         } else if (count($this->_matches) == 1) {
228             return new Zend_Search_Lucene_Search_Query_Term(reset($this->_matches));
229         } else {
230             $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
231
232             foreach ($this->_matches as $matchedTerm) {
233                 $rewrittenQuery->addTerm($matchedTerm);
234             }
235
236             return $rewrittenQuery;
237         }
238     }
239
240     /**
241      * Optimize query in the context of specified index
242      *
243      * @param Zend_Search_Lucene_Interface $index
244      * @return Zend_Search_Lucene_Search_Query
245      */
246     public function optimize(Zend_Search_Lucene_Interface $index)
247     {
248         require_once 'Zend/Search/Lucene/Exception.php';
249         throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)');
250     }
251
252     /**
253      * Return query terms
254      *
255      * @return array
256      * @throws Zend_Search_Lucene_Exception
257      */
258     public function getQueryTerms()
259     {
260         if ($this->_matches === null) {
261             require_once 'Zend/Search/Lucene/Exception.php';
262             throw new Zend_Search_Lucene_Exception('Search or rewrite operations have to be performed before.');
263         }
264
265         return $this->_matches;
266     }
267
268     /**
269      * Constructs an appropriate Weight implementation for this query.
270      *
271      * @param Zend_Search_Lucene_Interface $reader
272      * @return Zend_Search_Lucene_Search_Weight
273      * @throws Zend_Search_Lucene_Exception
274      */
275     public function createWeight(Zend_Search_Lucene_Interface $reader)
276     {
277         require_once 'Zend/Search/Lucene/Exception.php';
278         throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)');
279     }
280
281
282     /**
283      * Execute query in context of index reader
284      * It also initializes necessary internal structures
285      *
286      * @param Zend_Search_Lucene_Interface $reader
287      * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
288      * @throws Zend_Search_Lucene_Exception
289      */
290     public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null)
291     {
292         require_once 'Zend/Search/Lucene/Exception.php';
293         throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)');
294     }
295
296     /**
297      * Get document ids likely matching the query
298      *
299      * It's an array with document ids as keys (performance considerations)
300      *
301      * @return array
302      * @throws Zend_Search_Lucene_Exception
303      */
304     public function matchedDocs()
305     {
306         require_once 'Zend/Search/Lucene/Exception.php';
307         throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)');
308     }
309
310     /**
311      * Score specified document
312      *
313      * @param integer $docId
314      * @param Zend_Search_Lucene_Interface $reader
315      * @return float
316      * @throws Zend_Search_Lucene_Exception
317      */
318     public function score($docId, Zend_Search_Lucene_Interface $reader)
319     {
320         require_once 'Zend/Search/Lucene/Exception.php';
321         throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)');
322     }
323
324     /**
325      * Query specific matches highlighting
326      *
327      * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter  Highlighter object (also contains doc for highlighting)
328      */
329     protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter)
330     {
331         $words = array();
332
333         $docBody = $highlighter->getDocument()->getFieldUtf8Value('body');
334         $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($docBody, 'UTF-8');
335
336         $lowerTermText = ($this->_lowerTerm !== null)? $this->_lowerTerm->text : null;
337         $upperTermText = ($this->_upperTerm !== null)? $this->_upperTerm->text : null;
338
339         if ($this->_inclusive) {
340                 foreach ($tokens as $token) {
341                     $termText = $token->getTermText();
342                     if (($lowerTermText == null  ||  $lowerTermText <= $termText)  &&
343                         ($upperTermText == null  ||  $termText <= $upperTermText)) {
344                         $words[] = $termText;
345                     }
346                 }
347         } else {
348             foreach ($tokens as $token) {
349                 $termText = $token->getTermText();
350                 if (($lowerTermText == null  ||  $lowerTermText < $termText)  &&
351                     ($upperTermText == null  ||  $termText < $upperTermText)) {
352                     $words[] = $termText;
353                 }
354             }
355         }
356
357         $highlighter->highlight($words);
358     }
359
360     /**
361      * Print a query
362      *
363      * @return string
364      */
365     public function __toString()
366     {
367         // It's used only for query visualisation, so we don't care about characters escaping
368         return (($this->_field === null)? '' : $this->_field . ':')
369              . (($this->_inclusive)? '[' : '{')
370              . (($this->_lowerTerm !== null)?  $this->_lowerTerm->text : 'null')
371              . ' TO '
372              . (($this->_upperTerm !== null)?  $this->_upperTerm->text : 'null')
373              . (($this->_inclusive)? ']' : '}')
374              . (($this->getBoost() != 1)? '^' . round($this->getBoost(), 4) : '');
375     }
376 }
377