的解決方案是猴子修補雙方Mailboxer ::收據和Mailboxer ::型號:: Messageable模塊,允許通過搜索框類型:爲每個郵箱類型
class Mailboxer::Receipt < ActiveRecord::Base
...
if Mailboxer.search_enabled
searchable do
text :subject, :boost => 5 do
message.subject if message
end
text :body do
message.body if message
end
integer :receiver_id
# Add mailbox_type to sunspot search field
string :mailbox_type
boolean :trashed
boolean :deleted
end
end
end
和Messageable模塊下,我已經添加了新的方法search__messages:
module Mailboxer
module Models
module Messageable
def search_messages(query)
# Replaces 'search' with alias 'solr_search' since ransack search clashes with solr search method
@search = Mailboxer::Receipt.solr_search do
fulltext query
with :receiver_id, self.id
end
@search.results.map { |r| r.conversation }.uniq
end
# Adds additional Mailboxer search functionality to search by box type
def search_inbox_messages(query)
@search = Mailboxer::Receipt.solr_search do
fulltext query
with :receiver_id, self.id
with(:mailbox_type).equal_to("inbox")
end
@search.results.map { |r| r.conversation }.uniq
end
def search_sent_messages(query)
@search = Mailboxer::Receipt.solr_search do
fulltext query
with :receiver_id, self.id
with(:mailbox_type).equal_to("sentbox")
end
@search.results.map { |r| r.conversation }.uniq
end
def search_trash_messages(query)
@search = Mailboxer::Receipt.solr_search do
fulltext query
with :receiver_id, self.id
with :trashed, true
with :deleted, false
end
@search.results.map { |r| r.conversation }.uniq
end
end
end
end
有了這個,在您的控制器中,您可以簡單地使用新定義的方法來執行箱型搜索。
current_user.search_inbox_messages(query[:keywords])
current_user.search_sentbox_messages(query[:keywords])
current_user.search_trash_messages(query[:keywords])
重要提示:請務必添加新的搜索領域後再生Solr的索引,否則什麼也不會顯示出來:
bundle exec rake sunspot:solr:reindex
希望這有助於!
如果是的話,你有沒有實現搜索功能,請讓我知道,因爲我必須實現相同的功能。我遵循github,但它沒有工作.... – 2014-05-14 05:46:51