Borders and Rails & Sunspot Filtering

Quite a lot of noobie here, so I appreciate any help anyone can give.

I am trying to add a cut to the search on my site through Sunspot. Ryan just released a great Railscast that got me started: http://railscasts.com/episodes/278-search-with-sunspot . I got this job and was able to add extra facets. My problem is that the faces are not dependent on each other. If I have 3 faces on 3 different attributes, when I select a face after I have already selected, I would like to display only the results that fall into both of these objects. At the moment, he simply switches from one face to another. I feel that it should not be so difficult, but I cannot figure out how to do it.

I found this tutorial: http://blog.upubly.com/2011/01/06/using-sunspot-in-your-views/ , which I think does what I want. I tried to get this to work, but even when I try to get it to work with only one aspect, I don't see any results. Just the name of the face, and then nothing more.

Thoughts?

Thanks!

UPDATE

Here are some sample code I'm trying to do:

By adjusting the Railscasts code, I got the following:

In my StylesController:

def index @search = Style.search do fulltext params[:search] facet :departmental, :seasonal, :classifier with(:departmental, params[:department]) if params[:department].present? with(:classifier, params[:classification]) if params[:classification].present? with(:seasonal, params[:season]) if params[:season].present? end 

In my style index view (I know I need to condense this)

  = form_tag styles_path, :method => :get do %p = text_field_tag :search, params[:search] = submit_tag "Search", :name => nil #facets %h4 Departments %ul - for row in @search.facet(:departmental).rows %li - if params[:department].blank? = link_to row.value, :department => row.value (#{row.count}) - else %strong= row.value (#{link_to "remove", :department => nil}) %h4 Classifications %ul - for row in @search.facet(:classifier).rows %li - if params[:classification].blank? = link_to row.value, :classification => row.value (#{row.count}) - else %strong= row.value (#{link_to "remove", :classification => nil}) %h4 Seasons %ul - for row in @search.facet(:seasonal).rows %li - if params[:season].blank? = link_to row.value, :season => row.value (#{row.count}) - else %strong= row.value (#{link_to "remove", :season => nil}) 

In my style model:

  searchable do text :number, :description, :department, :classification, :season string :departmental string :classifier string :seasonal end def departmental self.department end def classifier self.classification end def seasonal self.season end 

And my version of inline code is paired up to just try and get the "seasonal" facet to work:

I left the Search Search Parts, Search Model, and SearchHelper in the same way as in the example. I tried to mess with the Helper, as my Facets will pull text values, not only identifiers of other models, but to no avail. I do not have different attributes configured as separate Models, because I did not think that I needed this functionality, but I start to think differently.

StylesController:

  def index @title = "All styles" @search = search = Search.new(params[:search]) # need to set local variable to pass into search method @search.url = styles_path @search.facets = [:seasonal] @solr_search = Style.search do keywords search.query with(:seasonal, true) search.facets.each do |item| facet(item) with(:seasonal, params[:season]) if params[:season].present? end any_of do # filter by facets search.facets.each do |item| with(item).all_of( params[item].try(:split, "-") ) if params[item].present? end end paginate(:page => params[:page], :per_page => 10) end 

Again, I appreciate the help. Definitely a noob, but really enjoying the process of creating this site. Stackoverflow was a HUGE help for me, so I owe it to everyone who posts answers here, thank you very much.

+6
source share
1 answer

I needed an answer to this myself, and, seeing that there was nothing else on the Internet on the Internet, I decided that I would try to understand it myself.

At first I came to the conclusion that the controller can process several faces and there are no reasons that it cannot, I remembered that the best part about ruby ​​is that it is the most human-readable code, try reading your first controller and you will see which makes sense that it works. I checked this by manually entering a query string in the url that returned the expected results. Therefore, as soon as I realized this, I knew that the problem remained in my view (which made me facepalm, because it is pretty obvious now)

Your example is much more complex than mine, and my answer may not meet 100% requirements, but I'm sure it is close. Also, your code in your model is relatively "departmental", etc. A little boring in my opinion.

controller

 def index @search = Style.search do fulltext params[:search] facet :departmental, :seasonal, :classifier with(:departmental, params[:department]) if params[:department].present? with(:classifier, params[:classification]) if params[:classification].present? with(:seasonal, params[:season]) if params[:season].present? end 

View

 %h4 Departments %ul - for row in @search.facet(:departmental).rows %li - if params[:department].blank? = link_to row.value, styles_path( :department => row.value, :classification => (params[:classification] unless params[:season].blank?), :season => (params[:season] unless params[:season].blank?)) (#{row.count}) - else %strong= row.value = link_to "remove", styles_path( :department => nil, :classification => (params[:classification] unless params[:season].blank?), :season => (params[:season] unless params[:season].blank?)) 
+3
source

Source: https://habr.com/ru/post/894875/


All Articles