import
[web.mtrack] / inc / lib / Zend / Search / Lucene / Search / Query.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: Query.php 16541 2009-07-07 06:59:03Z bkarwin $
21  */
22
23 /** Zend_Search_Lucene_Document_Html */
24 require_once 'Zend/Search/Lucene/Document/Html.php';
25
26 /** Zend_Search_Lucene_Index_DocsFilter */
27 require_once 'Zend/Search/Lucene/Index/DocsFilter.php';
28
29 /** Zend_Search_Lucene_Search_Highlighter_Default */
30 require_once 'Zend/Search/Lucene/Search/Highlighter/Default.php';
31
32
33 /**
34  * @category   Zend
35  * @package    Zend_Search_Lucene
36  * @subpackage Search
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
39  */
40 abstract class Zend_Search_Lucene_Search_Query
41 {
42     /**
43      * query boost factor
44      *
45      * @var float
46      */
47     private $_boost = 1;
48
49     /**
50      * Query weight
51      *
52      * @var Zend_Search_Lucene_Search_Weight
53      */
54     protected $_weight = null;
55
56     /**
57      * Current highlight color
58      *
59      * @var integer
60      */
61     private $_currentColorIndex = 0;
62
63     /**
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.
67      *
68      * @return float
69      */
70     public function getBoost()
71     {
72         return $this->_boost;
73     }
74
75     /**
76      * Sets the boost for this query clause to $boost.
77      *
78      * @param float $boost
79      */
80     public function setBoost($boost)
81     {
82         $this->_boost = $boost;
83     }
84
85     /**
86      * Score specified document
87      *
88      * @param integer $docId
89      * @param Zend_Search_Lucene_Interface $reader
90      * @return float
91      */
92     abstract public function score($docId, Zend_Search_Lucene_Interface $reader);
93
94     /**
95      * Get document ids likely matching the query
96      *
97      * It's an array with document ids as keys (performance considerations)
98      *
99      * @return array
100      */
101     abstract public function matchedDocs();
102
103     /**
104      * Execute query in context of index reader
105      * It also initializes necessary internal structures
106      *
107      * Query specific implementation
108      *
109      * @param Zend_Search_Lucene_Interface $reader
110      * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
111      */
112     abstract public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null);
113
114     /**
115      * Constructs an appropriate Weight implementation for this query.
116      *
117      * @param Zend_Search_Lucene_Interface $reader
118      * @return Zend_Search_Lucene_Search_Weight
119      */
120     abstract public function createWeight(Zend_Search_Lucene_Interface $reader);
121
122     /**
123      * Constructs an initializes a Weight for a _top-level_query_.
124      *
125      * @param Zend_Search_Lucene_Interface $reader
126      */
127     protected function _initWeight(Zend_Search_Lucene_Interface $reader)
128     {
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;
132         }
133
134         $this->createWeight($reader);
135         $sum = $this->_weight->sumOfSquaredWeights();
136         $queryNorm = $reader->getSimilarity()->queryNorm($sum);
137         $this->_weight->normalize($queryNorm);
138     }
139
140     /**
141      * Re-write query into primitive queries in the context of specified index
142      *
143      * @param Zend_Search_Lucene_Interface $index
144      * @return Zend_Search_Lucene_Search_Query
145      */
146     abstract public function rewrite(Zend_Search_Lucene_Interface $index);
147
148     /**
149      * Optimize query in the context of specified index
150      *
151      * @param Zend_Search_Lucene_Interface $index
152      * @return Zend_Search_Lucene_Search_Query
153      */
154     abstract public function optimize(Zend_Search_Lucene_Interface $index);
155
156     /**
157      * Reset query, so it can be reused within other queries or
158      * with other indeces
159      */
160     public function reset()
161     {
162         $this->_weight = null;
163     }
164
165
166     /**
167      * Print a query
168      *
169      * @return string
170      */
171     abstract public function __toString();
172
173     /**
174      * Return query terms
175      *
176      * @return array
177      */
178     abstract public function getQueryTerms();
179
180     /**
181      * Query specific matches highlighting
182      *
183      * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter  Highlighter object (also contains doc for highlighting)
184      */
185     abstract protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter);
186
187     /**
188      * Highlight matches in $inputHTML
189      *
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
193      * @return string
194      */
195     public function highlightMatches($inputHTML, $defaultEncoding = '', $highlighter = null)
196     {
197         if ($highlighter === null) {
198                 $highlighter = new Zend_Search_Lucene_Search_Highlighter_Default();
199         }
200
201         $doc = Zend_Search_Lucene_Document_Html::loadHTML($inputHTML, false, $defaultEncoding);
202         $highlighter->setDocument($doc);
203
204         $this->_highlightMatches($highlighter);
205
206         return $doc->getHTML();
207     }
208
209     /**
210      * Highlight matches in $inputHtmlFragment and return it (without HTML header and body tag)
211      *
212      * @param string $inputHtmlFragment
213      * @param string  $encoding   Input HTML string encoding
214      * @param Zend_Search_Lucene_Search_Highlighter_Interface|null $highlighter
215      * @return string
216      */
217     public function htmlFragmentHighlightMatches($inputHtmlFragment, $encoding = 'UTF-8', $highlighter = null)
218     {
219         if ($highlighter === null) {
220             $highlighter = new Zend_Search_Lucene_Search_Highlighter_Default();
221         }
222
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>';
225
226         $doc = Zend_Search_Lucene_Document_Html::loadHTML($inputHTML);
227         $highlighter->setDocument($doc);
228
229         $this->_highlightMatches($highlighter);
230
231         return $doc->getHtmlBody();
232     }
233 }
234