Class: Ferret::Search::PhraseQuery::PhraseWeight

Inherits:
Weight
  • Object
show all
Defined in:
lib/ferret/search/phrase_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, searcher) ⇒ PhraseWeight

Returns a new instance of PhraseWeight.



65
66
67
68
69
# File 'lib/ferret/search/phrase_query.rb', line 65

def initialize(query, searcher)
  @query = query
  @similarity = query.similarity(searcher)
  @idf = @similarity.idf_phrase(@query.terms, searcher)
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



63
64
65
# File 'lib/ferret/search/phrase_query.rb', line 63

def query
  @query
end

#valueObject (readonly)

Returns the value of attribute value.



63
64
65
# File 'lib/ferret/search/phrase_query.rb', line 63

def value
  @value
end

Instance Method Details

#explain(reader, doc) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ferret/search/phrase_query.rb', line 106

def explain(reader, doc)
  result = Explanation.new()
  result.description = "weight(#{@query} in #{doc}), product of:"

  doc_freqs = @query.terms.map do |term|
    "#{term.text}=#{reader.doc_freq(term)}"
  end.join(", ")

  idf_expl = Explanation.new(@idf, "idf(#{@query.field}:<#{doc_freqs}>)")
  
  # explain query weight

  query_expl = Explanation.new()
  query_expl.description = "query_weight(#{@query}), product of:"

  boost = @query.boost()
  if boost != 1.0
    boost_expl = Explanation.new(boost, "boost")
    query_expl << boost_expl
  end
  query_expl << idf_expl
  
  query_norm_expl = Explanation.new(@query_norm, "query_norm")
  query_expl << query_norm_expl
  
  query_expl.value = boost * @idf * query_norm_expl.value

  result << query_expl
 
  # explain field weight

  field_expl = Explanation.new()
  field_expl.description =
    "field_weight(#{query} in #{doc}), product of:"

  tf_expl = scorer(reader).explain(doc)
  field_expl << tf_expl
  field_expl << idf_expl

  field_norm_expl = Explanation.new()
  field_norms = reader.get_norms(@query.field)
  field_norm =
    field_norms ? Similarity.decode_norm(field_norms[doc]) : 0.0
  field_norm_expl.value = field_norm
  field_norm_expl.description =
    "field_norm(field=#{@query.field}, doc=#{doc})"
  field_expl << field_norm_expl

  field_expl.value = tf_expl.value * @idf * field_norm
  
  result << field_expl

  # combine them

  result.value = query_expl.value * field_expl.value

  if query_expl.value == 1.0
    return field_expl
  else
    return result
  end
end

#normalize(query_norm) ⇒ Object



78
79
80
81
82
# File 'lib/ferret/search/phrase_query.rb', line 78

def normalize(query_norm) 
  @query_norm = query_norm
  @query_weight *= query_norm            # normalize query weight

  @value = @query_weight * @idf          # idf for document 

end

#scorer(reader) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ferret/search/phrase_query.rb', line 84

def scorer(reader)
  return nil if @query.terms.size == 0   # optimize zero-term case


  tps = []
  @query.terms.each do |term|
    tp = reader.term_positions_for(term)
    return nil if tp.nil?
    tps << tp
  end

  if (@query.slop == 0)         # optimize exact case

    return ExactPhraseScorer.new(self, tps, @query.positions,
                                 @similarity,
                                 reader.get_norms(@query.field))
  else
    return SloppyPhraseScorer.new(self, tps, @query.positions,
                             @similarity,
                             @query.slop,
                             reader.get_norms(@query.field))
  end
end

#sum_of_squared_weightsObject



73
74
75
76
# File 'lib/ferret/search/phrase_query.rb', line 73

def sum_of_squared_weights() 
  @query_weight = @idf * @query.boost()  # compute query weight

  return @query_weight * @query_weight   # square it

end

#to_sObject



71
# File 'lib/ferret/search/phrase_query.rb', line 71

def to_s() return "phrase_weight(#{@value})" end