Class: Ferret::Search::ReqExclScorer

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

Overview

A Scorer for queries with a required subscorer and an excluding (prohibited) subscorer.

This Scorer implements Scorer#skip_to(int), and it uses the skip_to() on the given scorers.

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(req_scorer, excl_scorer) ⇒ ReqExclScorer

Construct a ReqExclScorer.

req_scorer

The scorer that must match, except where

excl_scorer

indicates exclusion.



11
12
13
14
15
16
17
# File 'lib/ferret/search/req_excl_scorer.rb', line 11

def initialize(req_scorer, excl_scorer) 
  super(nil) # No similarity used.
  @req_scorer = req_scorer
  @excl_scorer = excl_scorer

  @first_time = true
end

Instance Method Details

#docObject

only call when you know that a doc exists



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

def doc() 
  return @req_scorer.doc
end

#explain(doc) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/ferret/search/req_excl_scorer.rb', line 114

def explain(doc)
  e = Explanation.new()
  if @excl_scorer.skip_to(doc) and @excl_scorer.doc == doc
    e.description = "excluded"
  else 
    e.description = "not excluded"
    e.details << @req_scorer.explain(doc)
  end
  return e
end

#next?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ferret/search/req_excl_scorer.rb', line 20

def next?
  if @first_time
    if not @excl_scorer.next?
      @excl_scorer = nil # exhausted at start
    end
    @first_time = false
  end
  if @req_scorer == nil 
    return false
  end
  if not @req_scorer.next?
    @req_scorer = nil; # exhausted, nothing left
    return false
  end
  if @excl_scorer == nil
    return true # @req_scorer.next? already returned true
  end
  return to_non_excluded()
end

#scoreObject

Returns the score of the current document matching the query.

Initially invalid, until #next? is called the first time.

returns

The score of the required scorer.



83
84
85
# File 'lib/ferret/search/req_excl_scorer.rb', line 83

def score()
  return @req_scorer.score()
end

#skip_to(target) ⇒ Object

Skips to the first match beyond the current whose document number is greater than or equal to a given target.

When this method is used the #explain(int) method should not be used.

target

The target document number.

returns

true iff there is such a match.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ferret/search/req_excl_scorer.rb', line 94

def skip_to(target)
  if (@first_time) 
    @first_time = false
    if (! @excl_scorer.skip_to(target)) 
      @excl_scorer = nil; # exhausted
    end
  end
  if (@req_scorer == nil) 
    return false
  end
  if (@excl_scorer == nil) 
    return @req_scorer.skip_to(target)
  end
  if (! @req_scorer.skip_to(target)) 
    @req_scorer = nil
    return false
  end
  return to_non_excluded()
end

#to_non_excludedObject

Advance to non excluded doc. On entry:

  • @req_scorer != nil

  • @excl_scorer != nil

  • @req_scorer was advanced once via next? or skip_to() and @req_scorer.doc() may still be excluded.

Advances @req_scorer a non excluded required doc, if any.

returns

true iff there is a non excluded required doc.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ferret/search/req_excl_scorer.rb', line 51

def to_non_excluded()
  excl_doc = @excl_scorer.doc
  begin 
    req_doc = @req_scorer.doc # may be excluded
    if (req_doc < excl_doc) 
      return true # @req_scorer advanced to before @excl_scorer, ie. not excluded
    elsif (req_doc > excl_doc) 
      unless @excl_scorer.skip_to(req_doc) 
        @excl_scorer = nil # exhausted, no more exclusions
        return true
      end
      excl_doc = @excl_scorer.doc
      if excl_doc > req_doc 
        return true; # not excluded
      end
    end
  end while @req_scorer.next?
  @req_scorer = nil; # exhausted, nothing left
  return false
end