Select multiple faces or filter data at the same time

UPDATED 6/29/12

I was able to configure the search and sidebar to filter my search results using Sunspot and act-as-taggable with Rails. I read this tutorial here but I still cannot select the mutliple filter right away. When I select a filter, the names of other subcategories still disappear. What am I missing?

My original question is:

I'm not quite sure how to select multiple faces at once, filtering the data. Thus, I have several subcategories, i.e. (Hiking, skiing, etc.) I want to be able to choose hiking and climbing at the same time, therefore the data given are only those objects of hiking and climbing. Right now I choose one (say, a trip) and all other options disappear. Can someone explain?

Below is my code:

Gear controller

def index @search = Gear.solr_search do exclusions = [] fulltext params[:search] exclusions << with(:sub_category_name, params[:name]) if params[:name].present? exclusions.compact! exclusions = nil if exclusions.empty? facet :sub_category_name facet :sub_category_name, :exclude => exclusions, :name => :all_categories paginate(page: params[:page], :per_page => 15) end @gears = @search.results end 

** View Turn Signal **

 <div class="gears_container"> <div class="side_bar_search"> <%= form_tag gears_path, :method => :get do %> <p> <%= text_field_tag :keywords, params[:keywords] , class: 'gearsearchbar' %> <%= submit_tag "Search", :name => nil, class: 'btn btn-inverse gearsearchbutton' %> </p> <% end %> <div class="sidebar_section">Sub Category</div> <ul> <% for row in @search.facet(:all_categories).rows %> <li class="sidebar_options"> <% if params[:name].present? %> <strong><%= row.value %></strong>(<%= link_to "remove", :name => nil %>) <% else %> <%= link_to row.value, :name => row.value %> (<%= row.count %>) <% end %> </li> <% end %> </ul> </div> <div c <div class="search_results_gear"> <% @hits.each do |gear| %> <%= render partial: 'gear', locals: {gear: gear} %> <% end %> <div style="clear:both"></div> <%= will_paginate @hits, class: 'flickr_pagination' %> </br> </div> </div> 

Gear model

 class Gear < ActiveRecord::Base attr_accessible :title, :size, :price, :sub_category_id, :user_id, :image, :image_a, :remote_image_url, :color, :year, :latefee, :cancellation, :minrental, :policy, :about, :address, :city, :state, :zip, :sub_category_name belongs_to :user belongs_to :sub_category has_one :category, :through => :sub_category has_many :comments, :dependent => :destroy has_many :line_items require 'carrierwave/orm/activerecord' mount_uploader :image, GearpicUploader mount_uploader :image_a, GearpicUploader before_destroy :ensure_not_referenced_by_any_line_item acts_as_taggable_on :tags validates :title, presence: true validates :size, presence: true validates :price, presence: true validates :sub_category_id, presence: true validates :user_id, presence: true searchable do text :title, :size, :price, :year, :zip, :state, :city, :minrental, :about, :latefee, :color text :user_firstname do user.firstname end text :user_lastname do user.lastname end # **Facet Section** string :size, :price, :year, :zip, :state, :city, :minrental, :latefee, :color string :sub_category_name , :multiple => true, :stored => true do sub_category.name end string :category_name do category.name end end private def ensure_not_referenced_by_any_line_item if line_items.empty? return true else errors.add(:base, 'Line Items present') return false end end end 
+4
source share
1 answer

It looks like you need to use the :exclude option for your grant search.

 def index @search = Gear.search do exclusions = [] # tags, AND'd if params[:tag].present? all_of do params[:tag].each do |tag| exclusions << with(:sub_category_name, tag) end end end exclusions.compact! exclusions = nil if exclusions.empty? facet :sub_category_name facet :sub_category_name, :exclude => exclusions, :name => :all_categories paginate(page: params[:page]) end @hits = @search.results end 

So, what I did above, create an exclusions array to store a collection of filters. The with() method returns a filter object, so we commit all the filters in the exclusions array. We make compact! to remove nil objects from the array, and if it is empty, we make it nil (this is due to the limitations of the parameter :exclude , if you pass it an empty array, it will not work correctly). We then facet on the same :sub_category_name member, excluding filters, and also give it a name. This facet will give you all facet categories, excluding selected ones.

Then, in your opinion, when displaying facet parameters, you use a facet with the name :all_categories .

 @search.facet(:all_categories).rows.each_with_index do |facet, index| # This just outputs all the facets, you can add the logic in. <li><%= facet.value %> (<%= facet.count %>)</li> end 
+3
source

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


All Articles