我想在我的Rails 5项目中使用ThinkingSphinx。我在http://freelancing-gods.com/thinking-sphinx/ThinkingSphinx:SQL支持索引上的OR条件?
上读取指令我需要在SQL支持索引上实现OR
逻辑。
这里是我的类:
class Message < ApplicationRecord
belongs_to :sender, class_name: 'User', :inverse_of => :messages
belongs_to :recipient, class_name: 'User', :inverse_of => :messages
end
及其索引:
ThinkingSphinx::Index.define :message, :with => :active_record, :delta => true do
indexes text
indexes sender.email, :as => :sender_email, :sortable => true
indexes recipient.email, :as => :recipient_email, :sortable => true
has sender_id, created_at, updated_at
has recipient_id
end
schema.rb:
create_table "messages", force: :cascade do |t|
t.integer "sender_id"
t.integer "recipient_id"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "read", default: false
t.boolean "spam", default: false
t.boolean "delta", default: true, null: false
t.index ["recipient_id"], name: "index_messages_on_recipient_id", using: :btree
t.index ["sender_id"], name: "index_messages_on_sender_id", using: :btree
end
所以我需要在搜索仅在2个指标一次 - :sender_email
和:recipient_email
- 但ignorin g indexes text
。
伪代码,我需要这样的:
Message.search '[email protected]' :conditions => {:sender_email => '[email protected]' OR :receiver_email => '[email protected]'}
这意味着:找到所有“[email protected]”和“[email protected]”的消息(他们每个人可以是一个发件人或收件人) - 忽略包含文字的消息,文字为'[email protected]'或'[email protected]'。
不幸的是,该文件说:
The :conditions option must be a hash, with each key a field and each value a string.
换句话说,我需要一个条件指标集(在运行时) - 但同时在2个索引(而不是1,记录)。
我的意思是只允许散列作为条件是一个坏主意 - 并且没有字符串(如ActiveRecord查询确实允许http://guides.rubyonrails.org/active_record_querying.html#pure-string-conditions)。
PS我会说ThinkingSphinx文档http://freelancing-gods.com/thinking-sphinx/是非常糟糕的,需要从头开始完全重写。我读了这一切,什么都不明白。它没有例子(完整的例子 - 只有部分 - 因此完全不清楚)。我甚至不明白什么是领域和属性,它们又有什么不同。协会,条件等 - 都不清楚。很坏。宝石本身看起来不错 - 但它的文档很糟糕。