0

I have trouble with searching with the sunspot gem. I want to search for user email in an object tenant.

tenant has many users

In the tenant I did this:

searchable do
  text :name, :notifications_email

  text :users do  
    users.map(&:email)  
  end
end

Searching for the name and notifications_email works fine, but when I search for an email of the user no result is found.

I did this in the console:

s = Tenant.solr_search do fulltext "info" end

I get this object:

<Sunspot::Search:{:fq=>["type:Tenant"], :q=>"info", :fl=>"* score", :qf=>"name_text   notifications_email_text users_text", :defType=>"dismax", :start=>0, :rows=>30}>

What confuses me is that there is users_text doesn't it have to be users_email_text or something like this?

James Chevalier
  • 10,604
  • 5
  • 48
  • 74
michael_knight
  • 4,921
  • 3
  • 17
  • 10

1 Answers1

0

To make it more clear, you should define your searchable block like:

searchable do
  text :name
  text :notifications_email
  text :users_email do  
    users.map(&:email)
  end  
end

During index time, Sunspot appends the type of the attribute to the end of name of the attribute. In the case of :users_email, it gets turned into qf=>users_email_text. It seems like your confusion is around the naming conventions. I could have defined the email attribute to anything I wanted, as long as I defined what it contained in the do block. Defining it like this:

searchable do
  text :name
  text :notifications_email
  text :emails do  
    users.map(&:email)
  end  
end

Would have given me the same functionality, just Sunspot would turn the variable name into qf=>emails_text durning index time.

vanhowen
  • 153
  • 1
  • 7