问题
I have a rails application where I am searching for repair shops. The search class method looks like this:
def self.search(params)
if params
repairshop = Repairshop.where(:approved => true)
if params[:radius].present?
repairshop = repairshop.near(params[:location], params[:radius]) if params[:location].present?
else
repairshop = repairshop.near(params[:location], 200) if params[:location].present?
end
if params[:keywords].present?
repairshop = repairshop.joins(:specializations).joins(:brands_we_services).where("LOWER(specializations.title) LIKE ? OR LOWER(brands_we_services.title) LIKE ? OR LOWER(repairshop.title) LIKE ?", "%#{params[:keywords].downcase}%", "%#{params[:keywords].downcase}%", "%#{params[:keywords].downcase}%")
end
repairshop.uniq
else
all
end
end
A repair shop can have many specializations and brands_we_services. So, searching through all of the names apart form other params is important.
The search form looks like this:
Everything works fine. And the search controller takes me to the search page. The controller action is as follows:
def search
@repairshops = Repairshop.search(params)
end
The search result looks like this:
My question is:
On the left side of the search page, below the search box, I want to provide facets for filtering the results. I want to provide facets which would be dynamic. Like depending on Search results, it provides facets for cities (number of repair shops) etc.
I don't want to use elastic search or solr. I want to understand how I build dynamic faceted search from scratch, the basics of building facets from your search results.
Note: I looked into Forty facets but couldn't understand how can I use my complex class method search which is in Repairshop model and integrate it into the class MovieSearch < FortyFacets::FacetSearch
provided by the controller as per the docs.
Will really appreciate your help. Thanks!
来源:https://stackoverflow.com/questions/48350867/rails-how-to-use-facets-with-search-results