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

据我所知,使用tire提供的实际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/。所以我假设tire在这个特性的基础上提供了一个类似的功能。

谢谢你的帮助。

编辑
我现在要做的是使用过滤搜索,如下所示:

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 :andboolean快。这是正确的吗?

现在,这些过滤器还会被缓存吗?此外,在这种情况下,这是正确的方法吗?

谢谢!

编辑

根据我在评论中看到的问题,我想我可以这样做:

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

)

这样对吗?

谢谢!

我刚刚发现以下问题https://github.com/karmi/tire/issues/2。看来karma并没有真正实现过滤器的这个功能?

同时,正如我在一些集成测试中看到的,我认为我可以这样做:

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 :)

我仍然认为这不是最方便的方法,但它在我的情况下完成了工作(一个布尔块会很好,对于过滤器,但据我所知,:and过滤器比:bool过滤器更快-并且列出了一堆过滤器,正如我上面所做的,据我所知,到目前为止,创建了这样一个:and过滤器)。

我认为这是一个更好的方法,比最初的一个,只有查询,因为现在这些过滤器可以缓存(只要我读,再一次)。

希望这对将来的人有帮助:)

另外,如果有人有更好的建议/解释/有用的提示,请随时贡献!

谢谢!

最新更新