ruby on rails - postgres search query error if space used -
i'm following along ryan bates' railscast on full text search postgres, however, he's using postgres 9.1 , i'm using 9.2. builds following query execute search. works me if query single word, such "superman" if it's 2 words, such dc comics
, or super man
, i'm getting error, being new postgres can't figure out how fix. can assist?
pg::error: error: syntax error in tsquery: "super man" line 1: ...articles" (to_tsvector('english', name) @@ 'super man... ^ : select "articles".* "articles" (to_tsvector('english', name) @@ 'super man' or to_tsvector('english', content) @@ 'super man') order ts_rank(to_tsvector(name), plainto_tsquery('super man')) + ts_rank(to_tsvector(content), plainto_tsquery('super man')) desc limit 3 offset 0
query article.rb
def self.text_search(query) if query.present? rank = <<-rank ts_rank(to_tsvector(name), plainto_tsquery(#{sanitize(query)})) + ts_rank(to_tsvector(content), plainto_tsquery(#{sanitize(query)})) rank where("to_tsvector('english', name) @@ :q or to_tsvector('english', content) @@ :q", q: query).order("#{rank} desc") else scoped end end
@@
used compare tsvector
tsquery
. trying compare tsvector
not valid tsquery
.
'superman'
of type text
, should wrapped in call to_tsquery()
. looks postgres has tried out , coerced tsquery you, , to_tsquery('superman') valid query.
'super man'
of type text
, should wrapped in call to_tsquery()
. postgres has failed coerce tsquery you, since to_tsquery('super man') not valid query. valid tsquery must have boolean operators &
or |
tell query how treat words. 'super & man' work.
to save having write queries simple cases of and-style queries, plainto_tsquery
makes little easier. in case wrap :q
param in call plainto_tsquery
plainto_tsquery(:q)
Comments
Post a Comment