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.
16 * @package Zend_Search_Lucene
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 $
24 /** Zend_Search_Lucene_Search_Query */
25 require_once 'Zend/Search/Lucene/Search/Query.php';
27 /** Zend_Search_Lucene_Search_Query_MultiTerm */
28 require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
33 * @package Zend_Search_Lucene
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
38 class Zend_Search_Lucene_Search_Query_Range extends Zend_Search_Lucene_Search_Query
43 * @var Zend_Search_Lucene_Index_Term
50 * @var Zend_Search_Lucene_Index_Term
73 * It's filled during the search (rewrite operation) and may be used for search result
76 * Array of Zend_Search_Lucene_Index_Term objects
80 private $_matches = null;
84 * Zend_Search_Lucene_Search_Query_Range constructor.
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
91 public function __construct($lowerTerm, $upperTerm, $inclusive)
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');
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');
102 $this->_field = ($lowerTerm !== null)? $lowerTerm->field : $upperTerm->field;
103 $this->_lowerTerm = $lowerTerm;
104 $this->_upperTerm = $upperTerm;
105 $this->_inclusive = $inclusive;
109 * Get query field name
111 * @return string|null
113 public function getField()
115 return $this->_field;
121 * @return Zend_Search_Lucene_Index_Term|null
123 public function getLowerTerm()
125 return $this->_lowerTerm;
131 * @return Zend_Search_Lucene_Index_Term|null
133 public function getUpperTerm()
135 return $this->_upperTerm;
143 public function isInclusive()
145 return $this->_inclusive;
149 * Re-write query into primitive queries in the context of specified index
151 * @param Zend_Search_Lucene_Interface $index
152 * @return Zend_Search_Lucene_Search_Query
154 public function rewrite(Zend_Search_Lucene_Interface $index)
156 $this->_matches = array();
158 if ($this->_field === null) {
159 // Search through all fields
160 $fields = $index->getFieldNames(true /* indexed fields list */);
162 $fields = array($this->_field);
165 $maxTerms = Zend_Search_Lucene::getTermsPerQueryLimit();
166 foreach ($fields as $field) {
167 $index->resetTermsStream();
169 if ($this->_lowerTerm !== null) {
170 $lowerTerm = new Zend_Search_Lucene_Index_Term($this->_lowerTerm->text, $field);
172 $index->skipTo($lowerTerm);
174 if (!$this->_inclusive &&
175 $index->currentTerm() == $lowerTerm) {
180 $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
184 if ($this->_upperTerm !== null) {
185 // Walk up to the upper term
186 $upperTerm = new Zend_Search_Lucene_Index_Term($this->_upperTerm->text, $field);
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
194 $this->_matches[] = $index->currentTerm();
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.');
204 if ($this->_inclusive && $index->currentTerm() == $upperTerm) {
205 // Include upper term into result
206 $this->_matches[] = $upperTerm;
209 // Walk up to the end of field data
210 while ($index->currentTerm() !== null && $index->currentTerm()->field == $field) {
211 $this->_matches[] = $index->currentTerm();
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.');
222 $index->closeTermsStream();
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));
230 $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
232 foreach ($this->_matches as $matchedTerm) {
233 $rewrittenQuery->addTerm($matchedTerm);
236 return $rewrittenQuery;
241 * Optimize query in the context of specified index
243 * @param Zend_Search_Lucene_Interface $index
244 * @return Zend_Search_Lucene_Search_Query
246 public function optimize(Zend_Search_Lucene_Interface $index)
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)');
256 * @throws Zend_Search_Lucene_Exception
258 public function getQueryTerms()
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.');
265 return $this->_matches;
269 * Constructs an appropriate Weight implementation for this query.
271 * @param Zend_Search_Lucene_Interface $reader
272 * @return Zend_Search_Lucene_Search_Weight
273 * @throws Zend_Search_Lucene_Exception
275 public function createWeight(Zend_Search_Lucene_Interface $reader)
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)');
283 * Execute query in context of index reader
284 * It also initializes necessary internal structures
286 * @param Zend_Search_Lucene_Interface $reader
287 * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
288 * @throws Zend_Search_Lucene_Exception
290 public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null)
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)');
297 * Get document ids likely matching the query
299 * It's an array with document ids as keys (performance considerations)
302 * @throws Zend_Search_Lucene_Exception
304 public function matchedDocs()
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)');
311 * Score specified document
313 * @param integer $docId
314 * @param Zend_Search_Lucene_Interface $reader
316 * @throws Zend_Search_Lucene_Exception
318 public function score($docId, Zend_Search_Lucene_Interface $reader)
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)');
325 * Query specific matches highlighting
327 * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter Highlighter object (also contains doc for highlighting)
329 protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter)
333 $docBody = $highlighter->getDocument()->getFieldUtf8Value('body');
334 $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($docBody, 'UTF-8');
336 $lowerTermText = ($this->_lowerTerm !== null)? $this->_lowerTerm->text : null;
337 $upperTermText = ($this->_upperTerm !== null)? $this->_upperTerm->text : null;
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;
348 foreach ($tokens as $token) {
349 $termText = $token->getTermText();
350 if (($lowerTermText == null || $lowerTermText < $termText) &&
351 ($upperTermText == null || $termText < $upperTermText)) {
352 $words[] = $termText;
357 $highlighter->highlight($words);
365 public function __toString()
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')
372 . (($this->_upperTerm !== null)? $this->_upperTerm->text : 'null')
373 . (($this->_inclusive)? ']' : '}')
374 . (($this->getBoost() != 1)? '^' . round($this->getBoost(), 4) : '');