import
[web.mtrack] / inc / lib / Zend / Search / Lucene / Search / Weight / Boolean.php
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
23
24 /** Zend_Search_Lucene_Search_Weight */
25 require_once 'Zend/Search/Lucene/Search/Weight.php';
26
27
28 /**
29  * @category   Zend
30  * @package    Zend_Search_Lucene
31  * @subpackage Search
32  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
34  */
35 class Zend_Search_Lucene_Search_Weight_Boolean extends Zend_Search_Lucene_Search_Weight
36 {
37     /**
38      * IndexReader.
39      *
40      * @var Zend_Search_Lucene_Interface
41      */
42     private $_reader;
43
44     /**
45      * The query that this concerns.
46      *
47      * @var Zend_Search_Lucene_Search_Query
48      */
49     private $_query;
50
51     /**
52      * Queries weights
53      * Array of Zend_Search_Lucene_Search_Weight
54      *
55      * @var array
56      */
57     private $_weights;
58
59
60     /**
61      * Zend_Search_Lucene_Search_Weight_Boolean constructor
62      * query - the query that this concerns.
63      * reader - index reader
64      *
65      * @param Zend_Search_Lucene_Search_Query $query
66      * @param Zend_Search_Lucene_Interface    $reader
67      */
68     public function __construct(Zend_Search_Lucene_Search_Query $query,
69                                 Zend_Search_Lucene_Interface    $reader)
70     {
71         $this->_query   = $query;
72         $this->_reader  = $reader;
73         $this->_weights = array();
74
75         $signs = $query->getSigns();
76
77         foreach ($query->getSubqueries() as $num => $subquery) {
78             if ($signs === null || $signs[$num] === null || $signs[$num]) {
79                 $this->_weights[$num] = $subquery->createWeight($reader);
80             }
81         }
82     }
83
84
85     /**
86      * The weight for this query
87      * Standard Weight::$_value is not used for boolean queries
88      *
89      * @return float
90      */
91     public function getValue()
92     {
93         return $this->_query->getBoost();
94     }
95
96
97     /**
98      * The sum of squared weights of contained query clauses.
99      *
100      * @return float
101      */
102     public function sumOfSquaredWeights()
103     {
104         $sum = 0;
105         foreach ($this->_weights as $weight) {
106             // sum sub weights
107             $sum += $weight->sumOfSquaredWeights();
108         }
109
110         // boost each sub-weight
111         $sum *= $this->_query->getBoost() * $this->_query->getBoost();
112
113         // check for empty query (like '-something -another')
114         if ($sum == 0) {
115             $sum = 1.0;
116         }
117         return $sum;
118     }
119
120
121     /**
122      * Assigns the query normalization factor to this.
123      *
124      * @param float $queryNorm
125      */
126     public function normalize($queryNorm)
127     {
128         // incorporate boost
129         $queryNorm *= $this->_query->getBoost();
130
131         foreach ($this->_weights as $weight) {
132             $weight->normalize($queryNorm);
133         }
134     }
135 }
136
137