使用Elasticsearch的多个构面过滤器

时间:2013-02-01 11:25:17

标签: ruby-on-rails ruby ruby-on-rails-3 elasticsearch tire

如何将多个滤镜应用于刻面(带有轮胎宝石)?我有一个代码:

facet "packages" do
  terms :package
  facet_filter :terms, producer: [*params[:producer]] if params[:producer].present?
  facet_filter :terms, category_id: [*params[:category_id]] if params[:category_id].present?
end

但它似乎只适用于最后一个过滤器。但我需要他们两个。

2 个答案:

答案 0 :(得分:9)

试试这个

my_filter = []
my_filter << {:terms => {:producer => [*params[:producer]] }} if params[:producer].present?
my_filter << {:terms => { :producer => [*params[:category_id]] }} if params[:category_id].present?

facet "packages" do
  terms :package
  facet_filter :and, my_filter
end

答案 1 :(得分:1)

my_filter = []
my_filter << {:term => {:producer => params[:producer] }} if params[:producer].present?
my_filter << {:term => {:category_id => params[:category_id] }} if params[:category_id].present?

facet "packages" do
  terms :package
  facet_filter(:and, my_filter) if my_filter.present?
end