为当前环境添加索引名前缀
您可以 for each 环境设置不同的索引名(在您的示例中为测试环境).
例如,您可以在中创建初始值设定项
config/initializers/tire.rb
使用以下行:
Tire::Model::Search.index_prefix "#{Rails.application.class.parent_name.downcase}_#{Rails.env.to_s.downcase}"
一种删除索引的可行方法
假设您有名为Customer、Order和Product的模型,将以下代码放在测试启动/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)上进行测试.
Tire::Configuration.url "http://localhost:1234"
然后,在适当的位置(例如,您的测试启动),您可以使用以下命令删除ElasticSearch测试实例上的所有索引:
Tire::Configuration.client.delete(Tire::Configuration.url)
也许您仍然必须确保模型类的轮胎映射定义仍然被调用.