选择多个方面或同时过滤数据

时间:2012-05-12 02:29:51

标签: ruby-on-rails filter sunspot acts-as-taggable-on faceted-search

更新时间:6/29/12

我设法建立了一个搜索和一个侧栏,用于使用Sunspot过滤搜索结果,并使用Rails进行行为标记。我正在关注本教程here 但我仍然无法立即选择多重过滤器。当我选择过滤器时,其他子类别名称仍然会消失。我错过了什么?

我的原始问题是:

  

我不太确定如何一次过滤选择多个方面   数据。所以我有多个子类别,即(徒步旅行,滑雪,攀爬,   我希望能够同时选择远足和攀爬   所以显示的数据只是远足和登山的对象。对   现在我选择一个(让我们说远足)和所有其他选项   消失。有人可以解释一下吗?

以下是我的代码:

齿轮控制器

  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 

** Gear Index View **

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

齿轮模型

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

1 个答案:

答案 0 :(得分:3)

您似乎需要利用:exclude选项进行分面搜索。

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

所以我上面所做的是创建一个exclusions数组来保存过滤器集合。 with()方法返回一个过滤器对象,因此我们捕获exclusions数组中的所有过滤器。我们执行compact!从数组中删除nil个对象,如果它是空的,我们将它设为nil(这是由于:exclude选项的限制,如果你传递它空数组,它将无法正常工作)。然后我们facet使用相同的:sub_category_name字词,不包括过滤条件,并为其命名。此facet将为您提供除所选项之外的所有方面类别。

然后在您的视图中,当您显示构面选项时,使用名为: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