使用 Websolr 和 Heroku 进行全文搜索

发布于 2024-10-15 17:23:29 字数 290 浏览 2 评论 0原文

您好,我在尝试弄清楚如何在我的应用程序中全局实现搜索表单时遇到困难。我有一系列帖子需要由登录和未登录的用户搜索。我已在我的帖子模型中添加了以下代码:

searchable do 
text :content, :default_boost => 2 
text :body,    :default_boost => 1.5 
end    

但是,我不知道从哪里开始创建跨所有帖子的搜索字段页面并使其显示我需要的结果。我是 Rails 新手,如果有人愿意帮助我,我很乐意发布更多信息。

Hi I am having trouble trying to figure out how to implement a search form globally across my application. I have a series of posts that need to be searchable by users that are signed in and not signed in. I have added this code in my post model:

searchable do 
text :content, :default_boost => 2 
text :body,    :default_boost => 1.5 
end    

However, I do not know where to go from there to create a search field across all pages and make it show the results I need. I am new to rails and would be happy to post more information if someone is willing to help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2024-10-22 17:23:29

首先,您应该按照以下railscast中的说明添加搜索字段: http://railscasts.com /episodes/37-simple-search-form

由于您的搜索并不特定于特定模型,因此请使用通用控制器名称而不是 ProjectsController。

然后,您应该使用 Sunspot DSL 来替换 ActiveRecord finder。

以下是帮助您入门的示例代码:

page = @page = params[:page] && params[:page].to_i || 1
@search = Sunspot.search(Realty) do # search_ids
  per_page = params[:per_page] && params[:per_page].to_i || 10

  # not adapted to your case
  with(:equipments).all_of params['equip'].split(' ') if params['equip']
  case params[:sort]
  when "average_rating"
    order_by :average_rating, :desc
  when "type"
    order_by :type, :asc
  end

  paginate :page => page, :per_page => per_page

  # other criteria...
end

在您看来,您可以遍历 @search.results

<%= will_paginate @search.results %>

<% @search.results.each do |hit| %>
  <%# 'path' contains the stored polymorphic_path of each model object #%>
  <% link_to hit.stored('path') do %>
    <p><%= hit.stored('content') %></p>
  <% end %>
<% end %>

最后,使用 WebSolR 而不是标准 SolR 服务器非常简单,您可以按照 https://github.com/onemorecloud/websolr-rails

编辑:
正如 Nick 评论的那样,您完全应该访问 http://docs.heroku.com/websolr
谢谢尼克!

First, you should add your search field like explained in this railscast: http://railscasts.com/episodes/37-simple-search-form

Since your search isn't specific to a particular model, use a generic controller name instead of ProjectsController though.

Then, you should replace the ActiveRecord finder by the use of the Sunspot DSL.

Here is an sample code to help get you started:

page = @page = params[:page] && params[:page].to_i || 1
@search = Sunspot.search(Realty) do # search_ids
  per_page = params[:per_page] && params[:per_page].to_i || 10

  # not adapted to your case
  with(:equipments).all_of params['equip'].split(' ') if params['equip']
  case params[:sort]
  when "average_rating"
    order_by :average_rating, :desc
  when "type"
    order_by :type, :asc
  end

  paginate :page => page, :per_page => per_page

  # other criteria...
end

In your view, you can then iterate through @search.results

<%= will_paginate @search.results %>

<% @search.results.each do |hit| %>
  <%# 'path' contains the stored polymorphic_path of each model object #%>
  <% link_to hit.stored('path') do %>
    <p><%= hit.stored('content') %></p>
  <% end %>
<% end %>

Last, using WebSolR instead of a standard SolR server is quite simple, you can follow the setup instructions at https://github.com/onemorecloud/websolr-rails.

Edit:
As Nick commented, you should totally go to http://docs.heroku.com/websolr.
Thanks Nick !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文