remjih
remjih

Reputation: 313

Sunspot/Solr : Searching with multiple fields

What is the way to use Sunspot/Solr with mutiple fields ? It works fine with a simple form, as explained in this screencast : http://railscasts.com/episodes/278-search-with-sunspot

You can see my draft which doesn't work :

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

products/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 %>

The error :

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'

Thanks guys for your helping!

Upvotes: 3

Views: 1576

Answers (2)

thekingoftruth
thekingoftruth

Reputation: 1781

If you setup product.rb like so (specifying :model and :category as non-fulltext string fields that can be used for scoping), then your search code should work verbatim.

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

Upvotes: 1

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

You only want this (below)... You only want full text, which will search across all fields you define in searchable... I used "s" as the param name

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

Upvotes: 1

Related Questions