Class: Ferret::Search::BooleanQuery::BooleanWeight
- Defined in:
- lib/ferret/search/boolean_query.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#similarity ⇒ Object
Returns the value of attribute similarity.
-
#weights ⇒ Object
Returns the value of attribute weights.
Instance Method Summary collapse
- #explain(reader, doc) ⇒ Object
-
#initialize(query, searcher) ⇒ BooleanWeight
constructor
A new instance of BooleanWeight.
- #normalize(norm) ⇒ Object
-
#scorer(reader) ⇒ Object
- returns
-
An alternative Scorer that uses and provides skip_to(), and scores documents in document number order.
- #sum_of_squared_weights ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(query, searcher) ⇒ BooleanWeight
Returns a new instance of BooleanWeight.
106 107 108 109 110 111 112 113 114 |
# File 'lib/ferret/search/boolean_query.rb', line 106 def initialize(query, searcher) @query = query @weights = [] @similarity = query.similarity(searcher) query.clauses.each do |clause| @weights << clause.query.create_weight(searcher) end end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
104 105 106 |
# File 'lib/ferret/search/boolean_query.rb', line 104 def query @query end |
#similarity ⇒ Object
Returns the value of attribute similarity.
102 103 104 |
# File 'lib/ferret/search/boolean_query.rb', line 102 def similarity @similarity end |
#weights ⇒ Object
Returns the value of attribute weights.
103 104 105 |
# File 'lib/ferret/search/boolean_query.rb', line 103 def weights @weights end |
Instance Method Details
#explain(reader, doc) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ferret/search/boolean_query.rb', line 163 def explain(reader, doc) sum_expl = Explanation.new() sum_expl.description = "sum of:" coord = 0 max_coord = 0 sum = 0.0 @weights.each_with_index do |weight, i| clause = @query.clauses[i] explanation = weight.explain(reader, doc) max_coord += 1 if not clause.prohibited? if explanation.value > 0 if not clause.prohibited? sum_expl << explanation sum += explanation.value coord += 1 else return Explanation.new(0.0, "match prohibited") end elsif clause.required? return Explanation.new(0.0, "match required") end end sum_expl.value = sum if (coord == 1) # only one clause matched sum_expl = sum_expl.details[0] # eliminate wrapper end coord_factor = @similarity.coord(coord, max_coord) if (coord_factor == 1.0) # coord is no-op return sum_expl # eliminate wrapper else result = Explanation.new() result.description = "product of:" result << sum_expl result << Explanation.new(coord_factor, "coord(#{coord}/#{max_coord})") result.value = sum * coord_factor return result end end |
#normalize(norm) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/ferret/search/boolean_query.rb', line 135 def normalize(norm) norm *= @query.boost() @weights.each_with_index do |weight, i| clause = @query.clauses[i] if not clause.prohibited? weight.normalize(norm) end end end |
#scorer(reader) ⇒ Object
- returns
-
An alternative Scorer that uses and provides skip_to(),
and scores documents in document number order.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ferret/search/boolean_query.rb', line 147 def scorer(reader) result = BooleanScorer.new(@similarity) @weights.each_with_index do |weight, i| clause = @query.clauses[i] sub_scorer = weight.scorer(reader) if (sub_scorer != nil) result.add_scorer(sub_scorer, clause.occur) elsif (clause.required?()) return nil end end return result end |
#sum_of_squared_weights ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ferret/search/boolean_query.rb', line 120 def sum_of_squared_weights() sum = 0 @weights.each_with_index do |weight, i| clause = @query.clauses[i] if not clause.prohibited? sum += weight.sum_of_squared_weights() # sum sub weights end end sum *= @query.boost() * @query.boost() # boost each sub-weight return sum end |
#value ⇒ Object
116 117 118 |
# File 'lib/ferret/search/boolean_query.rb', line 116 def value() return @query.boost() end |