【问题标题】:Searchkick manual reindex on associations while testingSearchkick 在测试时手动重新索引关联
【发布时间】:2018-08-09 16:42:23
【问题描述】:

我正在手动调用关联的重新索引,以便它们按照docs 的规定保持最新。

但是,在我的测试环境中,重新索引调用会引发错误。在 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' ....

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

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

【问题讨论】:

    标签: ruby-on-rails elasticsearch searchkick


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 2023-03-09
      相关资源
      最近更新 更多