elasticsearch,tire,Ruby On Rails,Ruby,elasticsearch,Tire" /> elasticsearch,tire,Ruby On Rails,Ruby,elasticsearch,Tire" />

Ruby on rails 带布尔块的过滤器(轮胎/弹性搜索)

Ruby on rails 带布尔块的过滤器(轮胎/弹性搜索),ruby-on-rails,ruby,elasticsearch,tire,Ruby On Rails,Ruby,elasticsearch,Tire,我有这个搜索功能,它有一个文本/关键字框和一堆过滤器;我现在已经按照以下结构实施了它: query do boolean do must { string params[:text], default_operator: "AND" } unless params[:text].blank? must { } unless must { } unless should { } unless # some more must / shoul

我有这个搜索功能,它有一个文本/关键字框和一堆过滤器;我现在已经按照以下结构实施了它:

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
据我所知,使用轮胎在速度和缓存方面提供的实际过滤器来实现它是一种更好的方法

我想问一下,实施这一计划的正确方式是什么?我找不到将类似的布尔块传递给过滤器的方法。。。那太好了。也许我做错了

我正在考虑做类似的事情

# 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
这使我在过滤器块行出现以下错误: ArgumentError-1的参数0数量错误:

此外,我知道可以使用ElasticSearch JSON查询实现此行为,如下所示:。所以我假设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 :bool => [
  { :must => { :terms => { } } } unless ... ,
  { :must => { :terms => { } } } unless ... ,
  { :should => { :terms => { } } } unless ... ,
  # and the other filters
]

是这样吗


谢谢

我刚刚发现了以下问题。似乎karmi并没有真正为过滤器实现此功能

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

filter :bool => [
  { :must => { :terms => { } } } unless ... ,
  { :must => { :terms => { } } } unless ... ,
  { :should => { :terms => { } } } unless ... ,
  # and the other filters
]
资料来源如下:

此外,我还为我的特定案例编写了简单、单一的过滤器:

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过滤器

我认为这是一种比最初的方法更好的方法,即只使用查询,因为现在这些过滤器可以缓存到我读到的地方

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

此外,如果有人有更好的建议/解释/有用的提示,请随时提供


谢谢

我刚刚发现了以下问题。似乎karmi并没有真正为过滤器实现此功能

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

filter :bool => [
  { :must => { :terms => { } } } unless ... ,
  { :must => { :terms => { } } } unless ... ,
  { :should => { :terms => { } } } unless ... ,
  # and the other filters
]
资料来源如下:

此外,我还为我的特定案例编写了简单、单一的过滤器:

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过滤器

我认为这是一种比最初的方法更好的方法,即只使用查询,因为现在这些过滤器可以缓存到我读到的地方

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

此外,如果有人有更好的建议/解释/有用的提示,请随时提供


谢谢

我刚刚发现了以下问题。看起来karmi并没有真正为过滤器实现这个功能?嘿!你能把你的评论作为回答并接受吗?这可能会帮助其他有同样问题的人。好吧,我还在等待,希望卡米能来澄清一下:我刚刚发现了以下问题。看起来karmi并没有真正为过滤器实现这个功能?嘿!你能把你的评论作为回答并接受吗?这可能会帮助其他有同样问题的人。好吧,我还在等待,希望karmi可能会来解决这个问题: