PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/Zend/Search/Lucene/Search/Weight/Boolean.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 137 lines | 44 code | 21 blank | 72 comment | 5 complexity | b494eff3c495ea3ab048d1b692d23599 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: Boolean.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /** Zend_Search_Lucene_Search_Weight */
  23. require_once 'Zend/Search/Lucene/Search/Weight.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_Weight_Boolean extends Zend_Search_Lucene_Search_Weight
  32. {
  33. /**
  34. * IndexReader.
  35. *
  36. * @var Zend_Search_Lucene_Interface
  37. */
  38. private $_reader;
  39. /**
  40. * The query that this concerns.
  41. *
  42. * @var Zend_Search_Lucene_Search_Query
  43. */
  44. private $_query;
  45. /**
  46. * Queries weights
  47. * Array of Zend_Search_Lucene_Search_Weight
  48. *
  49. * @var array
  50. */
  51. private $_weights;
  52. /**
  53. * Zend_Search_Lucene_Search_Weight_Boolean constructor
  54. * query - the query that this concerns.
  55. * reader - index reader
  56. *
  57. * @param Zend_Search_Lucene_Search_Query $query
  58. * @param Zend_Search_Lucene_Interface $reader
  59. */
  60. public function __construct(Zend_Search_Lucene_Search_Query $query,
  61. Zend_Search_Lucene_Interface $reader)
  62. {
  63. $this->_query = $query;
  64. $this->_reader = $reader;
  65. $this->_weights = array();
  66. $signs = $query->getSigns();
  67. foreach ($query->getSubqueries() as $num => $subquery) {
  68. if ($signs === null || $signs[$num] === null || $signs[$num]) {
  69. $this->_weights[$num] = $subquery->createWeight($reader);
  70. }
  71. }
  72. }
  73. /**
  74. * The weight for this query
  75. * Standard Weight::$_value is not used for boolean queries
  76. *
  77. * @return float
  78. */
  79. public function getValue()
  80. {
  81. return $this->_query->getBoost();
  82. }
  83. /**
  84. * The sum of squared weights of contained query clauses.
  85. *
  86. * @return float
  87. */
  88. public function sumOfSquaredWeights()
  89. {
  90. $sum = 0;
  91. foreach ($this->_weights as $weight) {
  92. // sum sub weights
  93. $sum += $weight->sumOfSquaredWeights();
  94. }
  95. // boost each sub-weight
  96. $sum *= $this->_query->getBoost() * $this->_query->getBoost();
  97. // check for empty query (like '-something -another')
  98. if ($sum == 0) {
  99. $sum = 1.0;
  100. }
  101. return $sum;
  102. }
  103. /**
  104. * Assigns the query normalization factor to this.
  105. *
  106. * @param float $queryNorm
  107. */
  108. public function normalize($queryNorm)
  109. {
  110. // incorporate boost
  111. $queryNorm *= $this->_query->getBoost();
  112. foreach ($this->_weights as $weight) {
  113. $weight->normalize($queryNorm);
  114. }
  115. }
  116. }