![](/img/trans.png)
[英]Sunspot Solr Rails - Search Multiple Models using 'with'
[英]Rails + Sunspot: Multiple model search, but only certain fields on one of the models?
在我的Rails應用程序中,我使用太陽黑子來索引幾個不同的模型。 然后我有一個全局搜索表單,返回混合結果。 這很好用:
Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress) do
fulltext params[:q]
paginate :per_page => 10
end
我想在此搜索中添加一個額外的模型,比如Project。 Project模型有很多索引:
class Project < ActiveRecord::Base
searchable do
string :category do
category.name.downcase if category = self.category
end
string :client do
client.name.downcase if client = self.client
end
string :status
text :tracking_number
text :description
integer :category_id, :references => Category
integer :client_id, :references => Client
integer :tag_ids, :multiple => true, :references => Tag
time :created_at, :trie => true
time :updated_at, :trie => true
time :received_at, :trie => true
time :completed_at, :trie => true
end
end
如何修改我原來的Sunspot.search
調用,僅通過tracking_number
字段添加搜索項目記錄,而不是 description
字段?
Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress, Project) do
fulltext params[:q] do
fields(:tracking_number, :other_fields_outside_your_project_model)
end
paginate :per_page => 10
end
這將在tracking_number字段和您指定的任何其他字段上進行全文搜索,尤其是在Person,EmailAddress,PhoneNumber和PhysicalAddress模型中。
我認為你必須將tracking_number定義為文本字段而不是字符串字段。 僅對“文本字段”進行全文搜索。
你試過這個:
text:tracking_number
你的太陽黑子搜索看起來像:
Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress, Project) do
fulltext params[:q]
paginate :per_page => 10
end
再見
你嘗試過這樣的事情:
Sunspot.search(Post) do
keywords 'great pizza', :fields => [:title, :body]
end
您可以為每個模型發出一個請求,然后只在一個列表中將結果連接起來。 我想你不能在一次搜索中做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.