簡體   English   中英

Rails + Sunspot:多個模型搜索,但只有其中一個模型的某些字段?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM