Rails & Sunspot facets and filtering

前端 未结 1 1351
名媛妹妹
名媛妹妹 2021-02-09 21:31

Pretty much a noobie here, so I appreciate any help someone can give.

I\'m trying to add faceting to the search on my site through Sunspot. Ryan just released a great

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-09 21:43

    I needed the answer to this myself, and seeing as there seems to be nothing else on the web about it, I decided I'd try to figure it out myself.

    First I came to the conclusion through logic, that the controller can handle multiple facets and there's no reasons it cannot, I remembered that the best part about ruby is that it is the most human readable code, try to read your first controller and you'll see that it makes sense that it works. I tested this by manually entering in a query string in url, which returned expected results. Therefore, once I figured that out, I knew the issue resided in my view (which made me facepalm because it's fairly obvious now)

    Your example is significantly more complex than mine, and my answer might not 100% meet every requirement but I'm pretty sure it's close. Also your code in your model regarding "departmental" etc is a little redundant in my view

    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?))
    

    0 讨论(0)
提交回复
热议问题