| 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_Query */
|
|
|
23 |
require_once 'Zend/Search/Lucene/Search/Query.php';
|
|
|
24 |
|
|
|
25 |
/** Zend_Search_Lucene_Search_Weight_Term */
|
|
|
26 |
require_once 'Zend/Search/Lucene/Search/Weight/Term.php';
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* @package Zend_Search_Lucene
|
|
|
31 |
* @subpackage Search
|
|
|
32 |
* @copyright Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
|
|
|
33 |
* @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
|
|
|
34 |
*/
|
|
|
35 |
class Zend_Search_Lucene_Search_Query_Term extends Zend_Search_Lucene_Search_Query
|
|
|
36 |
{
|
|
|
37 |
/**
|
|
|
38 |
* Term to find.
|
|
|
39 |
*
|
|
|
40 |
* @var Zend_Search_Lucene_Index_Term
|
|
|
41 |
*/
|
|
|
42 |
private $_term;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Term sign.
|
|
|
46 |
* If true then term is required
|
|
|
47 |
* If false then term is prohibited.
|
|
|
48 |
*
|
|
|
49 |
* @var bool
|
|
|
50 |
*/
|
|
|
51 |
private $_sign;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Documents vector.
|
|
|
55 |
* Bitset or array of document IDs
|
|
|
56 |
* (depending from Bitset extension availability).
|
|
|
57 |
*
|
|
|
58 |
* @var mixed
|
|
|
59 |
*/
|
|
|
60 |
private $_docVector = null;
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Term positions vector.
|
|
|
64 |
* Array: docId => array( pos1, pos2, ... )
|
|
|
65 |
*
|
|
|
66 |
* @var array
|
|
|
67 |
*/
|
|
|
68 |
private $_termPositions;
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Zend_Search_Lucene_Search_Query_Term constructor
|
|
|
73 |
*
|
|
|
74 |
* @param Zend_Search_Lucene_Index_Term $term
|
|
|
75 |
* @param boolean $sign
|
|
|
76 |
*/
|
|
|
77 |
public function __construct( $term, $sign = true )
|
|
|
78 |
{
|
|
|
79 |
$this->_term = $term;
|
|
|
80 |
$this->_sign = $sign;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Constructs an appropriate Weight implementation for this query.
|
|
|
86 |
*
|
|
|
87 |
* @param Zend_Search_Lucene $reader
|
|
|
88 |
* @return Zend_Search_Lucene_Search_Weight
|
|
|
89 |
*/
|
|
|
90 |
protected function _createWeight($reader)
|
|
|
91 |
{
|
|
|
92 |
return new Zend_Search_Lucene_Search_Weight_Term($this->_term, $this, $reader);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Score specified document
|
|
|
97 |
*
|
|
|
98 |
* @param integer $docId
|
|
|
99 |
* @param Zend_Search_Lucene $reader
|
|
|
100 |
* @return float
|
|
|
101 |
*/
|
|
|
102 |
public function score( $docId, $reader )
|
|
|
103 |
{
|
|
|
104 |
if($this->_docVector===null) {
|
|
|
105 |
if (extension_loaded('bitset')) {
|
|
|
106 |
$this->_docVector = bitset_from_array( $reader->termDocs($this->_term) );
|
|
|
107 |
} else {
|
|
|
108 |
$this->_docVector = array_flip($reader->termDocs($this->_term));
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$this->_termPositions = $reader->termPositions($this->_term);
|
|
|
112 |
$this->_initWeight($reader);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$match = extension_loaded('bitset') ? bitset_in($this->_docVector, $docId) :
|
|
|
116 |
isset($this->_docVector[$docId]);
|
|
|
117 |
if ($this->_sign && $match) {
|
|
|
118 |
return $reader->getSimilarity()->tf(count($this->_termPositions[$docId]) ) *
|
|
|
119 |
$this->_weight->getValue() *
|
|
|
120 |
$reader->norm($docId, $this->_term->field);
|
|
|
121 |
} else {
|
|
|
122 |
return 0;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|