PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/shopaholic/lib/Zend/Search/Lucene/Search/Query/Boolean.php

http://github.com/jakubkulhan/shopaholic
PHP | 806 lines | 429 code | 126 blank | 251 comment | 112 complexity | b905051c0876d7fdd8708e726448a12b MD5 | raw file
Possible License(s): WTFPL
  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: Boolean.php 16541 2009-07-07 06:59:03Z bkarwin $
  21. */
  22. /** Zend_Search_Lucene_Search_Query */
  23. require_once 'Zend/Search/Lucene/Search/Query.php';
  24. /** Zend_Search_Lucene_Search_Weight_Boolean */
  25. require_once 'Zend/Search/Lucene/Search/Weight/Boolean.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Search_Lucene
  29. * @subpackage Search
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Search_Lucene_Search_Query_Boolean extends Zend_Search_Lucene_Search_Query
  34. {
  35. /**
  36. * Subqueries
  37. * Array of Zend_Search_Lucene_Search_Query
  38. *
  39. * @var array
  40. */
  41. private $_subqueries = array();
  42. /**
  43. * Subqueries signs.
  44. * If true then subquery is required.
  45. * If false then subquery is prohibited.
  46. * If null then subquery is neither prohibited, nor required
  47. *
  48. * If array is null then all subqueries are required
  49. *
  50. * @var array
  51. */
  52. private $_signs = array();
  53. /**
  54. * Result vector.
  55. *
  56. * @var array
  57. */
  58. private $_resVector = null;
  59. /**
  60. * A score factor based on the fraction of all query subqueries
  61. * that a document contains.
  62. * float for conjunction queries
  63. * array of float for non conjunction queries
  64. *
  65. * @var mixed
  66. */
  67. private $_coord = null;
  68. /**
  69. * Class constructor. Create a new Boolean query object.
  70. *
  71. * if $signs array is omitted then all subqueries are required
  72. * it differs from addSubquery() behavior, but should never be used
  73. *
  74. * @param array $subqueries Array of Zend_Search_Search_Query objects
  75. * @param array $signs Array of signs. Sign is boolean|null.
  76. * @return void
  77. */
  78. public function __construct($subqueries = null, $signs = null)
  79. {
  80. if (is_array($subqueries)) {
  81. $this->_subqueries = $subqueries;
  82. $this->_signs = null;
  83. // Check if all subqueries are required
  84. if (is_array($signs)) {
  85. foreach ($signs as $sign ) {
  86. if ($sign !== true) {
  87. $this->_signs = $signs;
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * Add a $subquery (Zend_Search_Lucene_Search_Query) to this query.
  96. *
  97. * The sign is specified as:
  98. * TRUE - subquery is required
  99. * FALSE - subquery is prohibited
  100. * NULL - subquery is neither prohibited, nor required
  101. *
  102. * @param Zend_Search_Lucene_Search_Query $subquery
  103. * @param boolean|null $sign
  104. * @return void
  105. */
  106. public function addSubquery(Zend_Search_Lucene_Search_Query $subquery, $sign=null) {
  107. if ($sign !== true || $this->_signs !== null) { // Skip, if all subqueries are required
  108. if ($this->_signs === null) { // Check, If all previous subqueries are required
  109. $this->_signs = array();
  110. foreach ($this->_subqueries as $prevSubquery) {
  111. $this->_signs[] = true;
  112. }
  113. }
  114. $this->_signs[] = $sign;
  115. }
  116. $this->_subqueries[] = $subquery;
  117. }
  118. /**
  119. * Re-write queries into primitive queries
  120. *
  121. * @param Zend_Search_Lucene_Interface $index
  122. * @return Zend_Search_Lucene_Search_Query
  123. */
  124. public function rewrite(Zend_Search_Lucene_Interface $index)
  125. {
  126. $query = new Zend_Search_Lucene_Search_Query_Boolean();
  127. $query->setBoost($this->getBoost());
  128. foreach ($this->_subqueries as $subqueryId => $subquery) {
  129. $query->addSubquery($subquery->rewrite($index),
  130. ($this->_signs === null)? true : $this->_signs[$subqueryId]);
  131. }
  132. return $query;
  133. }
  134. /**
  135. * Optimize query in the context of specified index
  136. *
  137. * @param Zend_Search_Lucene_Interface $index
  138. * @return Zend_Search_Lucene_Search_Query
  139. */
  140. public function optimize(Zend_Search_Lucene_Interface $index)
  141. {
  142. $subqueries = array();
  143. $signs = array();
  144. // Optimize all subqueries
  145. foreach ($this->_subqueries as $id => $subquery) {
  146. $subqueries[] = $subquery->optimize($index);
  147. $signs[] = ($this->_signs === null)? true : $this->_signs[$id];
  148. }
  149. // Remove insignificant subqueries
  150. foreach ($subqueries as $id => $subquery) {
  151. if ($subquery instanceof Zend_Search_Lucene_Search_Query_Insignificant) {
  152. // Insignificant subquery has to be removed anyway
  153. unset($subqueries[$id]);
  154. unset($signs[$id]);
  155. }
  156. }
  157. if (count($subqueries) == 0) {
  158. // Boolean query doesn't has non-insignificant subqueries
  159. return new Zend_Search_Lucene_Search_Query_Insignificant();
  160. }
  161. // Check if all non-insignificant subqueries are prohibited
  162. $allProhibited = true;
  163. foreach ($signs as $sign) {
  164. if ($sign !== false) {
  165. $allProhibited = false;
  166. break;
  167. }
  168. }
  169. if ($allProhibited) {
  170. return new Zend_Search_Lucene_Search_Query_Insignificant();
  171. }
  172. // Check for empty subqueries
  173. foreach ($subqueries as $id => $subquery) {
  174. if ($subquery instanceof Zend_Search_Lucene_Search_Query_Empty) {
  175. if ($signs[$id] === true) {
  176. // Matching is required, but is actually empty
  177. return new Zend_Search_Lucene_Search_Query_Empty();
  178. } else {
  179. // Matching is optional or prohibited, but is empty
  180. // Remove it from subqueries and signs list
  181. unset($subqueries[$id]);
  182. unset($signs[$id]);
  183. }
  184. }
  185. }
  186. // Check, if reduced subqueries list is empty
  187. if (count($subqueries) == 0) {
  188. return new Zend_Search_Lucene_Search_Query_Empty();
  189. }
  190. // Check if all non-empty subqueries are prohibited
  191. $allProhibited = true;
  192. foreach ($signs as $sign) {
  193. if ($sign !== false) {
  194. $allProhibited = false;
  195. break;
  196. }
  197. }
  198. if ($allProhibited) {
  199. return new Zend_Search_Lucene_Search_Query_Empty();
  200. }
  201. // Check, if reduced subqueries list has only one entry
  202. if (count($subqueries) == 1) {
  203. // It's a query with only one required or optional clause
  204. // (it's already checked, that it's not a prohibited clause)
  205. if ($this->getBoost() == 1) {
  206. return reset($subqueries);
  207. }
  208. $optimizedQuery = clone reset($subqueries);
  209. $optimizedQuery->setBoost($optimizedQuery->getBoost()*$this->getBoost());
  210. return $optimizedQuery;
  211. }
  212. // Prepare first candidate for optimized query
  213. $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
  214. $optimizedQuery->setBoost($this->getBoost());
  215. $terms = array();
  216. $tsigns = array();
  217. $boostFactors = array();
  218. // Try to decompose term and multi-term subqueries
  219. foreach ($subqueries as $id => $subquery) {
  220. if ($subquery instanceof Zend_Search_Lucene_Search_Query_Term) {
  221. $terms[] = $subquery->getTerm();
  222. $tsigns[] = $signs[$id];
  223. $boostFactors[] = $subquery->getBoost();
  224. // remove subquery from a subqueries list
  225. unset($subqueries[$id]);
  226. unset($signs[$id]);
  227. } else if ($subquery instanceof Zend_Search_Lucene_Search_Query_MultiTerm) {
  228. $subTerms = $subquery->getTerms();
  229. $subSigns = $subquery->getSigns();
  230. if ($signs[$id] === true) {
  231. // It's a required multi-term subquery.
  232. // Something like '... +(+term1 -term2 term3 ...) ...'
  233. // Multi-term required subquery can be decomposed only if it contains
  234. // required terms and doesn't contain prohibited terms:
  235. // ... +(+term1 term2 ...) ... => ... +term1 term2 ...
  236. //
  237. // Check this
  238. $hasRequired = false;
  239. $hasProhibited = false;
  240. if ($subSigns === null) {
  241. // All subterms are required
  242. $hasRequired = true;
  243. } else {
  244. foreach ($subSigns as $sign) {
  245. if ($sign === true) {
  246. $hasRequired = true;
  247. } else if ($sign === false) {
  248. $hasProhibited = true;
  249. break;
  250. }
  251. }
  252. }
  253. // Continue if subquery has prohibited terms or doesn't have required terms
  254. if ($hasProhibited || !$hasRequired) {
  255. continue;
  256. }
  257. foreach ($subTerms as $termId => $term) {
  258. $terms[] = $term;
  259. $tsigns[] = ($subSigns === null)? true : $subSigns[$termId];
  260. $boostFactors[] = $subquery->getBoost();
  261. }
  262. // remove subquery from a subqueries list
  263. unset($subqueries[$id]);
  264. unset($signs[$id]);
  265. } else { // $signs[$id] === null || $signs[$id] === false
  266. // It's an optional or prohibited multi-term subquery.
  267. // Something like '... (+term1 -term2 term3 ...) ...'
  268. // or
  269. // something like '... -(+term1 -term2 term3 ...) ...'
  270. // Multi-term optional and required subqueries can be decomposed
  271. // only if all terms are optional.
  272. //
  273. // Check if all terms are optional.
  274. $onlyOptional = true;
  275. if ($subSigns === null) {
  276. // All subterms are required
  277. $onlyOptional = false;
  278. } else {
  279. foreach ($subSigns as $sign) {
  280. if ($sign !== null) {
  281. $onlyOptional = false;
  282. break;
  283. }
  284. }
  285. }
  286. // Continue if non-optional terms are presented in this multi-term subquery
  287. if (!$onlyOptional) {
  288. continue;
  289. }
  290. foreach ($subTerms as $termId => $term) {
  291. $terms[] = $term;
  292. $tsigns[] = ($signs[$id] === null)? null /* optional */ :
  293. false /* prohibited */;
  294. $boostFactors[] = $subquery->getBoost();
  295. }
  296. // remove subquery from a subqueries list
  297. unset($subqueries[$id]);
  298. unset($signs[$id]);
  299. }
  300. }
  301. }
  302. // Check, if there are no decomposed subqueries
  303. if (count($terms) == 0 ) {
  304. // return prepared candidate
  305. return $optimizedQuery;
  306. }
  307. // Check, if all subqueries have been decomposed and all terms has the same boost factor
  308. if (count($subqueries) == 0 && count(array_unique($boostFactors)) == 1) {
  309. $optimizedQuery = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns);
  310. $optimizedQuery->setBoost(reset($boostFactors)*$this->getBoost());
  311. return $optimizedQuery;
  312. }
  313. // This boolean query can't be transformed to Term/MultiTerm query and still contains
  314. // several subqueries
  315. // Separate prohibited terms
  316. $prohibitedTerms = array();
  317. foreach ($terms as $id => $term) {
  318. if ($tsigns[$id] === false) {
  319. $prohibitedTerms[] = $term;
  320. unset($terms[$id]);
  321. unset($tsigns[$id]);
  322. unset($boostFactors[$id]);
  323. }
  324. }
  325. if (count($terms) == 1) {
  326. $clause = new Zend_Search_Lucene_Search_Query_Term(reset($terms));
  327. $clause->setBoost(reset($boostFactors));
  328. $subqueries[] = $clause;
  329. $signs[] = reset($tsigns);
  330. // Clear terms list
  331. $terms = array();
  332. } else if (count($terms) > 1 && count(array_unique($boostFactors)) == 1) {
  333. $clause = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns);
  334. $clause->setBoost(reset($boostFactors));
  335. $subqueries[] = $clause;
  336. // Clause sign is 'required' if clause contains required terms. 'Optional' otherwise.
  337. $signs[] = (in_array(true, $tsigns))? true : null;
  338. // Clear terms list
  339. $terms = array();
  340. }
  341. if (count($prohibitedTerms) == 1) {
  342. // (boost factors are not significant for prohibited clauses)
  343. $subqueries[] = new Zend_Search_Lucene_Search_Query_Term(reset($prohibitedTerms));
  344. $signs[] = false;
  345. // Clear prohibited terms list
  346. $prohibitedTerms = array();
  347. } else if (count($prohibitedTerms) > 1) {
  348. // prepare signs array
  349. $prohibitedSigns = array();
  350. foreach ($prohibitedTerms as $id => $term) {
  351. // all prohibited term are grouped as optional into multi-term query
  352. $prohibitedSigns[$id] = null;
  353. }
  354. // (boost factors are not significant for prohibited clauses)
  355. $subqueries[] = new Zend_Search_Lucene_Search_Query_MultiTerm($prohibitedTerms, $prohibitedSigns);
  356. // Clause sign is 'prohibited'
  357. $signs[] = false;
  358. // Clear terms list
  359. $prohibitedTerms = array();
  360. }
  361. /** @todo Group terms with the same boost factors together */
  362. // Check, that all terms are processed
  363. // Replace candidate for optimized query
  364. if (count($terms) == 0 && count($prohibitedTerms) == 0) {
  365. $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
  366. $optimizedQuery->setBoost($this->getBoost());
  367. }
  368. return $optimizedQuery;
  369. }
  370. /**
  371. * Returns subqueries
  372. *
  373. * @return array
  374. */
  375. public function getSubqueries()
  376. {
  377. return $this->_subqueries;
  378. }
  379. /**
  380. * Return subqueries signs
  381. *
  382. * @return array
  383. */
  384. public function getSigns()
  385. {
  386. return $this->_signs;
  387. }
  388. /**
  389. * Constructs an appropriate Weight implementation for this query.
  390. *
  391. * @param Zend_Search_Lucene_Interface $reader
  392. * @return Zend_Search_Lucene_Search_Weight
  393. */
  394. public function createWeight(Zend_Search_Lucene_Interface $reader)
  395. {
  396. $this->_weight = new Zend_Search_Lucene_Search_Weight_Boolean($this, $reader);
  397. return $this->_weight;
  398. }
  399. /**
  400. * Calculate result vector for Conjunction query
  401. * (like '<subquery1> AND <subquery2> AND <subquery3>')
  402. */
  403. private function _calculateConjunctionResult()
  404. {
  405. $this->_resVector = null;
  406. if (count($this->_subqueries) == 0) {
  407. $this->_resVector = array();
  408. }
  409. $resVectors = array();
  410. $resVectorsSizes = array();
  411. $resVectorsIds = array(); // is used to prevent arrays comparison
  412. foreach ($this->_subqueries as $subqueryId => $subquery) {
  413. $resVectors[] = $subquery->matchedDocs();
  414. $resVectorsSizes[] = count(end($resVectors));
  415. $resVectorsIds[] = $subqueryId;
  416. }
  417. // sort resvectors in order of subquery cardinality increasing
  418. array_multisort($resVectorsSizes, SORT_ASC, SORT_NUMERIC,
  419. $resVectorsIds, SORT_ASC, SORT_NUMERIC,
  420. $resVectors);
  421. foreach ($resVectors as $nextResVector) {
  422. if($this->_resVector === null) {
  423. $this->_resVector = $nextResVector;
  424. } else {
  425. //$this->_resVector = array_intersect_key($this->_resVector, $nextResVector);
  426. /**
  427. * This code is used as workaround for array_intersect_key() slowness problem.
  428. */
  429. $updatedVector = array();
  430. foreach ($this->_resVector as $id => $value) {
  431. if (isset($nextResVector[$id])) {
  432. $updatedVector[$id] = $value;
  433. }
  434. }
  435. $this->_resVector = $updatedVector;
  436. }
  437. if (count($this->_resVector) == 0) {
  438. // Empty result set, we don't need to check other terms
  439. break;
  440. }
  441. }
  442. // ksort($this->_resVector, SORT_NUMERIC);
  443. // Used algorithm doesn't change elements order
  444. }
  445. /**
  446. * Calculate result vector for non Conjunction query
  447. * (like '<subquery1> AND <subquery2> AND NOT <subquery3> OR <subquery4>')
  448. */
  449. private function _calculateNonConjunctionResult()
  450. {
  451. $requiredVectors = array();
  452. $requiredVectorsSizes = array();
  453. $requiredVectorsIds = array(); // is used to prevent arrays comparison
  454. $optional = array();
  455. foreach ($this->_subqueries as $subqueryId => $subquery) {
  456. if ($this->_signs[$subqueryId] === true) {
  457. // required
  458. $requiredVectors[] = $subquery->matchedDocs();
  459. $requiredVectorsSizes[] = count(end($requiredVectors));
  460. $requiredVectorsIds[] = $subqueryId;
  461. } elseif ($this->_signs[$subqueryId] === false) {
  462. // prohibited
  463. // Do nothing. matchedDocs() may include non-matching id's
  464. // Calculating prohibited vector may take significant time, but do not affect the result
  465. // Skipped.
  466. } else {
  467. // neither required, nor prohibited
  468. // array union
  469. $optional += $subquery->matchedDocs();
  470. }
  471. }
  472. // sort resvectors in order of subquery cardinality increasing
  473. array_multisort($requiredVectorsSizes, SORT_ASC, SORT_NUMERIC,
  474. $requiredVectorsIds, SORT_ASC, SORT_NUMERIC,
  475. $requiredVectors);
  476. $required = null;
  477. foreach ($requiredVectors as $nextResVector) {
  478. if($required === null) {
  479. $required = $nextResVector;
  480. } else {
  481. //$required = array_intersect_key($required, $nextResVector);
  482. /**
  483. * This code is used as workaround for array_intersect_key() slowness problem.
  484. */
  485. $updatedVector = array();
  486. foreach ($required as $id => $value) {
  487. if (isset($nextResVector[$id])) {
  488. $updatedVector[$id] = $value;
  489. }
  490. }
  491. $required = $updatedVector;
  492. }
  493. if (count($required) == 0) {
  494. // Empty result set, we don't need to check other terms
  495. break;
  496. }
  497. }
  498. if ($required !== null) {
  499. $this->_resVector = &$required;
  500. } else {
  501. $this->_resVector = &$optional;
  502. }
  503. ksort($this->_resVector, SORT_NUMERIC);
  504. }
  505. /**
  506. * Score calculator for conjunction queries (all subqueries are required)
  507. *
  508. * @param integer $docId
  509. * @param Zend_Search_Lucene_Interface $reader
  510. * @return float
  511. */
  512. public function _conjunctionScore($docId, Zend_Search_Lucene_Interface $reader)
  513. {
  514. if ($this->_coord === null) {
  515. $this->_coord = $reader->getSimilarity()->coord(count($this->_subqueries),
  516. count($this->_subqueries) );
  517. }
  518. $score = 0;
  519. foreach ($this->_subqueries as $subquery) {
  520. $subscore = $subquery->score($docId, $reader);
  521. if ($subscore == 0) {
  522. return 0;
  523. }
  524. $score += $subquery->score($docId, $reader) * $this->_coord;
  525. }
  526. return $score * $this->_coord * $this->getBoost();
  527. }
  528. /**
  529. * Score calculator for non conjunction queries (not all subqueries are required)
  530. *
  531. * @param integer $docId
  532. * @param Zend_Search_Lucene_Interface $reader
  533. * @return float
  534. */
  535. public function _nonConjunctionScore($docId, Zend_Search_Lucene_Interface $reader)
  536. {
  537. if ($this->_coord === null) {
  538. $this->_coord = array();
  539. $maxCoord = 0;
  540. foreach ($this->_signs as $sign) {
  541. if ($sign !== false /* not prohibited */) {
  542. $maxCoord++;
  543. }
  544. }
  545. for ($count = 0; $count <= $maxCoord; $count++) {
  546. $this->_coord[$count] = $reader->getSimilarity()->coord($count, $maxCoord);
  547. }
  548. }
  549. $score = 0;
  550. $matchedSubqueries = 0;
  551. foreach ($this->_subqueries as $subqueryId => $subquery) {
  552. $subscore = $subquery->score($docId, $reader);
  553. // Prohibited
  554. if ($this->_signs[$subqueryId] === false && $subscore != 0) {
  555. return 0;
  556. }
  557. // is required, but doen't match
  558. if ($this->_signs[$subqueryId] === true && $subscore == 0) {
  559. return 0;
  560. }
  561. if ($subscore != 0) {
  562. $matchedSubqueries++;
  563. $score += $subscore;
  564. }
  565. }
  566. return $score * $this->_coord[$matchedSubqueries] * $this->getBoost();
  567. }
  568. /**
  569. * Execute query in context of index reader
  570. * It also initializes necessary internal structures
  571. *
  572. * @param Zend_Search_Lucene_Interface $reader
  573. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  574. */
  575. public function execute(Zend_Search_Lucene_Interface $reader, $docsFilter = null)
  576. {
  577. // Initialize weight if it's not done yet
  578. $this->_initWeight($reader);
  579. if ($docsFilter === null) {
  580. // Create local documents filter if it's not provided by upper query
  581. $docsFilter = new Zend_Search_Lucene_Index_DocsFilter();
  582. }
  583. foreach ($this->_subqueries as $subqueryId => $subquery) {
  584. if ($this->_signs == null || $this->_signs[$subqueryId] === true) {
  585. // Subquery is required
  586. $subquery->execute($reader, $docsFilter);
  587. } else {
  588. $subquery->execute($reader);
  589. }
  590. }
  591. if ($this->_signs === null) {
  592. $this->_calculateConjunctionResult();
  593. } else {
  594. $this->_calculateNonConjunctionResult();
  595. }
  596. }
  597. /**
  598. * Get document ids likely matching the query
  599. *
  600. * It's an array with document ids as keys (performance considerations)
  601. *
  602. * @return array
  603. */
  604. public function matchedDocs()
  605. {
  606. return $this->_resVector;
  607. }
  608. /**
  609. * Score specified document
  610. *
  611. * @param integer $docId
  612. * @param Zend_Search_Lucene_Interface $reader
  613. * @return float
  614. */
  615. public function score($docId, Zend_Search_Lucene_Interface $reader)
  616. {
  617. if (isset($this->_resVector[$docId])) {
  618. if ($this->_signs === null) {
  619. return $this->_conjunctionScore($docId, $reader);
  620. } else {
  621. return $this->_nonConjunctionScore($docId, $reader);
  622. }
  623. } else {
  624. return 0;
  625. }
  626. }
  627. /**
  628. * Return query terms
  629. *
  630. * @return array
  631. */
  632. public function getQueryTerms()
  633. {
  634. $terms = array();
  635. foreach ($this->_subqueries as $id => $subquery) {
  636. if ($this->_signs === null || $this->_signs[$id] !== false) {
  637. $terms = array_merge($terms, $subquery->getQueryTerms());
  638. }
  639. }
  640. return $terms;
  641. }
  642. /**
  643. * Query specific matches highlighting
  644. *
  645. * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter Highlighter object (also contains doc for highlighting)
  646. */
  647. protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter)
  648. {
  649. foreach ($this->_subqueries as $id => $subquery) {
  650. if ($this->_signs === null || $this->_signs[$id] !== false) {
  651. $subquery->_highlightMatches($highlighter);
  652. }
  653. }
  654. }
  655. /**
  656. * Print a query
  657. *
  658. * @return string
  659. */
  660. public function __toString()
  661. {
  662. // It's used only for query visualisation, so we don't care about characters escaping
  663. $query = '';
  664. foreach ($this->_subqueries as $id => $subquery) {
  665. if ($id != 0) {
  666. $query .= ' ';
  667. }
  668. if ($this->_signs === null || $this->_signs[$id] === true) {
  669. $query .= '+';
  670. } else if ($this->_signs[$id] === false) {
  671. $query .= '-';
  672. }
  673. $query .= '(' . $subquery->__toString() . ')';
  674. }
  675. if ($this->getBoost() != 1) {
  676. $query = '(' . $query . ')^' . round($this->getBoost(), 4);
  677. }
  678. return $query;
  679. }
  680. }