Skip to content

Instantly share code, notes, and snippets.

@bogdan
Created September 15, 2010 11:12
Show Gist options
  • Save bogdan/580583 to your computer and use it in GitHub Desktop.
Save bogdan/580583 to your computer and use it in GitHub Desktop.
class SearchController < ApplicationController
AUTHORS_TYPE = "AUTHORS_TYPE"
BOOKS_TYPE = "BOOKS_TYPE"
RECOMMENDATIONS_TYPE = "RECOMMENDATIONS_TYPE"
ALL_TYPES = [ AUTHORS_TYPE, BOOKS_TYPE, RECOMMENDATIONS_TYPE]
def index
@keywords = params[:keywords]
@category_ids = params[:category_ids] ? params[:category_ids].map(&:to_i) : []
@all_categories = Category.root_categories.all(:include => {:children => :children})
@types = params[:types] || ALL_TYPES
books_page = params[:books_page]
@types = [ BOOKS_TYPE ] if books_page
authors_page = params[:authors_page]
@types = [AUTHORS_TYPE] if authors_page
if @types.include?(BOOKS_TYPE)
@search = Group.search do |s|
s.keywords preprocess_keywords(@keywords), :minimum_match => 1 do
highlight :title, :formatted_summary, :author_names
end
unless @category_ids.blank?
s.with(:category_ids).all_of(@category_ids)
end
#unless @genre_id.blank?
#s.with(:genre_ids).all_of([@genre_id])
#end
s.facet :category_ids
s.paginate :page => params[:books_page], :per_page => 10
s.data_accessor_for(Group).include = {:representative => :authors}
end
end
if @types.include?(AUTHORS_TYPE)
@authors_search = Contributor.search do |s|
s.keywords preprocess_keywords(@keywords), :minimum_match => 1 do
highlight :display_name
end
unless @category_ids.blank?
s.with(:authored_category_ids).all_of(@category_ids)
end
#unless @genre_id.blank?
#s.with(:genre_ids).all_of([@genre_id])
#end
s.facet :authored_category_ids
s.with(:authored_products_count).greater_than(1)
s.data_accessor_for(Contributor).include = {:authored_products => {:group => :representative}}
s.paginate :page => params[:authors_page], :per_page => 10
end
end
end
def preprocess_keywords(keywords)
return keywords if keywords.blank?
keywords.gsub('.', ' ')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment