Thinking Sphinx Faceted Search Implementation Examples?
I am putting together a repository-type rails 3 site.
I have Thinking Sphinx installed and working on my site, insomuch as I can enter urls like localhost:3000/articles?search=test&page=2
and it will return the expected results.
I'm new to 开发者_JAVA技巧Rails (and web dev in general); I think I can manage the model and controller aspects of this, but the views so far have me stumped. I have a sidebar I would like to use as the search interface. The closest I have come is this (rendered as part of a sidebar partial):
<% form_tag @search, :method => :get do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search"%>
<% end %>
The search method is in my articles#index controller, and when I test it when the browser is pointed to that page (routed as /articles
), it works as expected, but with this odd url: localhost:3000/articles?utf8=✓&search=test&commit=Search
. When the browser is first pointed to the root path, nothing happens.
So, I think these are the main issues I need to address:
- EDIT - solved (see below)
- Should I move the search methods to their own controller, or should they be part of the articles controller? For now, Article will be the only model indexed.
- EDIT - solved (see below)
- Does anyone have any good example code of a faceted search view using Rails 3 and Thinking Sphinx? Like I said, I am something of a neophyte and am a little flustered by the documentation that skims by the view implementation. However, I am fairly adept at reading and interpreting code as long as it is reasonably complete.
Thanks in advance!
Solved:
How do I make the 'Search' button call the index method before trying to search? (I have solved this by replacing
@search
witharticles_path
).Solved using
will_paginate
, which I had trouble with before, but which seems to be working now.
Hey, This is an extract of how my site worked before i switched to solr
Product has many categories, we tell sphinx that we want to index them as facets
class Product < ActiveRecord::Base {
has_many :categorisations, :dependent => :destroy
has_many :categories, :through => :categorisations
define_index do
indexes product_name,
indexes description
indexes categories(:name), :as => :category,:facet => true
end
}
Results Controller
class ResultsController < ApplicationController
def index
@facets = Product.facets params[:qt], :conditions => {:category => params[:category}},:page => params[:page], :per_page => 20
@products = @facets.for
end
end
And then in the view you can do something like
<% @facets.each do |facet, facet_options| %>
<span><%= facet %></span>
<ul>
<% facet_options.each do |option, count| %>
<li><%= link_to "#{option} (#{count})",
:params => {facet => option, :page => 1} %></li>
<% end %>
</ul>
<% end %>
精彩评论