PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/Zend/Search/Lucene/Search/Query/Term.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 228 lines | 88 code | 28 blank | 112 comment | 10 complexity | 5a1b93deb560ef55a591204a94b9bd86 MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /** Zend_Search_Lucene_Search_Query */
  23. require_once 'Zend/Search/Lucene/Search/Query.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Search_Lucene
  27. * @subpackage Search
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Search_Lucene_Search_Query_Term extends Zend_Search_Lucene_Search_Query
  32. {
  33. /**
  34. * Term to find.
  35. *
  36. * @var Zend_Search_Lucene_Index_Term
  37. */
  38. private $_term;
  39. /**
  40. * Documents vector.
  41. *
  42. * @var array
  43. */
  44. private $_docVector = null;
  45. /**
  46. * Term freqs vector.
  47. * array(docId => freq, ...)
  48. *
  49. * @var array
  50. */
  51. private $_termFreqs;
  52. /**
  53. * Zend_Search_Lucene_Search_Query_Term constructor
  54. *
  55. * @param Zend_Search_Lucene_Index_Term $term
  56. * @param boolean $sign
  57. */
  58. public function __construct(Zend_Search_Lucene_Index_Term $term)
  59. {
  60. $this->_term = $term;
  61. }
  62. /**
  63. * Re-write query into primitive queries in the context of specified index
  64. *
  65. * @param Zend_Search_Lucene_Interface $index
  66. * @return Zend_Search_Lucene_Search_Query
  67. */
  68. public function rewrite(Zend_Search_Lucene_Interface $index)
  69. {
  70. if ($this->_term->field != null) {
  71. return $this;
  72. } else {
  73. require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
  74. $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
  75. $query->setBoost($this->getBoost());
  76. require_once 'Zend/Search/Lucene/Index/Term.php';
  77. foreach ($index->getFieldNames(true) as $fieldName) {
  78. $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);
  79. $query->addTerm($term);
  80. }
  81. return $query->rewrite($index);
  82. }
  83. }
  84. /**
  85. * Optimize query in the context of specified index
  86. *
  87. * @param Zend_Search_Lucene_Interface $index
  88. * @return Zend_Search_Lucene_Search_Query
  89. */
  90. public function optimize(Zend_Search_Lucene_Interface $index)
  91. {
  92. // Check, that index contains specified term
  93. if (!$index->hasTerm($this->_term)) {
  94. require_once 'Zend/Search/Lucene/Search/Query/Empty.php';
  95. return new Zend_Search_Lucene_Search_Query_Empty();
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Constructs an appropriate Weight implementation for this query.
  101. *
  102. * @param Zend_Search_Lucene_Interface $reader
  103. * @return Zend_Search_Lucene_Search_Weight
  104. */
  105. public function createWeight(Zend_Search_Lucene_Interface $reader)
  106. {
  107. require_once 'Zend/Search/Lucene/Search/Weight/Term.php';
  108. $this->_weight = new Zend_Search_Lucene_Search_Weight_Term($this->_term, $this, $reader);
  109. return $this->_weight;
  110. }
  111. /**
  112. * Execute query in context of index reader
  113. * It also initializes necessary internal structures
  114. *
  115. * @param Zend_Search_Lucene_Interface $reader
  116. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  117. */
  118. public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null)
  119. {
  120. $this->_docVector = array_flip($reader->termDocs($this->_term, $docsFilter));
  121. $this->_termFreqs = $reader->termFreqs($this->_term, $docsFilter);
  122. // Initialize weight if it's not done yet
  123. $this->_initWeight($reader);
  124. }
  125. /**
  126. * Get document ids likely matching the query
  127. *
  128. * It's an array with document ids as keys (performance considerations)
  129. *
  130. * @return array
  131. */
  132. public function matchedDocs()
  133. {
  134. return $this->_docVector;
  135. }
  136. /**
  137. * Score specified document
  138. *
  139. * @param integer $docId
  140. * @param Zend_Search_Lucene_Interface $reader
  141. * @return float
  142. */
  143. public function score($docId, Zend_Search_Lucene_Interface $reader)
  144. {
  145. if (isset($this->_docVector[$docId])) {
  146. return $reader->getSimilarity()->tf($this->_termFreqs[$docId]) *
  147. $this->_weight->getValue() *
  148. $reader->norm($docId, $this->_term->field) *
  149. $this->getBoost();
  150. } else {
  151. return 0;
  152. }
  153. }
  154. /**
  155. * Return query terms
  156. *
  157. * @return array
  158. */
  159. public function getQueryTerms()
  160. {
  161. return array($this->_term);
  162. }
  163. /**
  164. * Return query term
  165. *
  166. * @return Zend_Search_Lucene_Index_Term
  167. */
  168. public function getTerm()
  169. {
  170. return $this->_term;
  171. }
  172. /**
  173. * Query specific matches highlighting
  174. *
  175. * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter Highlighter object (also contains doc for highlighting)
  176. */
  177. protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter)
  178. {
  179. $highlighter->highlight($this->_term->text);
  180. }
  181. /**
  182. * Print a query
  183. *
  184. * @return string
  185. */
  186. public function __toString()
  187. {
  188. // It's used only for query visualisation, so we don't care about characters escaping
  189. if ($this->_term->field !== null) {
  190. $query = $this->_term->field . ':';
  191. } else {
  192. $query = '';
  193. }
  194. $query .= $this->_term->text;
  195. if ($this->getBoost() != 1) {
  196. $query = $query . '^' . round($this->getBoost(), 4);
  197. }
  198. return $query;
  199. }
  200. }