PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/lib/Zend/Search/Lucene/Search/Weight/MultiTerm.php

https://bitbucket.org/yoander/mtrack
PHP | 139 lines | 46 code | 21 blank | 72 comment | 5 complexity | 1da2eb6b0cdf48469e29ca89508b2ed0 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: MultiTerm.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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_MultiTerm 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. * Query terms weights
  47. * Array of Zend_Search_Lucene_Search_Weight_Term
  48. *
  49. * @var array
  50. */
  51. private $_weights;
  52. /**
  53. * Zend_Search_Lucene_Search_Weight_MultiTerm 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->getTerms() as $id => $term) {
  68. if ($signs === null || $signs[$id] === null || $signs[$id]) {
  69. require_once 'Zend/Search/Lucene/Search/Weight/Term.php';
  70. $this->_weights[$id] = new Zend_Search_Lucene_Search_Weight_Term($term, $query, $reader);
  71. $query->setWeight($id, $this->_weights[$id]);
  72. }
  73. }
  74. }
  75. /**
  76. * The weight for this query
  77. * Standard Weight::$_value is not used for boolean queries
  78. *
  79. * @return float
  80. */
  81. public function getValue()
  82. {
  83. return $this->_query->getBoost();
  84. }
  85. /**
  86. * The sum of squared weights of contained query clauses.
  87. *
  88. * @return float
  89. */
  90. public function sumOfSquaredWeights()
  91. {
  92. $sum = 0;
  93. foreach ($this->_weights as $weight) {
  94. // sum sub weights
  95. $sum += $weight->sumOfSquaredWeights();
  96. }
  97. // boost each sub-weight
  98. $sum *= $this->_query->getBoost() * $this->_query->getBoost();
  99. // check for empty query (like '-something -another')
  100. if ($sum == 0) {
  101. $sum = 1.0;
  102. }
  103. return $sum;
  104. }
  105. /**
  106. * Assigns the query normalization factor to this.
  107. *
  108. * @param float $queryNorm
  109. */
  110. public function normalize($queryNorm)
  111. {
  112. // incorporate boost
  113. $queryNorm *= $this->_query->getBoost();
  114. foreach ($this->_weights as $weight) {
  115. $weight->normalize($queryNorm);
  116. }
  117. }
  118. }