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: Query.php 16541 2009-07-07 06:59:03Z bkarwin $
23 /** Zend_Search_Lucene_Document_Html */
24 require_once 'Zend/Search/Lucene/Document/Html.php';
26 /** Zend_Search_Lucene_Index_DocsFilter */
27 require_once 'Zend/Search/Lucene/Index/DocsFilter.php';
29 /** Zend_Search_Lucene_Search_Highlighter_Default */
30 require_once 'Zend/Search/Lucene/Search/Highlighter/Default.php';
35 * @package Zend_Search_Lucene
37 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
40 abstract class Zend_Search_Lucene_Search_Query
52 * @var Zend_Search_Lucene_Search_Weight
54 protected $_weight = null;
57 * Current highlight color
61 private $_currentColorIndex = 0;
64 * Gets the boost for this clause. Documents matching
65 * this clause will (in addition to the normal weightings) have their score
66 * multiplied by boost. The boost is 1.0 by default.
70 public function getBoost()
76 * Sets the boost for this query clause to $boost.
80 public function setBoost($boost)
82 $this->_boost = $boost;
86 * Score specified document
88 * @param integer $docId
89 * @param Zend_Search_Lucene_Interface $reader
92 abstract public function score($docId, Zend_Search_Lucene_Interface $reader);
95 * Get document ids likely matching the query
97 * It's an array with document ids as keys (performance considerations)
101 abstract public function matchedDocs();
104 * Execute query in context of index reader
105 * It also initializes necessary internal structures
107 * Query specific implementation
109 * @param Zend_Search_Lucene_Interface $reader
110 * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
112 abstract public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null);
115 * Constructs an appropriate Weight implementation for this query.
117 * @param Zend_Search_Lucene_Interface $reader
118 * @return Zend_Search_Lucene_Search_Weight
120 abstract public function createWeight(Zend_Search_Lucene_Interface $reader);
123 * Constructs an initializes a Weight for a _top-level_query_.
125 * @param Zend_Search_Lucene_Interface $reader
127 protected function _initWeight(Zend_Search_Lucene_Interface $reader)
129 // Check, that it's a top-level query and query weight is not initialized yet.
130 if ($this->_weight !== null) {
131 return $this->_weight;
134 $this->createWeight($reader);
135 $sum = $this->_weight->sumOfSquaredWeights();
136 $queryNorm = $reader->getSimilarity()->queryNorm($sum);
137 $this->_weight->normalize($queryNorm);
141 * Re-write query into primitive queries in the context of specified index
143 * @param Zend_Search_Lucene_Interface $index
144 * @return Zend_Search_Lucene_Search_Query
146 abstract public function rewrite(Zend_Search_Lucene_Interface $index);
149 * Optimize query in the context of specified index
151 * @param Zend_Search_Lucene_Interface $index
152 * @return Zend_Search_Lucene_Search_Query
154 abstract public function optimize(Zend_Search_Lucene_Interface $index);
157 * Reset query, so it can be reused within other queries or
160 public function reset()
162 $this->_weight = null;
171 abstract public function __toString();
178 abstract public function getQueryTerms();
181 * Query specific matches highlighting
183 * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter Highlighter object (also contains doc for highlighting)
185 abstract protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter);
188 * Highlight matches in $inputHTML
190 * @param string $inputHTML
191 * @param string $defaultEncoding HTML encoding, is used if it's not specified using Content-type HTTP-EQUIV meta tag.
192 * @param Zend_Search_Lucene_Search_Highlighter_Interface|null $highlighter
195 public function highlightMatches($inputHTML, $defaultEncoding = '', $highlighter = null)
197 if ($highlighter === null) {
198 $highlighter = new Zend_Search_Lucene_Search_Highlighter_Default();
201 $doc = Zend_Search_Lucene_Document_Html::loadHTML($inputHTML, false, $defaultEncoding);
202 $highlighter->setDocument($doc);
204 $this->_highlightMatches($highlighter);
206 return $doc->getHTML();
210 * Highlight matches in $inputHtmlFragment and return it (without HTML header and body tag)
212 * @param string $inputHtmlFragment
213 * @param string $encoding Input HTML string encoding
214 * @param Zend_Search_Lucene_Search_Highlighter_Interface|null $highlighter
217 public function htmlFragmentHighlightMatches($inputHtmlFragment, $encoding = 'UTF-8', $highlighter = null)
219 if ($highlighter === null) {
220 $highlighter = new Zend_Search_Lucene_Search_Highlighter_Default();
223 $inputHTML = '<html><head><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"/></head><body>'
224 . iconv($encoding, 'UTF-8//IGNORE', $inputHtmlFragment) . '</body></html>';
226 $doc = Zend_Search_Lucene_Document_Html::loadHTML($inputHTML);
227 $highlighter->setDocument($doc);
229 $this->_highlightMatches($highlighter);
231 return $doc->getHtmlBody();