[英]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.