Class: Ferret::Search::PrefixQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/ferret/search/prefix_query.rb

Overview

A Query that matches documents containing terms with a specified prefix. A PrefixQuery is built by QueryParser for input like app*.

Instance Attribute Summary collapse

Attributes inherited from Query

#boost

Instance Method Summary collapse

Methods inherited from Query

#combine, #create_weight, #extract_terms, #merge_boolean_queries, #similarity, #weight

Constructor Details

#initialize(prefix) ⇒ PrefixQuery

Constructs a query for terms starting with prefix.



7
8
9
10
# File 'lib/ferret/search/prefix_query.rb', line 7

def initialize(prefix) 
  super()
  @prefix = prefix
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'lib/ferret/search/prefix_query.rb', line 5

def prefix
  @prefix
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ferret/search/prefix_query.rb', line 46

def eql?(o)
  (@prefix == o.prefix and boost() == o.boost)
end

#hashObject



50
51
52
# File 'lib/ferret/search/prefix_query.rb', line 50

def hash()
  boost().hash ^ @prefix.hash
end

#rewrite(reader) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ferret/search/prefix_query.rb', line 12

def rewrite(reader)
  bq = BooleanQuery.new(true)
  enumerator = reader.terms_from(@prefix)
  begin 
    prefix_text = @prefix.text
    prefix_length = prefix_text.length
    prefix_field = @prefix.field
    begin 
      term = enumerator.term
      if (term.nil? or
        term.field != prefix_field or
        term.text[0,prefix_length] != prefix_text)
        break
      end
        tq = TermQuery.new(term)                        # found a match
        tq.boost = boost()                              # set the boost
        bq.add_query(tq, BooleanClause::Occur::SHOULD)  # add to query
        #puts("added " + term)
    end while (enumerator.next?)
  ensure 
    enumerator.close()
  end
  return bq
end

#to_s(f) ⇒ Object

Prints a user-readable version of this query.



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

def to_s(f) 
  buffer = ""
  buffer << "#{@prefix.field}:" if @prefix.field != f
  buffer << "#{@prefix.text}*"
  buffer << "^#{boost()}" if boost() != 1.0 
  return buffer
end