Tire 允许您使用旨在镜像 JSON api 的 DSL 构建弹性搜索查询,例如:
search = Tire::Search::Search.new
search.query do
boolean do
should { match :title, "Red" }
should { match :synopsis, "Red" }
should { match :brand_title, "Red" }
end
boolean do
must { term :excluded, false }
end
end
我想把它分成我自己的 DSL 来定义可以构建的查询组,有点像 Rails 范围:
class Proxy
def initialize
@queries = []
end
def results
search = Tire::Search::Search.new
queries = @queries
search.query do
# …? What should go here to search the combined set of conditions?
end
end
def term t
query do
boolean do
should { match :title, t }
should { match :synopsis, t }
should { match :brand_title, t }
end
end
end
def included bool
query do
boolean do
must { term :excluded, !bool }
end
end
end
private
def query &block
@queries << block
end
end
p = Proxy.new
p.term "Red"
p.included true
p.results
问题是,轮胎不允许超过一个search.query
块 - 后续query
s 替换前一个。我可以使用 instance_eval 之类的东西在查询块的正确上下文中运行多个块吗?