ruby-on-rails - 使用 ElasticSearch + Tire 搜索多个术语

标签 ruby-on-rails ruby mongoid elasticsearch tire

将 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/

相关文章:

ruby-on-rails - mongoid-回形针 : Photo is not recognized by the 'identify' command

ruby-on-rails - Rails 仅作为 API 需要实例变量吗?

ruby-on-rails - 获取渲染以识别自定义路由路径

ruby-on-rails - 设计记住我和 session

ruby-on-rails - ruby regex 使用最后一个匹配来分隔字符串,但应该首先使用

collections - 如何使用 Mongoid 判断 MongoDB 中是否存在集合?

ruby - MongoDB 中与 Ruby 一起使用的字符串数据类型的最大长度是多少?

javascript - 每当输入文本字段更改时,如何在 RoR 中提交表单?

ruby-on-rails - form_for 辅助方法上的嵌套命名空间 - Rails

ruby - 生成json的哈希结构是什么?