| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Zend Framework
|
|
|
4 |
*
|
|
|
5 |
* LICENSE
|
|
|
6 |
*
|
|
|
7 |
* This source file is subject to version 1.0 of the Zend Framework
|
|
|
8 |
* license, that is bundled with this package in the file LICENSE, and
|
|
|
9 |
* is available through the world-wide-web at the following URL:
|
|
|
10 |
* http://www.zend.com/license/framework/1_0.txt. If you did not receive
|
|
|
11 |
* a copy of the Zend Framework license and are unable to obtain it
|
|
|
12 |
* through the world-wide-web, please send a note to license@zend.com
|
|
|
13 |
* so we can mail you a copy immediately.
|
|
|
14 |
*
|
|
|
15 |
* @package Zend_Search_Lucene
|
|
|
16 |
* @subpackage Search
|
|
|
17 |
* @copyright Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
|
|
|
18 |
* @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
/** Zend_Search_Lucene_Search_Weight */
|
|
|
23 |
require_once 'Zend/Search/Lucene/Search/Weight.php';
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* @package Zend_Search_Lucene
|
|
|
28 |
* @subpackage Search
|
|
|
29 |
* @copyright Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
|
|
|
30 |
* @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
|
|
|
31 |
*/
|
|
|
32 |
class Zend_Search_Lucene_Search_Weight_MultiTerm extends Zend_Search_Lucene_Search_Weight
|
|
|
33 |
{
|
|
|
34 |
/**
|
|
|
35 |
* IndexReader.
|
|
|
36 |
*
|
|
|
37 |
* @var Zend_Search_Lucene
|
|
|
38 |
*/
|
|
|
39 |
private $_reader;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* The query that this concerns.
|
|
|
43 |
*
|
|
|
44 |
* @var Zend_Search_Lucene_Search_Query_MultiTerm
|
|
|
45 |
*/
|
|
|
46 |
private $_query;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Query terms weights
|
|
|
50 |
* Array of Zend_Search_Lucene_Search_Weight_Term
|
|
|
51 |
*
|
|
|
52 |
* @var array
|
|
|
53 |
*/
|
|
|
54 |
private $_weights;
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Zend_Search_Lucene_Search_Weight_MultiTerm constructor
|
|
|
59 |
* query - the query that this concerns.
|
|
|
60 |
* reader - index reader
|
|
|
61 |
*
|
|
|
62 |
* @param Zend_Search_Lucene_Search_Query_MultiTerm $query
|
|
|
63 |
* @param Zend_Search_Lucene $reader
|
|
|
64 |
*/
|
|
|
65 |
public function __construct($query, $reader)
|
|
|
66 |
{
|
|
|
67 |
$this->_query = $query;
|
|
|
68 |
$this->_reader = $reader;
|
|
|
69 |
$this->_weights = array();
|
|
|
70 |
|
|
|
71 |
$signs = $query->getSigns();
|
|
|
72 |
|
|
|
73 |
foreach ($query->getTerms() as $num => $term) {
|
|
|
74 |
if ($signs === null || $signs[$num] === null || $signs[$num]) {
|
|
|
75 |
$this->_weights[$num] = new Zend_Search_Lucene_Search_Weight_Term($term, $query, $reader);
|
|
|
76 |
$query->setWeight($num, $this->_weights[$num]);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* The weight for this query
|
|
|
84 |
*
|
|
|
85 |
* @return float
|
|
|
86 |
*/
|
|
|
87 |
public function getValue()
|
|
|
88 |
{
|
|
|
89 |
return $this->_query->getBoost();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* The sum of squared weights of contained query clauses.
|
|
|
95 |
*
|
|
|
96 |
* @return float
|
|
|
97 |
*/
|
|
|
98 |
public function sumOfSquaredWeights()
|
|
|
99 |
{
|
|
|
100 |
$sum = 0;
|
|
|
101 |
foreach ($this->_weights as $weight) {
|
|
|
102 |
// sum sub weights
|
|
|
103 |
$sum += $weight->sumOfSquaredWeights();
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// boost each sub-weight
|
|
|
107 |
$sum *= $this->_query->getBoost() * $this->_query->getBoost();
|
|
|
108 |
|
|
|
109 |
// check for empty query (like '-something -another')
|
|
|
110 |
if ($sum == 0) {
|
|
|
111 |
$sum = 1.0;
|
|
|
112 |
}
|
|
|
113 |
return $sum;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Assigns the query normalization factor to this.
|
|
|
119 |
*
|
|
|
120 |
* @param float $queryNorm
|
|
|
121 |
*/
|
|
|
122 |
public function normalize($queryNorm)
|
|
|
123 |
{
|
|
|
124 |
// incorporate boost
|
|
|
125 |
$queryNorm *= $this->_query->getBoost();
|
|
|
126 |
|
|
|
127 |
foreach ($this->_weights as $weight) {
|
|
|
128 |
$weight->normalize($queryNorm);
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
|