【问题标题】:Filter with boolean block (Tire / ElasticSearch)使用布尔块过滤(轮胎/弹性搜索)
【发布时间】: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 :andboolean 更快。这是正确的吗?

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

谢谢!

编辑

根据我在 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


【解决方案1】:

我刚刚发现了以下问题 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 :)

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

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

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

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

谢谢!

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多