[英]Filter with boolean block (Tire / 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
(在速度和緩存方面)實現它將是更好的方法。
我想問一下實現這個的正確方法是什么? 我找不到將類似的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
]
這是正確的嗎?
謝謝!
我剛剛發現以下問題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 :)
我仍然認為這不是最方便的方法,但它在我的情況下完成了工作(對於過濾器來說,布爾塊很好,但據我所知,a :and
filter比a :bool
更快)一個 - 並列出了一堆過濾器,就像我上面所做的那樣,據我所知,到目前為止,創建了這樣一個:and
過濾器)。
我認為這是一個比初始方法更好的方法,只有查詢,因為現在這些過濾器可以被緩存(據我所讀,再次)。
希望這將有助於未來的人:)
此外,如果有人有更好的建議/解釋/有用的提示,請隨時貢獻!
謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.