MKK
MKK

Reputation: 2753

How can I implement sunspot search when it's nested

How can I extend my search?
I'd like to search User at users_controllers#index when user pressed search button.

Now I have 3 models.

User > User_profile > Prefecture

User_profile has these column such as user_id, and prefecture_id.

In Prefecture model, it has its id, and name (Exp: California, New York)

Now I have setup models/user.rb like this. If I want to add prefecture search, what should I add to this? User should be able to type in California, and it hits search.

searchable do 
    text :user_profile_nickname do
        user_profile.nickname
    end

    text :user_profile_introduction do
        user_profile.introduction
    end

    text :tag_list do 
        tag_list
    end 

end

Upvotes: 4

Views: 210

Answers (1)

Leonard Saers
Leonard Saers

Reputation: 669

Each user will be stored as a document in Solr and you need to give the prefacture information to the user document in order for it to be searchable.

Try:

searchable do 
  text :user_profile_nickname do
    user_profile.nickname
  end

  text :user_profile_introduction do
    user_profile.introduction
  end

  text :tag_list do 
    tag_list
  end

  string :prefacture do
     user_profile.prefacture.name
  end

end

I would use string instead of text since you don't need to apply text processing such as stemming on the prefacture. And with Sunspot I don't think it's possible to build facets on text fields.

Upvotes: 1

Related Questions