Class: Ferret::Search::ConjunctionScorer

Inherits:
Scorer
  • Object
show all
Defined in:
lib/ferret/search/conjunction_scorer.rb

Overview

Scorer for conjunctions, sets of queries, all of which are required.

Constant Summary

Constants inherited from Scorer

Scorer::MAX_DOCS

Instance Attribute Summary

Attributes inherited from Scorer

#similarity

Instance Method Summary collapse

Methods inherited from Scorer

#each_hit, #each_hit_up_to

Constructor Details

#initialize(similarity) ⇒ ConjunctionScorer

Returns a new instance of ConjunctionScorer.



6
7
8
9
10
11
# File 'lib/ferret/search/conjunction_scorer.rb', line 6

def initialize(similarity) 
  super
  @scorers = []
  @first_time = true
  @more = true
end

Instance Method Details

#add(scorer) ⇒ Object Also known as: <<



13
14
15
# File 'lib/ferret/search/conjunction_scorer.rb', line 13

def add(scorer) 
  @scorers << scorer
end

#do_nextObject



39
40
41
42
43
44
45
# File 'lib/ferret/search/conjunction_scorer.rb', line 39

def do_next()
  while @more and first().doc < last().doc # find doc w/ all clauses
    @more = first().skip_to(last().doc)    # skip first upto last
    @scorers << @scorers.shift             # move first to last
  end
  return @more # found a doc with all clauses
end

#docObject



26
27
28
# File 'lib/ferret/search/conjunction_scorer.rb', line 26

def doc()
  return first().doc()
end

#explain(doc) ⇒ Object

Raises:

  • (NotImplementedError)


95
96
97
# File 'lib/ferret/search/conjunction_scorer.rb', line 95

def explain(doc) 
  raise NotImplementedError
end

#firstObject



18
19
20
# File 'lib/ferret/search/conjunction_scorer.rb', line 18

def first()
  return @scorers.first
end

#init(init_scorers) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ferret/search/conjunction_scorer.rb', line 72

def init(init_scorers)
  #  compute coord factor
  @coord = similarity().coord(@scorers.size(), @scorers.size())
 
  @more = @scorers.size() > 0

  if init_scorers
    # move each scorer to its first entry
    @scorers.each do |scorer|
      break if not @more
      @more = scorer.next?
    end
    sort_scorers() if @more
  end

  @first_time = false
end

#lastObject



22
23
24
# File 'lib/ferret/search/conjunction_scorer.rb', line 22

def last()
  return @scorers.last
end

#next?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/ferret/search/conjunction_scorer.rb', line 30

def next?()
  if (@first_time) 
    init(true)
  elsif (@more) 
    @more = last().next? # trigger further scanning
  end
  return do_next()
end

#scoreObject

Sums the scores of all of the scorers for the current document.



63
64
65
66
67
68
69
70
# File 'lib/ferret/search/conjunction_scorer.rb', line 63

def score()
  score = 0.0 # sum scores
  @scorers.each do |scorer|
    score += scorer.score
  end
  score *= @coord
  return score
end

#skip_to(target) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ferret/search/conjunction_scorer.rb', line 47

def skip_to(target)
  if(@first_time) 
    init(false)
  end
  
  @scorers.each do |scorer|
    break if not @more
    @more = scorer.skip_to(target)
  end
  
  sort_scorers() if @more # resort the scorers
  
  return do_next()
end

#sort_scorersObject



90
91
92
93
# File 'lib/ferret/search/conjunction_scorer.rb', line 90

def sort_scorers() 
  # move @scorers to an array
  @scorers.sort! {|a,b| a.doc <=> b.doc }
end