pg_search和Rails 3.2.3中Multisearch的基本设置

时间:2012-05-14 03:22:54

标签: ruby-on-rails-3 full-text-search pg-search

经过几个月的潜伏和吸收,这是我的第一个问题。所以我希望我能正确地做到这一点。

在了解了this Railscast中的pg_search_scope功能后,我一直在尝试使用我的Rails 3.2.3应用程序中的pg_search的多重功能。我相信pg_search文档假定读者比我更了解Rails。我只是无法从我发现的资源跳转到使用multisearch获得一个有效的应用程序。任何帮助将非常感激。这是我的设置:

配置/初始化/ pg_search.rb

PgSearch.multisearch_options = {
  :using => {
    :tsearch => {
      :dictionary => "english"
    },
    :trigram => {}
  },
  :ignoring => :accents
}

在视图中搜索表单

<%= form_tag articles_path, method: :get do %>
    <%= text_field_tag :query, params[:query], :class => "search-box" %>
    <%= submit_tag "Search This Site", name: nil, :class => "btn btn-search" %>
<% end %>

article.rb

include PgSearch
multisearchable :against => [:title, :content]

def self.search(query)
  if query.present?
    search(query)
  else
    scoped
  end
end

articles_controller.rb

def index
  @articles = PgSearch.multisearch(params[:query])

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @articles }
  end
end

搜索已知字词时,我没有搜索结果。我做错了什么?

1 个答案:

答案 0 :(得分:1)

看起来我的错误是在我的控制器中使用@articles变量而不是显式定义@pg_search_documents,这是我在我的视图中使用的(我完全忘记发布)。出于某种原因,我认为在我的控制器中使用@articles = PgSearch.multisearch(params[:query])会通过pg_search魔术酱将搜索结果附加到“@pg_search_documents”。