简体   繁体   中英

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. 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. The Project model has quite a bit that is indexed:

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(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.

I think you have to define your tracking_number as a text field and not a string field. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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