Class: Ferret::Search::PhraseQuery

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

Overview

A Query that matches documents containing a particular sequence of terms. A PhraseQuery is built by QueryParser for input like “new york”.

This query may be combined with other terms or queries with a BooleanQuery.

Defined Under Namespace

Classes: PhraseWeight

Instance Attribute Summary collapse

Attributes inherited from Query

#boost

Instance Method Summary collapse

Methods inherited from Query

#combine, #merge_boolean_queries, #rewrite, #similarity, #weight

Constructor Details

#initializePhraseQuery

Returns a new instance of PhraseQuery.



7
8
9
10
11
12
13
# File 'lib/ferret/search/phrase_query.rb', line 7

def initialize()
  super
  @slop = 0
  @terms = []
  @positions = []
  @field = nil
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



30
31
32
# File 'lib/ferret/search/phrase_query.rb', line 30

def field
  @field
end

#positionsObject (readonly)

Returns the value of attribute positions.



30
31
32
# File 'lib/ferret/search/phrase_query.rb', line 30

def positions
  @positions
end

#slopObject

Sets the number of other words permitted between words in query phrase. If zero, then this is an exact phrase search. For larger values this works like a WITHIN or NEAR operator.

The slop is in fact an edit-distance, where the units correspond to moves of terms in the query phrase out of position. For example, to switch the order of two words requires two moves (the first move places the words atop one another), so to permit re-orderings of phrases, the slop must be at least two.

More exact matches are scored higher than sloppier matches, thus search results are sorted by exactness.

The slop is zero by default, requiring exact matches.



29
30
31
# File 'lib/ferret/search/phrase_query.rb', line 29

def slop
  @slop
end

#termsObject (readonly)

Returns the value of attribute terms.



30
31
32
# File 'lib/ferret/search/phrase_query.rb', line 30

def terms
  @terms
end

Instance Method Details

#<<(term) ⇒ Object



57
58
59
60
# File 'lib/ferret/search/phrase_query.rb', line 57

def <<(term)
  add(term)
  return self
end

#add(term, position = nil, pos_inc = 1) ⇒ Object

Adds a term to the end of the query phrase.

The relative position of the term is the one immediately after the last term added, unless explicitly specified. By specifying explicitly, you can have phrases with more than one term at the same position or phrases with gaps (e.g. in connection with stopwords).

term

the term to search for

position

the relative position of the term to the rest of the terms

int the query.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ferret/search/phrase_query.rb', line 42

def add(term, position = nil, pos_inc = 1) 
  if position.nil?
    position = (@positions.size > 0) ? (@positions[-1] + pos_inc) : 0
  end
  
  if @terms.size == 0
    @field = term.field
  elsif (term.field != @field)
    raise ArgumentError, "All phrase terms must be in the same field: #{term}"
  end
  
  @terms << term
  @positions << position
end

#create_weight(searcher) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/ferret/search/phrase_query.rb', line 167

def create_weight(searcher)
  if @terms.size == 1 # optimize one-term case

    term = @terms[0]
    tq = TermQuery.new(term)
    tq.boost = boost()
    return tq.create_weight(searcher)
  end
  return PhraseWeight.new(self, searcher)
end

#eql?(o) ⇒ Boolean Also known as: ==

Returns true iff o is equal to this.

Returns:

  • (Boolean)


203
204
205
206
207
208
209
# File 'lib/ferret/search/phrase_query.rb', line 203

def eql?(o) 
  if not o.instance_of? PhraseQuery
    return false
  end
  return (boost() == o.boost() and @slop == o.slop and
    @terms == o.terms and @positions == o.positions)
end

#extract_terms(query_terms) ⇒ Object

See Query#extract_terms()



178
179
180
# File 'lib/ferret/search/phrase_query.rb', line 178

def extract_terms(query_terms) 
  query_terms.add_all(@terms)
end

#hashObject

Returns a hash code value for this object.



213
214
215
# File 'lib/ferret/search/phrase_query.rb', line 213

def hash() 
  return boost().hash ^ slop.hash ^ @terms.hash ^ @positions.hash
end

#to_s(f = nil) ⇒ Object

Prints a user-readable version of this query.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ferret/search/phrase_query.rb', line 183

def to_s(f=nil) 
  buffer = ""
  buffer << "#{@field}:" if @field != f
  buffer << '"'
  last_pos = -1
  @terms.each_index do |i|
    term = @terms[i]
    pos = @positions[i]
    last_pos.upto(pos-2) {buffer << "<> "}
    last_pos = pos
    buffer << "#{term.text} "
  end
  buffer.rstrip!
  buffer << '"'
  buffer << "~#{slop}" if (slop != 0) 
  buffer << "^#{boost()}" if boost() != 1.0
  return buffer
end