final move of files
[web.mtrack] / Zend / Search / Lucene / Search / Weight / MultiTerm.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: MultiTerm.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_MultiTerm 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      * Query terms weights
53      * Array of Zend_Search_Lucene_Search_Weight_Term
54      *
55      * @var array
56      */
57     private $_weights;
58
59
60     /**
61      * Zend_Search_Lucene_Search_Weight_MultiTerm 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->getTerms() as $id => $term) {
78             if ($signs === null || $signs[$id] === null || $signs[$id]) {
79                 $this->_weights[$id] = new Zend_Search_Lucene_Search_Weight_Term($term, $query, $reader);
80                 $query->setWeight($id, $this->_weights[$id]);
81             }
82         }
83     }
84
85
86     /**
87      * The weight for this query
88      * Standard Weight::$_value is not used for boolean queries
89      *
90      * @return float
91      */
92     public function getValue()
93     {
94         return $this->_query->getBoost();
95     }
96
97
98     /**
99      * The sum of squared weights of contained query clauses.
100      *
101      * @return float
102      */
103     public function sumOfSquaredWeights()
104     {
105         $sum = 0;
106         foreach ($this->_weights as $weight) {
107             // sum sub weights
108             $sum += $weight->sumOfSquaredWeights();
109         }
110
111         // boost each sub-weight
112         $sum *= $this->_query->getBoost() * $this->_query->getBoost();
113
114         // check for empty query (like '-something -another')
115         if ($sum == 0) {
116             $sum = 1.0;
117         }
118         return $sum;
119     }
120
121
122     /**
123      * Assigns the query normalization factor to this.
124      *
125      * @param float $queryNorm
126      */
127     public function normalize($queryNorm)
128     {
129         // incorporate boost
130         $queryNorm *= $this->_query->getBoost();
131
132         foreach ($this->_weights as $weight) {
133             $weight->normalize($queryNorm);
134         }
135     }
136 }
137
138