我想在我的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/是非常糟糕的,需要從頭開始完全重寫。我讀了這一切,什麼都不明白。它沒有例子(完整的例子 - 只有部分 - 因此完全不清楚)。我甚至不明白什麼是領域和屬性,它們又有什麼不同。協會,條件等 - 都不清楚。很壞。寶石本身看起來不錯 - 但它的文檔很糟糕。