【发布时间】:2013-06-12 20:02:10
【问题描述】:
我有这个搜索功能,它有一个文本/关键字框和一堆过滤器;我现在用以下结构实现了它:
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 更快。这是正确的吗?
现在,这些过滤器还会被缓存吗?另外,在这种情况下这是正确的方法吗?
谢谢!
编辑
根据我在 cmets 中发布的问题中所读到的内容,我认为我可以这样做:
filter :bool => [
{ :must => { :terms => { } } } unless ... ,
{ :must => { :terms => { } } } unless ... ,
{ :should => { :terms => { } } } unless ... ,
# and the other filters
]
是这样吗?
谢谢!
【问题讨论】:
-
我刚刚发现了以下问题github.com/karmi/tire/issues/2。似乎 karmi 并没有真正为
filter实现此功能? -
嘿!您能否发表您的评论作为答案并接受它?它可能会帮助其他有同样问题的人。
-
好吧,我还在等,希望也许 karmi 会来清理它:)
标签: ruby-on-rails ruby elasticsearch tire