2013-06-12 164 views
1

我有了一個文本/關鍵字框和一堆濾鏡此搜索功能;我把它實現,現在結構如下:過濾器與布爾塊(輪胎/ ElasticSearch)

query do 
    boolean do 
    must { string params[:text], default_operator: "AND" } unless params[:text].blank? 
    must { } unless 
    must { } unless 
    should { } unless 
    # some more must/should/must_not queries 
    end 
end 

# Some sorting 

據我已閱讀,這將是利用輪胎提供的實際filter(在速度和cacheing方面)來實現它一個更好的辦法。

我要問什麼是實現這個的正確方法嗎?我找不到一種將類似boolean塊傳遞給filter的方法...那很好。也許我做錯了?

我想到做這樣的事情

# This would be the main query, by text/keyword 
query { string params[:text], default_operator: "AND" } unless params[:text].blank? 

filter do 
    boolean do 
    # My regular filters that I implemented with queries 
    must { } unless 
    must { } unless 
    should { } unless 
    end 
end 

# Some sorting 

這給了我下面的錯誤(在filter塊線): ArgumentError - wrong number of arguments (0 for 1):

而且,我知道,這個行爲可以使用ElasticSearch實現JSON查詢,如下所示:http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter/。所以我認爲輪胎在這個功能上提供了類似的功能。

謝謝你的幫助。

編輯
什麼我想現在要做的是使用過濾的搜索,以下列方式:

query do 
    filtered do 
    query { string params[:text], default_operator: "AND" } unless params[:text].blank? 

    # And here I would like to add the filter block... but for now I am using the filter :and 
    filter :and, { :terms => { } } unless params[:something1].blank?, 
       { :terms => { } } unless params[:something2].blank?, 
       { :range => { } } unless params[:something3].blank?, 
       # more filters that need the AND behavior 
    filter :or, { :terms => { } } unless params[:something4].blank? 
       # more filters that need the OR behavior 
    end 
end 

我想這種做法,因爲我還讀了一個filter :and更快比一個boolean之一。它是否正確?

現在,將這些過濾器仍然可以緩存?另外,在這種情況下這是否正確?

謝謝!

編輯

從我在張貼在評論的問題讀過,我想我可以做這樣的事情:

filter :bool => [ 
    { :must => { :terms => { } } } unless ... , 
    { :must => { :terms => { } } } unless ... , 
    { :should => { :terms => { } } } unless ... , 
    # and the other filters 

]

這是正確的?

謝謝!

+0

我剛剛發現以下問題https://github.com/karmi/tire/issues/2。看起來像karmi沒有真正實現'filter'的這個特性? – vburca

+0

嘿!你可以發表你的評論作爲答案並接受它嗎?它可以幫助其他有相同問題的人。 – ramseykhalaf

+0

好吧,我還在等,希望卡爾米可能會來清除它:) – vburca

回答

4

我剛剛發現了以下問題https://github.com/karmi/tire/issues/2。看起來像karmi沒有真正實現這個功能的過濾器?

同時,正如我在一些集成測試中所看到的,我想我可以做這樣的事情:

filter :bool => [ 
    { :must => { :terms => { } } } unless ... , 
    { :must => { :terms => { } } } unless ... , 
    { :should => { :terms => { } } } unless ... , 
    # and the other filters 
] 

這裏是源:https://github.com/karmi/tire/commit/01ba026

而且,我結束了編寫簡單,針對我的具體情況單過濾器:

query { all } unless !params[:keyword].blank? 
query { string params[:keyword], default_operator: "AND" } unless params[:keyword].blank? 

filter :term, :a => params[:a] unless params[:a].blank? 
filter :term, :b => params[:b] unless params[:b].blank? 

filter :range, :c => { gte: params[:c_min] } unless params[:c_min].blank? 
filter :range, :c => { lte: params[:c_max] } unless params[:c_max].blank? 

filter :terms, :d => params[:d] unless params[:d].blank? 

# and more such filters, just wanted to give more examples since 
# the documentation is so sparse over the internet and I know what 
# it takes to find everything in one place :) 

我仍然認爲這是不是最方便的方法,但它的工作在我的情況(一個布爾塊將b我很高興有過濾器,但據我所知,一個:and過濾器比一個:bool更快 - 並列出了一堆過濾器,就像我上面所做的那樣,據我瞭解,迄今爲止,創建了這樣一個過濾器:and過濾器)。

我認爲這是一個比只有查詢更好的方法,因爲現在可以緩存這些過濾器(據我所知,再次)。

希望這將幫助別人的未來:)

此外,如果任何人有一個更好的建議/解釋/有用的提示,請隨時做出貢獻!

謝謝!