Bra*_*cil 7 postgresql ruby-on-rails
我跟随Ryan Bates的Railscast跟Postgres进行全文搜索,然而,他正在使用postgres 9.1而我正在使用9.2.他构建以下查询以执行搜索.如果我的查询是单个单词,例如"超人",但如果它是两个单词,例如dc comics
,或者super man
,我收到此错误,这对于postgres来说是新手,我无法弄清楚如何修复它对我有用.你能帮忙吗?
PG::Error: ERROR: syntax error in tsquery: "super man"
LINE 1: ...articles" WHERE (to_tsvector('english', name) @@ 'super man...
^
: SELECT "articles".* FROM "articles" WHERE (to_tsvector('english', name) @@ 'super man' or to_tsvector('english', content) @@ 'super man') ORDER BY ts_rank(to_tsvector(name), plainto_tsquery('super man')) +
ts_rank(to_tsvector(content), plainto_tsquery('super man'))
desc LIMIT 3 OFFSET 0
Run Code Online (Sandbox Code Playgroud)
来自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
Run Code Online (Sandbox Code Playgroud)
Chr*_*loe 18
@@
用来比较a tsvector
和a tsquery
.您试图将a tsvector
与无效的东西进行比较tsquery
.
'superman'
是类型text
,应该真的包装在一个调用to_tsquery()
.然而,看起来postgres试图帮助你并将其强制为tsquery,而to_tsquery('superman')是一个有效的查询.
'super man'
是类型text
,应该真的包装在一个调用to_tsquery()
.由于to_tsquery('super man')不是一个有效的查询,Postgres 未能将它强制转换为tsquery .有效的tsquery必须有类似的布尔运算符或告诉查询如何处理单词.'super&man'可能会奏效.&
|
为了节省您必须为AND样式查询的简单情况编写查询,plainto_tsquery
使这更容易一些.在你的情况下,将你的:q
参数包装在一个电话中plainto_tsquery
plainto_tsquery(:q)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3622 次 |
最近记录: |