加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails太阳黑子:多个模型搜索,但只有某个模型

发布时间:2020-12-17 02:31:49 所属栏目:百科 来源:网络整理
导读:在我的Rails应用程序中,我使用太阳黑子来索引几个不同的模型.然后我有一个全局搜索表单,返回混合结果.这很好用: Sunspot.search(Person,EmailAddress,PhoneNumber,PhysicalAddress) do fulltext params[:q] paginate :per_page = 10end 我想在此搜索中添加
在我的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字段添加搜索项目记录,而不是描述字段?

解决方法

Sunspot.search(Person,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,PhoneNumber和PhysicalAddress模型中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读