Module: PostgresFulltextSearchHelper

Extended by:
PostgresFulltextSearchHelper
Included in:
PostgresFulltextSearchHelper
Defined in:
lib/postgres-fulltext-search-helper.rb

Constant Summary collapse

POSTGRES_TSQUERY_SENSITIVE_CHARACTERS =
/[|&!():\* ]/

Instance Method Summary collapse

Instance Method Details

#format_query_for_fulltext(query) ⇒ Object



8
9
10
11
# File 'lib/postgres-fulltext-search-helper.rb', line 8

def format_query_for_fulltext(query)
  parts = query.split(POSTGRES_TSQUERY_SENSITIVE_CHARACTERS).delete_if{|x| x.strip.empty? }
  return parts.map{|token| "(#{token}:* | -#{token}:*)" }.join(' & ')
end

#search(scope, field_name, query) ⇒ Object



13
14
15
# File 'lib/postgres-fulltext-search-helper.rb', line 13

def search(scope, field_name, query)
  return scope.where("to_tsvector('simple', #{field_name}::text) @@ to_tsquery('simple', ?)", [format_query_for_fulltext(query)])
end