太阳黑子/ Solr:搜索多个字段

时间:2011-10-30 20:35:37

标签: ruby-on-rails ruby solr sunspot

使用Sunspot / Solr与多个字段的方法是什么? 它可以通过简单的形式工作,如此截屏视频中所述:http://railscasts.com/episodes/278-search-with-sunspot

您可以看到我的草稿不起作用:

product.rb

searchable do
  text :address
  text :model
  text :category
end

* products_controller.rb *

  def search
    @search = Sunspot.search(Product) do
      fulltext params[:address]
        with(:model, params[:model]) if params[:model].present?
        with(:category, params[:category]) if params[:category].present?
    end
    @products = @search.results
  end

产品/ search.html.erb

  <%= form_tag products_search_path, :method => :get do %>
    <div class="field">
        <%= label_tag "Address ?" %>
        <%= select_tag :address, "<option>he</option><option>ho</option>".html_safe %>
    </div>
    <div class="field">
        <%= label_tag "Model ?" %>
        <%= text_field_tag :model, params[:model] %>
        <%#= select_tag :model, "<option>hi</option><option>ha</option>".html_safe %>
    </div>
    <div class="field">
        <%= label_tag "Category ?" %>
        <%= text_field_tag :category, params[:category] %>
    </div>
    <div id="buttonSearch"><%= submit_tag "Search" %></div>
  <% end %>

错误:

Sunspot::UnrecognizedFieldError (No field configured for Product with name 'model'):
  app/controllers/products_controller.rb:99:in `block in search'
  app/controllers/products_controller.rb:97:in `search'

谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

你只需要这个(下面)...你只需要全文,它将搜索你在可搜索中定义的所有字段...我用“s”作为参数名称

@search = Sunspot.search(Product) do
  fulltext(params[:s])
end

答案 1 :(得分:1)

如果您这样设置product.rb(将:model:category指定为可用于作用域的非全文字符串字段),那么您的搜索代码应该逐字逐句。

# product.rb
searchable do
  text :address
  string :model
  string :category
end