繁体   English   中英

PostgreSQL全文搜索:我如何确定范围?

[英]PostgreSQL full-text search: how do i scope it?

通常,这是基于RailsCasts屏幕录像#341。

我正在尝试使用PostgreSQL全文搜索,它可以工作,但是我需要确定范围。 搜索应通过object.state=='saved'对象进行。

在截屏视频中,搜索方法的定义类似于类方法,因此我不能使用“ where”,因为“ where”返回Array,而搜索方法是class方法,因此这里出现错误。 我尝试通过以下方式进行更改:

def test_search(query)
 if query.present?
  self.where("title @@ :q or text_for_test @@ :q", q:query)
 else
  scoped
 end
 end

至:

@tests=Test.where(:state=>'saved')
@tests.test_search(params[:query])

但是比Rails抛出未定义的方法错误。

我还能做什么来限制搜索范围? 或如何解决此尝试的错误?

使用textacular gem,它可以全文搜索postgres。

您可以使用textacular给出的“ basic_search”功能,类似于使用命名作用域的方式。

Test.basic_search('Sonic').where(:status => 'saved')

Test.where(:status => 'saved').basic_search('Sonic')
where("title @@ :q or text_for_test @@ :q and state @@ :state", q:query, state:'saved')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM