简体   繁体   English

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

[英]Rails + Sunspot: Multiple model search, but only certain fields on one of the models?

In my Rails app I'm using Sunspot to index a few different models. 在我的Rails应用程序中,我使用太阳黑子来索引几个不同的模型。 I then have a global search form which returns mixed results. 然后我有一个全局搜索表单,返回混合结果。 This is working fine: 这很好用:

Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress) do
  fulltext params[:q]
  paginate :per_page => 10
end

I would like to add an additional model, say Project, to this search. 我想在此搜索中添加一个额外的模型,比如Project。 The Project model has quite a bit that is indexed: 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

How can I modify my original Sunspot.search call to add searching for Project records by just the tracking_number field and not the description field? 如何修改我原来的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

This will do full text search on tracking_number field and any other fields you specify, particularly in your Person, EmailAddress, PhoneNumber, and PhysicalAddress models. 这将在tracking_number字段和您指定的任何其他字段上进行全文搜索,尤其是在Person,EmailAddress,PhoneNumber和PhysicalAddress模型中。

I think you have to define your tracking_number as a text field and not a string field. 我认为你必须将tracking_number定义为文本字段而不是字符串字段。 Full text search only on "text fields". 仅对“文本字段”进行全文搜索。

Did you try this : 你试过这个:

text:tracking_number

And your sunspot search looks like : 你的太阳黑子搜索看起来像:

Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress, Project) do
  fulltext params[:q]
  paginate :per_page => 10
end

See you 再见

Did you try something like : 你尝试过这样的事情:

Sunspot.search(Post) do
  keywords 'great pizza', :fields => [:title, :body]
end

You can make one request for each model and then concat your results in only one list. 您可以为每个模型发出一个请求,然后只在一个列表中将结果连接起来。 I think you can't make it on one search. 我想你不能在一次搜索中做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM