Class: Query::Parser

Inherits:
Hayfork::QueryParser show all
Defined in:
lib/generators/hayfork/templates/query/parser.rb

Instance Attribute Summary

Attributes inherited from Hayfork::QueryParser

#klass, #querystring

Instance Method Summary collapse

Methods inherited from Hayfork::QueryParser

#initialize, #parse!

Constructor Details

This class inherits a constructor from Hayfork::QueryParser

Instance Method Details

#parse_exact_phrase(querystring, phrases) ⇒ Object



12
13
14
# File 'lib/generators/hayfork/templates/query/parser.rb', line 12

def parse_exact_phrase(querystring, phrases)
  phrases << Query::ExactPhrase.new(tokenize_words(querystring))
end

#parse_phrase(querystring, phrases) ⇒ Object



6
7
8
9
10
# File 'lib/generators/hayfork/templates/query/parser.rb', line 6

def parse_phrase(querystring, phrases)
  tokenize_words(querystring).each do |word|
    phrases << Query::ExactPhrase.new([ word ])
  end
end

#tokenize_words(querystring) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/generators/hayfork/templates/query/parser.rb', line 16

def tokenize_words(querystring)
  # Postgres does not handle hyphens well.
  #
  # Notice how, in the following example, the way it breaks up
  # the hyphenated word throws off the index (Jesus is the fifth word
  # not the third or fourth). This prevents you from constructing
  # an exact-phrase query for a hyphenated word:
  #
  #   > select to_tsvector('hayfork', 'thou long-expected jesus');
  #   { 'expect':4 'jesus':5 'long':3 'long-expect':2 'thou':1 }
  #
  #
  # We'll coerce Postgres into treating hyphenated words as two words.
  querystring.to_s.scan(/\w+/)
end