Reputation: 143
I am very new to Sunspot/Solr and I am facing problems in getting a fulltext search to work on my website. Please find the code below:
/models/product.rb
attr_accessible :category_id, :title, :description
belongs_to :category
searchable do
text :title, :description
end
/models/category.rb
has_many :products
/controllers/categoriescontroller.rb
def show
@category = Category.find_by_slug!(params[:id])
@search = @category.products.solr_search do
fulltext params[:search]
end
@products = @search.results
end
/views/categories/show.html.erb
<%= form_tag @category, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<%end%>
I want to search the products that belong to a particular category. For example, if the category in the params is :fiction , I want to limit my search results to the products that are under that category. However , with the above code if I search, say 'lost' , I get all the products matching 'lost' under all categories. I am not able to understand what I am doing wrong here. Please help.
Upvotes: 0
Views: 480
Reputation: 143
I figured out how to do it ater reading the sunspot wiki. In categoriescontroller.rb
if(params[:search] != nil)
@search = Product.solr_search do
with :category_id, Category.find_by_slug!(params[:id]).id
fulltext params[:search]
end
@products = @search.results
else
@products = @category.products
end
Upvotes: 0