【问题标题】:Thinking Sphinx or condtion思考狮身人面像或条件
【发布时间】:2014-07-07 06:27:34
【问题描述】:

在思考狮身人面像时如何使用conditional or

情况是: 我有一个带有 sender_id 和 recipient_id 属性的Message 模型。我想编写这个查询:

  Message.where("sender_id = ? OR recipient_id = ?", business_id, business_id)

现在,我搜索了两次,一次搜索所有有 recipient_id = business_id 的消息,另一个返回所有有 sender_id = business_id 的消息。然后我只是合并它们。

我觉得有一种更有效的方法来做到这一点。

EDIT - 添加索引文件

ThinkingSphinx::Index.define :message, with: :active_record, delta: ThinkingSphinx::Deltas::DelayedDelta do
  # fields
  indexes body 

  # attributes
  has job_id
  has sender_id
  has recipient_id

end

【问题讨论】:

    标签: mysql ruby-on-rails-3.2 thinking-sphinx


    【解决方案1】:

    Sphinx 不允许属性之间的 OR 逻辑,只有字段。但是,一种解决方法是将两列合并为第三个属性:

    has [sender_id, recipient_id], :as => :business_ids, :multi => true
    

    然后您可以像这样搜索组合值:

    Message.search :with => {:business_ids => business_id}
    

    【讨论】:

    • 感谢 Pat 的回复,但是当我尝试解决方法时,它只会搜索带有 sender_id = business_id 的所有消息
    • 索引是基于 SQL 的,还是实时的?
    • 您好,您所说的基于 SQL 的索引或实时索引是什么意思?如果有帮助,我会发布索引文件。
    • 在您的索引中,您有with: :active_record,这意味着生成的索引是通过 SQL 查询填充的。 (另一方面,实时索引是通过 Ruby 和 Rails 应用程序的上下文填充的。)
    • 事实证明我的建议缺少一个设置::multi => true - 它通知 Sphinx 该属性有多个值(因此,发送者和接收者的 ID)。试一试,如果它不起作用,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多