簡體   English   中英

如何在Rails應用程序中測試ElasticSearch(Rspec)

[英]How to test ElasticSearch in a Rails application (Rspec)

我想知道在使用ElasticSearch和Tire時如何在應用程序中測試搜索。

  • 您如何設置新的ElasticSearch測試實例? 有沒有辦法嘲笑它?

  • 你知道的任何寶石可能對此有所幫助嗎?


我發現一些有用的東西:

我發現了一篇很棒的文章回答了我所有的問題:)

http://bitsandbit.es/post/11295134047/unit-testing-with-tire-and-elastic-search#disqus_thread

此外,還有來自輪胎作者卡米的答案。

這也很有用: https//github.com/karmi/tire/wiki/Integration-Testing-Rails-Models-with-Tire

在問之前我無法相信我沒有找到這些......

為當前環境添加索引名稱前綴

您可以為每個環境設置不同的索引名稱(在您的情況下:測試環境)。

例如,您可以在中創建初始化程序

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)

也許您仍然必須確保您的模型類的輪胎映射定義仍然被調用。

在我的rspec套件中通過輪胎刪除我的彈性搜索索引時遇到了一個奇怪的錯誤。 在我的Rspec配置中,類似於Bits和Bytes博客,我有一個after_each調用,它清理數據庫並清除索引。

我發現我需要調用Tire的create_elasticsearch_index方法,該方法負責讀取ActiveRecord類中的映射以設置適當的分析器等。我看到的問題是我在我的模型中有一些:not_analyzed字段實際上正在分析(這打破了我想要分面工作的方式)。

開發方面的一切都很好,但測試套件失敗了,因為各個單詞而不是整個多字符串都破壞了方面。 刪除索引后,似乎沒有在rspec中正確創建映射配置。 添加create_elasticsearch_index調用修復了問題:

config.after(:each) do
  DatabaseCleaner.clean
  Media.tire.index.delete
  Media.tire.create_elasticsearch_index
end

媒體是我的模特課。

我遇到了類似的問題,這就是我解決它的方法。 請記住,我的解決方案建立在@spaudanjo解決方案之上。 由於我正在使用spork,我在spec_helper.rbSpork.each_run塊中添加了這個,但是你可以將它添加到任何其他每個/之前的塊中。

# Define random prefix to prevent indexes from clashing
Tire::Model::Search.index_prefix "#{Rails.application.class.parent_name.downcase}_#{Rails.env.to_s.downcase}_#{rand(1000000)}"

# In order to know what all of the models are, we need to load all of them
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
  load model
end

# Refresh Elastic Search indexes
# NOTE: relies on all app/models/**/*.rb to be loaded
models = ActiveRecord::Base.subclasses.collect { |type| type.name }.sort
models.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

它基本上為每個測試用例定義了它自己的唯一前綴,因此索引中沒有。 其他解決方案都遇到了一個問題,即使在刪除索引之后,Elastic Search也不會刷新索引(即使在運行Model.index.refresh ),這就是隨機化前綴存在的原因。

它還會加載每個模型並檢查它是否響應tire以便我們不再需要維護在spec_helper.rb和其他區域中響應輪胎的所有模型的列表。

由於此方法在使用后不會“刪除”索引,因此您必須定期手動刪除它。 雖然我不認為這是一個很大的問題,但您可以使用以下命令刪除:

curl -XDELETE 'http://localhost:9200/YOURRAILSNAMEHERE_test_*/'

要查找YOURRAILSNAMEHERE是什么,請運行rails console並運行Rails.application.class.parent_name.downcase 輸出將是您項目的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM