4

sunspot_solr の使用に関連する問題があります。この宝石を使用していくつかの検索を実行するアプリケーションがあります。この機能を持つモデルは 5 つありますが、エラーが表示されるのはそのうちの 1 つだけです。

 @search.results

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

これが私のコードです:

コントローラ

@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)

モデル (購入):

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

もう 1 つの奇妙な点は、私の開発マシンでは、このコードが完全に機能することです。nginx を使用する実稼働マシンでは、コードにこのエラーが表示されます。

gems のバージョンを確認したところ、一致しました。私は試した

rake sunspot:solr:reindex RAILS_ENV=production

インデックスを再作成します。私は試した

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

検索サーバーを再起動します。solr/ フォルダーを削除して、開始スクリプトに再度コピーさせようとしました。

そして、他のモデルが完璧に機能するのはなぜですか? これを解決する方法はありますか?

ありがとう

4

1 に答える 1

2

私の場合は、キー フィールド (id) が一意でない状況でした。

これは、個別の id フィールドを持たない mysql ビューを設計したために発生しました。

そのため、次の一意でない行のインデックス作成中に、sunspot が常に最初のヒットを nil に「落とした」のです。

そう

hit.result = result

太陽黒点の gem コードのどこかでエラーが発生しました


私がそれを理解したとき(私はそれに数時間を費やしました)、私はidフィールドを一意にし、モデルを再インデックスし、問題はなくなりました。

于 2015-06-11T18:31:12.557 に答える