运行测试时 ElasticSearch Rails resource_already_exists_exception

seb*_*seb 5 ruby ruby-on-rails minitest elasticsearch elasticsearch-rails

我正在尝试运行我的一个测试,该测试进行搜索,试图断言搜索结果中包含记录,但与此同时,我收到一个Elasticsearch::Transport::Transport::Errors::BadRequest错误:

SearchTest#test_simple_test_returns_product:
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] 

{
  "error":{
    "root_cause":[
      {
        "type":"resource_already_exists_exception",
        "reason":"index [app_application_test_products/FTt1YC6eQrCw2XwJuqjmDw] already exists",
        "index_uuid":"FTt1YC6eQrCw2XwJuqjmDw",
        "index":"app_application_test_products"
      }
    ],
    "type":"resource_already_exists_exception",
    "reason":"index [app_application_test_products/FTt1YC6eQrCw2XwJuqjmDw] already exists",
    "index_uuid":"FTt1YC6eQrCw2XwJuqjmDw",
    "index":"app_application_test_products"
  },
  "status":400
}
Run Code Online (Sandbox Code Playgroud)

当我在开发中执行搜索时,它按预期工作,但在测试中抛出此类错误,在测试中我添加了导入和索引刷新,仅此而已:

class SearchTest < ActiveSupport::TestCase
  setup do
    Product.import force: true
    Product.__elasticsearch__.refresh_index!
  end

  test "simple test returns product" do
    product = products(:one)
    I18n.locale = product.market.lang
    search = Search.new(
      category: product.category.custom_slug,
      page: 1,
      market_id: product.market_id,
      status: "active",
      seed: Date.today.to_time.to_i
    )
    assert_includes search.results.records, products(:one)
    assert_includes search.results.records, products(:two)
    assert_not_includes search.results.records, products(:three)
  end
end
Run Code Online (Sandbox Code Playgroud)

任何帮助以及改进代码的提示都将受到赞赏。

我在用着:

# Gemfile
gem 'minitest', '5.10.1'

# Gemfile.lock
elasticsearch (6.1.0)
elasticsearch-model (6.0.0)
elasticsearch-rails (6.0.0)
minitest (= 5.10.1)
Run Code Online (Sandbox Code Playgroud)

seb*_*seb 0

我的模型中的映射错误。type我使用的是 ,而不是使用该选项index,这使得 ElasticSearch 创建了多个映射。从 6.4 版本开始就不再可用了(我猜)。