PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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