Class: Ferret::Search::WildcardTermEnum
- Inherits:
-
FilteredTermEnum
- Object
- Index::TermEnum
- FilteredTermEnum
- Ferret::Search::WildcardTermEnum
- 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
-
#end_enum ⇒ Object
readonly
Returns the value of attribute end_enum.
Attributes inherited from FilteredTermEnum
Instance Method Summary collapse
- #close ⇒ Object
- #difference ⇒ Object
-
#initialize(reader, term) ⇒ WildcardTermEnum
constructor
Creates a new
WildcardTermEnum. - #term_compare(term) ⇒ Object
Methods inherited from FilteredTermEnum
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_enum ⇒ Object (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
#close ⇒ Object
55 56 57 58 59 |
# File 'lib/ferret/search/wildcard_term_enum.rb', line 55 def close() super() @pattern = nil @field = nil end |
#difference ⇒ Object
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 |