Searchkick手动重新索引测试时的关联

时间:2018-05-02 23:22:15

标签: ruby-on-rails elasticsearch searchkick

我手动调用关联的重新索引,以便它们按照docs的规定更新。

但是,在我的测试环境中,reindex调用正在抛出错误。在CI上,错误是一个可以理解的端口9200没有运行,因为ES没有运行。在本地,错误看起来更像是显示文档不存在。

我的test_helper

中有规定的Searchkick.disable_callbacks

设定:

class Parent < ApplicationRecord
  belongs_to :client

  searchkick

  def search_data
    { name }.merge(**client_data)
  end

  def client_data
    { market_id: client.market_id }
  end
end

class Client < ApplicationRecord
  has_many :parents

  after_save :reindex_parents

  def reindex_parents
    parents.reindex(:client_data) # <-- ERROR raised here without `unless Rails.env.test?` guard
  end
end

错误:

TestClass#test_name:
Searchkick::ImportError: {"type"=>"document_missing_exception", "reason"=>"[model][395824130]: document missing", "index_uuid"=>"5UOKtvfvR52x76Nf5njMBQ", "shard"=>"0", "index"=>"students_test"} on item with id '395824130' ....

我可以通过使用reindex_parents保护unless Rails.env.test?来避免此问题,但似乎 应该是更好的方式

我错过了什么吗? 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

由于禁用了回调,因此搜索索引将没有任何数据。当reindex_parents尝试更新不存在的文档时,将引发错误。如果感觉更干净,可以使用unless Rails.env.test?代替if Searchkick.callbacks?