3
votes

I'm unable to index my database for searching with sunspot_rails. I get the following error:

Execute sunspot:reindex rake aborted! undefined local variable or method 'counter'

I'm getting the following output after running rake sunspot:reindex. I'm a novice at Rails. I want to add sunspot_rails to my project to add search functionality (with the hope of deploying the project with Heroku).

I'm using Rails 3. I followed the instructions here: https://github.com/outoftime/sunspot/blob/master/sunspot_rails/README.rdoc. My various other attempts to diagnose the problem included:

  • installing sunspot in addition to sunspot_rails.
  • I ended up with sunspot_rails v. 1.2.0 and 1.2.1 so I uninstalled 1.2.1 because I have sunspot_rails 1.2.0.
  • installed the nokogiri gem which I understand is a dependency for sunspot_rails.
  • installed libxml2 separately following the instructions here to install nokogiri: http://www.engineyard.com/blog/2010/getting-started-with-nokogiri/

    ** Invoke sunspot:reindex (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute sunspot:reindex rake aborted! undefined local variable or method counter' for [removed pound]<Class:0x10359aef8> /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:inmethod_missing' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:235:in solr_index' /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation/batches.rb:71:infind_in_batches' /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:440:in __send__' /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:440:infind_in_batches' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:234:in solr_index' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:184:insolr_reindex' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/tasks.rb:57 /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/tasks.rb:56:in each' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/tasks.rb:56 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:incall' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:ineach' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:ininvoke_with_call_chain' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in invoke' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:ininvoke_task' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:ineach' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:inrun' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:inrun' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31 /usr/bin/rake:19:in `load' /usr/bin/rake:19

This is what I have in class I'd like to search:

  searchable do
    text :fname
    text :mname
    text :lname, :default_boost => 2   end

Any help would be greatly appreciated!

3

3 Answers

2
votes

The error is in the sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:235 code. You should fix it by yourself.

And fixing it is very easy:

    def solr_index(opts={})
      options = {
        :batch_size => 500,
        :batch_commit => true,
        :include => self.sunspot_options[:include],
        :first_id => 0
      }.merge(opts)

      if options[:batch_size]
        counter = 1 #Add the variable
        find_in_batches(:include => options[:include], :batch_size => options[:batch_size]) do |records|
          solr_benchmark options[:batch_size], counter do
            Sunspot.index(records)
          end
          Sunspot.commit if options[:batch_commit]
          counter += 1 # Increase the variable
        end
        Sunspot.commit unless options[:batch_commit]
      else
        Sunspot.index!(all(:include => options[:include]))
      end
    end

Add counter variable in the front of find_in_batches, and increase it at the end of find_in_batches block. And this variable is using for benchmark. Don't worry~

1
votes

Just to add a note, this is now fixed in Sunspot 1.2.1

0
votes

That's a pretty timely intervention, user34.

Just installing Sunspot for Rails 3.0.1 running from Ubuntu 10.4. After first stumbling on connection problems with reindexing (Ubuntu users need to be sure they have Javascript running, it seems - sudo apt-get install default-jdk), I then ran into the same problem as Walter with 'counter'.

Is this a new Sunspot issue, I wonder?

Anyway, you posted your reply just a couple of hours before I needed it .. and as far as I can see, I look good to go now.

Thanks to both of you .. (and ALWAYS be wary of 5 minute installation claims :) )