为当前环境添加索引名称的前缀
您可以为每个环境(在您的情况下:测试环境)设置不同的索引名称。
例如,您可以在
config/initializers/tire.rb
使用以下行:
Tire::Model::Search.index_prefix "#{Rails.application.class.parent_name.downcase}_#{Rails.env.to_s.downcase}"
删除索引的一种可能的方法
假设您有名为 Customer、Order 和 Product 的模型,请将以下代码放在 test-startup/before-block/each-run-block 的某个位置。
# iterate over the model types
# there are also ways to fetch all model classes of the rails app automaticly, e.g.:
# http://stackoverflow.com/questions/516579/is-there-a-way-to-get-a-collection-of-all-the-models-in-your-rails-app
[Customer, Order, Product].each do |klass|
# make sure that the current model is using tire
if klass.respond_to? :tire
# delete the index for the current model
klass.tire.index.delete
# the mapping definition must get executed again. for that, we reload the model class.
load File.expand_path("../../app/models/#{klass.name.downcase}.rb", __FILE__)
end
end
选择
另一种方法是设置一个不同的 ElasticSearch 实例以在另一个端口上进行测试,比如 1234。然后在您的 enviornment/test.rb 中设置
Tire::Configuration.url "http://localhost:1234"
然后在合适的位置(例如您的测试启动),您可以删除 ElasticSearch 测试实例上的所有索引:
Tire::Configuration.client.delete(Tire::Configuration.url)
也许您仍然必须确保您的模型类的轮胎映射定义仍然被调用。