Class: Ferret::Search::WildcardTermEnum

Inherits:
FilteredTermEnum show all
Includes:
Index
Defined in:
lib/ferret/search/wildcard_term_enum.rb

Overview

Subclass of FilteredTermEnum for enumerating all terms that match the specified wildcard filter term.

Term enumerations are always ordered by Term.compareTo(). Each term in the enumeration is greater than all that precede it.

Constant Summary collapse

WILDCARD_STRING =
'*'
WILDCARD_CHAR =
'?'

Instance Attribute Summary collapse

Attributes inherited from FilteredTermEnum

#term

Instance Method Summary collapse

Methods inherited from FilteredTermEnum

#doc_freq, #enum=, #next?

Methods inherited from Index::TermEnum

#doc_freq, #next?, #skip_to, #term

Constructor Details

#initialize(reader, term) ⇒ WildcardTermEnum

Creates a new WildcardTermEnum. Passing in a org.apache.lucene.index.Term Term that does not contain a WILDCARD_CHAR will cause an exception to be raisen.

After calling the constructor the enumeration is already pointing to the first valid term if such a term exists.



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

def initialize(reader, term)
  super()
  @end_enum = false
  @search_term = term
  @field = @search_term.field
  text = @search_term.text
  len = text.length

  sidx = text.index(WILDCARD_STRING)||len
  cidx = text.index(WILDCARD_CHAR)||len
  idx = [sidx, cidx].min

  @pre = @search_term.text[0,idx]
  @pre_len = idx
  @pattern = /^#{Regexp.escape(text[idx..-1]).gsub(/\\([?*])/){".#{$1}"}}$/
  self.enum = reader.terms_from(Term.new(@search_term.field, @pre))
end

Instance Attribute Details

#end_enumObject (readonly)

Returns the value of attribute end_enum.



11
12
13
# File 'lib/ferret/search/wildcard_term_enum.rb', line 11

def end_enum
  @end_enum
end

Instance Method Details

#closeObject



55
56
57
58
59
# File 'lib/ferret/search/wildcard_term_enum.rb', line 55

def close()
  super()
  @pattern = nil
  @field = nil
end

#differenceObject



51
52
53
# File 'lib/ferret/search/wildcard_term_enum.rb', line 51

def difference() 
  return 1.0
end

#term_compare(term) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/ferret/search/wildcard_term_enum.rb', line 40

def term_compare(term) 
  if (@field == term.field) 
    search_text = term.text
    if (search_text[0, @pre_len] == @pre) 
      return (search_text[@pre_len..-1] =~ @pattern)
    end
  end
  @end_enum = true
  return false
end