Tag Archives: solr

Re-Index Sunspot/Solr – Ruby on Rails

Just a very quick post on how to re-index Sunspot/Solr – simply open the console and enter the following command:

chris@chris-VirtualBox:~/site$ rake sunspot:solr:reindex
 
WARNING: 'task :t, arg, :needs => [deps]' is deprecated.  Please use 'task :t, [args] => [deps]' instead.
    at /usr/lib/ruby/gems/1.8/gems/sunspot_rails-1.2.1/lib/sunspot/rails/tasks.rb:41
chris@chris-VirtualBox:~/site$

That’s all there is to it, nice and easy!

Will_Paginate with Sunspot – Ruby on Rails

Hey all,

I’ve just gone through setting up will_paginate with sunspot 1.3, just thought I’d share in case anyone else has any troubles/improvements.

In my case I’m setting it up on a products model.

  #products.rb
 
  #Sunspot/Search
  searchable do
    text :title, :boost => 5
    text :description
  end

#products_controller.rb
  #Products search
  def search
 
    #Create vars
    search = Product.search do
      fulltext params[:search]
      paginate :page => params[:page] || 1, :per_page => 2
    end    
 
    #Retrieve results
    @products = search.results
 
    #Create responses
    respond_to do |format|
      format.html { render 'index' }
    end
  end

#Search partial - _search.html.erb
<%= form_tag :controller => 'products', :action => 'search', :method => :get do %>
   <%= text_field_tag :search, params[:search], :size => '25', :class => 'search' %>
<% end %>

#Product display page - index.html.erb
<% @products.each do |product| %>
   <%= render :partial => "products/product_line", :locals => {:product => product} %>
<% end %>
 
<%= will_paginate @products, :container => false, :params => { :search_text => params[:search_text] } %>

Well that’s about it, if anyone needs any further info or has any tips, leave a comment and I’ll get back to you asap.

Uninstalling a Gem – Ruby on Rails

Due to a few recent issues with SunSpot (see my previous posts), I’ve decided to uninstall it until I have enough time to work on it properly. Once again, Ruby makes this pretty simple:

 
chris@chris-VirtualBox:~/site$ sudo gem uninstall sunspot
[sudo] password for chris: 
 
You have requested to uninstall the gem: sunspot-1.2.1 sunspot_rails-1.2.1 depends on [sunspot (= 1.2.1)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn]  y
 
 
Remove executables: sunspot-solr, sunspot-installer in addition to the gem? [Yn]  y
 
 
Removing sunspot-solr
Removing sunspot-installer
 
 
Successfully uninstalled sunspot-1.2.1

Errno::ECONNREFUSED in – Ruby on Rails

First problem of the morning – Errno::ECONNREFUSED in ProductsController#create. This one was actually a little bit tricky – for me anyway. The error seemed to imply a database issue, so I started by checking all the common problems there however no luck unfortunately.

After a bit of a Google I came across a post mentioning that a few people had encountered this after installing Solr. I had previously setup Sunspot, which just so happens to sit on top of Solr.

Manually starting solr fixed the problem – just using the code below:

chris@chris-VirtualBox:~/site$ rake sunspot:solr:start

 

I’m not really sure what triggered the error as I’d been using the exact same functionality all morning without any changes or issues. Thankfully it’s a quick fix. If anyone has anymore info on the problem please let me know.

Anyway, that’s all I’ve got for you for now, Good Luck!

UPDATE: I had this problem again this morning and realised that it was the models with search fields still defined that were triggering the error. Removing this code seems to have completely resolved the issue.