sunspot_solr - NoMethodError (undefined method `result=' for nil:NilClass)

I have a problem related to the use of sunspot_solr. I have an application that performs some searches using this gem. There are 5 models that has this feature, only 1 of them is showing an error in

 @search.results

NoMethodError (undefined method `result=' for nil:NilClass):
  app/controllers/financial_dashboards_controller.rb:27:in `index'

Here is my code:

Controller

@search = Sunspot.search(Purchase) do
   fulltext params[:search]
   with(:product_owner).equal_to(current_user.id)
   facet(:status)
   if params[:status].present?
      with(:status).equal_to(params[:status]) 
   end
   facet(:sell_date)
   if params[:sell_date].present?
      with(:sell_date).equal_to(params[:sell_date])
   end
   order_by(:sell_date, :desc)
end

#line 27
@sales = @search.results.paginate(:page => params[:page], :per_page => 5)

Model (Purchase):

searchable do
  text :product_name
  text :product_short_description
  integer :product_owner
  string :status
  string :sell_date
end         

def product_name
  product.name
end

def product_short_description
  product.short_description
end

def product_owner
  product.user.id
end

def sell_date
  date.to_s(:year_month)
end

#indicates status of the payment and delivery
def status()
    if !self.closed.nil?
    I18n.t('purchases.status.finished')
    elsif !self.measured.nil?
    I18n.t('purchases.status.measured')
    elsif !self.accomplished.nil?
    I18n.t('purchases.status.delivered')
    elsif !self.paid.nil?
    I18n.t('purchases.status.paid')
    elsif !self.canceled.nil?
    I18n.t('purchases.status.canceled')
    elsif !self.date.nil?
    I18n.t('purchases.status.waiting_payment')
    end
end

Another strange thing is that on my development machine, this code works perfectly. On production machine that uses nginx, the code displays this error.

I checked the version of gems and they match. I tried

rake sunspot:solr:reindex RAILS_ENV=production

to reindex. I tried

rake sunspot:solr:stop RAILS_ENV=production
rake sunspot:solr:start RAILS_ENV=production

to restart the search server. Even tried to remove solr/ folder and let the start script copy it again.

And why the other models work perfectly? Any ideas how to solve this?

Thanks

Upvotes: 4

Views: 1299

Answers (1)

okliv
okliv

Reputation: 3959

In my case, it was a situation when the key field (id) was not unique.

It happened because I have designed a mysql view with non distinct id field.

And that is why sunspot always "dropped" first hit to nil during next non unique row indexation.

So

hit.result = result

raised an error somewhere in sunspot gem code


When i figured it out (i've spent few hours on it), I just have made my id field unique, reindexed my models and problem have gone.

Upvotes: 2

Related Questions