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

I have a problem with using sunspot_solr. I have an application that does some searches using this gem. There are 5 models that have this function, only 1 of them shows 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 (Buying):

 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 weird thing: on my development machine, this code works fine. On a production machine using nginx, the code displays this error.

I checked the gemstones version and they match. I tried

 rake sunspot:solr:reindex RAILS_ENV=production 

for reindexing. 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 the solr / folder and let the start script copy it again.

And why do other models work fine? Any ideas how to solve this?

thanks

+4
source share
1 answer

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

This happened because I developed a mysql view with an odd identifier field.

And that’s why the sunspot always “fell” with the first hit to zero during the next non-interactive row indexing.

So

 hit.result = result 

raised an error somewhere in gem code sunspot


When I realized this (I spent several hours on this), I just made my id field unique, reindexed my models and the problem disappeared.

+1
source

Source: https://habr.com/ru/post/1432747/


All Articles