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: Term.php 16541 2009-07-07 06:59:03Z bkarwin $
24 /** Zend_Search_Lucene_Search_Query */
25 require_once 'Zend/Search/Lucene/Search/Query.php';
27 /** Zend_Search_Lucene_Search_Weight_Term */
28 require_once 'Zend/Search/Lucene/Search/Weight/Term.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_Term extends Zend_Search_Lucene_Search_Query
43 * @var Zend_Search_Lucene_Index_Term
52 private $_docVector = null;
56 * array(docId => freq, ...)
64 * Zend_Search_Lucene_Search_Query_Term constructor
66 * @param Zend_Search_Lucene_Index_Term $term
67 * @param boolean $sign
69 public function __construct(Zend_Search_Lucene_Index_Term $term)
75 * Re-write query into primitive queries in the context of specified index
77 * @param Zend_Search_Lucene_Interface $index
78 * @return Zend_Search_Lucene_Search_Query
80 public function rewrite(Zend_Search_Lucene_Interface $index)
82 if ($this->_term->field != null) {
85 $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
86 $query->setBoost($this->getBoost());
88 foreach ($index->getFieldNames(true) as $fieldName) {
89 $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);
91 $query->addTerm($term);
94 return $query->rewrite($index);
99 * Optimize query in the context of specified index
101 * @param Zend_Search_Lucene_Interface $index
102 * @return Zend_Search_Lucene_Search_Query
104 public function optimize(Zend_Search_Lucene_Interface $index)
106 // Check, that index contains specified term
107 if (!$index->hasTerm($this->_term)) {
108 return new Zend_Search_Lucene_Search_Query_Empty();
116 * Constructs an appropriate Weight implementation for this query.
118 * @param Zend_Search_Lucene_Interface $reader
119 * @return Zend_Search_Lucene_Search_Weight
121 public function createWeight(Zend_Search_Lucene_Interface $reader)
123 $this->_weight = new Zend_Search_Lucene_Search_Weight_Term($this->_term, $this, $reader);
124 return $this->_weight;
128 * Execute query in context of index reader
129 * It also initializes necessary internal structures
131 * @param Zend_Search_Lucene_Interface $reader
132 * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
134 public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null)
136 $this->_docVector = array_flip($reader->termDocs($this->_term, $docsFilter));
137 $this->_termFreqs = $reader->termFreqs($this->_term, $docsFilter);
139 // Initialize weight if it's not done yet
140 $this->_initWeight($reader);
144 * Get document ids likely matching the query
146 * It's an array with document ids as keys (performance considerations)
150 public function matchedDocs()
152 return $this->_docVector;
156 * Score specified document
158 * @param integer $docId
159 * @param Zend_Search_Lucene_Interface $reader
162 public function score($docId, Zend_Search_Lucene_Interface $reader)
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) *
179 public function getQueryTerms()
181 return array($this->_term);
187 * @return Zend_Search_Lucene_Index_Term
189 public function getTerm()
195 * Query specific matches highlighting
197 * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter Highlighter object (also contains doc for highlighting)
199 protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter)
201 $highlighter->highlight($this->_term->text);
209 public function __toString()
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 . ':';
218 $query .= $this->_term->text;
220 if ($this->getBoost() != 1) {
221 $query = $query . '^' . round($this->getBoost(), 4);