Search code examples
ruby-on-railssolrsunspot

What is wrong with this Sunspot Solr setup?


I am using sunspot to search my local db. After adding the gems, running the generate command, and booting up the solr server I do the following:

class Style < ActiveRecord::Base
  attr_accessible :full_name, :brand_name
  searchable do
    text :full_name
    text :brand_name
  end
end

Added the above to my Style model and re-indexed (I had already indexed prior to creating this post, which is why I re-indexed to put it here)

funkdified@vizio ~/rails_projects/goodsounds.org $ rake sunspot:solr:reindex
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
*Note: the reindex task will remove your current indexes and start from scratch.
If you have a large dataset, reindexing can take a very long time, possibly weeks.
This is not encouraged if you have anywhere near or over 1 million rows.
Are you sure you want to drop your indexes and completely reindex? (y/n)
y
[#######################################] [14/14] [100.00%] [00:00] [00:00] [53.19/s]

Then I try a search and get nothing

1.9.3p392 :003 > Style.search { fulltext 'Monkey' }.results
  SOLR Request (10.4ms)  [ path=#<RSolr::Client:0x0000000685ab28> parameters={data: fq=type%3AStyle&q=Monkey&fl=%2A+score&qf=full_name_text+brand_name_text&defType=dismax&start=0&rows=30, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8982/solr/select?wt=ruby, open_timeout: , read_timeout: , retry_503: , retry_after_limit: } ]
 => [] 

But, wait shouldn't it have worked and picked this up?

Style.first
  Style Load (1.3ms)  SELECT "styles".* FROM "styles" LIMIT 1
 => #<Style id: 54, brand_name: "Monkey", full_name "Monkey Chicken", created_at: "2013-02-01 23:25:58", updated_at: "2013-02-16 03:02:16"> 

Here is one more clue. I am seeing "unknown field" for brand_name (setup in Style.rb)

enter image description here


Solution

  • If you change the schema (the "searchable" block) you have to either reindex all models:

    rake sunspot:solr:reindex
    

    or reindex that specific model with a given batch size (here 500):

    rake sunspot:solr:reindex[500,Style]
    

    as per the Sunspot doco on Github (search "Reindexing Objects").

    FYI, to use Style.reindex for non-schema changes, you will have to call Sunspot.commit to save changes.