繁体   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