简体   繁体   English

结合使用Websolr和Heroku进行全文搜索

[英]Using Websolr with Heroku to do full text search

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. 我是Rails的新手,如果有人愿意帮助我,我们将乐意发布更多信息。

First, you should add your search field like explained in this railscast: http://railscasts.com/episodes/37-simple-search-form 首先,您应该像在以下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. 由于搜索不是特定于特定模型的,因此请使用通用控制器名称而不是ProjectsController。

Then, you should replace the ActiveRecord finder by the use of the Sunspot DSL. 然后,您应该使用Sunspot DSL替换ActiveRecord查找程序。

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 在您看来,您可以通过@ 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 . 最后,使用WebSolR代替标准SolR服务器非常简单, 您可以按照 https://github.com/onemorecloud/websolr-rails上的设置说明进行操作

Edit: As Nick commented, you should totally go to http://docs.heroku.com/websolr . 编辑:正如Nick所说,您应该完全转到http://docs.heroku.com/websolr Thanks Nick ! 谢谢尼克!

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

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