将 Tire 与 Mongoid 结合使用时,我无法弄清楚如何构建查询以使用 ElasticSearch 查找事件。特别是,除了用户关注的表演者的事件之外,我还试图找到用户正在观看的事件:
# app/models/event.rb
class Event
include Mongoid::Document
include Tire::Model::Search
include Tire::Model::Callbacks
field :name, type: String
has_and_belongs_to_many :performers
has_many :watchers, class_name: 'User'
mapping do
indexes :name
indexes :watcher_ids, type: 'string', index: :not_analyzed
indexes :performer_ids, type: 'string', index: :not_analyzed
end
end
以下查询仅适用于观察者或表演者。
Tire.search 'events' do
query do
string params[:query]
# Only one of these will work at a time:
terms :performer_ids, current_user.followed_performers.collect(&:id).map(&:to_s)
terms :watcher_ids, [current_user.id.to_s]
end
end
- 稍作修改,因为我输入的示例有误。
这是一个似乎“有效”的解决方案……但感觉不对
Tire.search('events') do
query do
boolean do
should { string params[:query] }
should { string "performer_ids:#{current_user.followed_performers.collect(&:id).map(&:to_s).join(',')}" }
should { string "watcher_ids:#{current_user.id.to_s}" }
end
end
end
最佳答案
您走在正确的道路上,但正如 Russ Smith 所建议的那样,您需要使用 filter
DSL。
现在,如果您只是重复调用 filter
,您将执行联合:AND
。如果您想返回用户正在观看的事件或用户关注的表演者,您必须使用或
过滤器。
此外,为了获得最佳性能,请使用filtered
查询,而不是顶级过滤器——在前一种情况下,过滤器首先运行,对您的语料库进行切片并仅对该子集执行查询。
代码应该是这样的:
Tire.search 'events' do
query do
filtered do
query do
string params[:query]
end
filter :or, terms: { organization_ids: current_user.followed_performers.collect(&:id).map(&:to_s) },
terms: { watcher_ids: [current_user.id.to_s] }
end
end
end
有关更多示例,请参阅集成测试:
关于ruby-on-rails - 使用 ElasticSearch + Tire 搜索多个术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997581/