searchindex_helper.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. require 'rake'
  2. module SearchindexHelper
  3. def self.included(base)
  4. base.teardown do
  5. next if ENV['ES_URL'].blank?
  6. Rake::Task['searchindex:drop'].execute
  7. end
  8. end
  9. def configure_elasticsearch(required: false)
  10. if ENV['ES_URL'].blank?
  11. return if !required
  12. raise "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
  13. end
  14. Setting.set('es_url', ENV['ES_URL'])
  15. # Setting.set('es_url', 'http://127.0.0.1:9200')
  16. # Setting.set('es_index', 'estest.local_zammad')
  17. # Setting.set('es_user', 'elasticsearch')
  18. # Setting.set('es_password', 'zammad')
  19. if ENV['ES_INDEX_RAND'].present?
  20. rand_id = ENV.fetch('CI_JOB_ID', "r#{rand(999)}")
  21. test_method_name = method_name.gsub(/[^\w]/, '_')
  22. ENV['ES_INDEX'] = "es_index_#{test_method_name}_#{rand_id}_#{rand(999_999_999)}"
  23. end
  24. if ENV['ES_INDEX'].blank?
  25. raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
  26. end
  27. Setting.set('es_index', ENV['ES_INDEX'])
  28. # set max attachment size in mb
  29. Setting.set('es_attachment_max_size_in_mb', 1)
  30. yield if block_given?
  31. end
  32. def rebuild_searchindex
  33. Rake::Task.clear
  34. Zammad::Application.load_tasks
  35. Rake::Task['searchindex:rebuild'].execute
  36. end
  37. end