2013-02-01 120 views
3

如何將多個過濾器應用於facet(帶輪胎寶石)?我有一個代碼:Elasticsearch的多面過濾器

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 

但它似乎只適用於最後一個過濾器。但我需要他們兩個。

回答

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

不行的 - 「過濾畸形,必須start_object開始」,但我覺得我得到的總體方向。 – radixhound

+0

更新,只嘗試過:術語查詢,所以YMMV。讓我知道它是如何發展的 – concept47

+1

我在閱讀完這篇文章後不久就發現了正確的語法,事實上這很有效。謝謝。 – radixhound

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