search_index_es.rake 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. $LOAD_PATH << './lib'
  2. require 'rubygems'
  3. namespace :searchindex do
  4. task :drop, [:opts] => :environment do |_t, _args|
  5. # drop indexes
  6. puts 'drop indexes...'
  7. SearchIndexBackend.index(
  8. action: 'delete',
  9. )
  10. end
  11. task :create, [:opts] => :environment do |_t, _args|
  12. # create indexes
  13. puts 'create indexes...'
  14. SearchIndexBackend.index(
  15. action: 'create',
  16. data: {
  17. mappings: {
  18. Ticket: {
  19. _source: { excludes: [ 'article.attachment' ] },
  20. properties: {
  21. article: {
  22. type: 'nested',
  23. include_in_parent: true,
  24. properties: {
  25. attachment: {
  26. type: 'attachment',
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. )
  35. end
  36. task :reload, [:opts] => :environment do |_t, _args|
  37. puts 'reload data...'
  38. Models.searchable.each { |model_class|
  39. puts " reload #{model_class}"
  40. started_at = Time.zone.now
  41. puts " - started at #{started_at}"
  42. model_class.search_index_reload
  43. took = Time.zone.now - started_at
  44. puts " - took #{took.to_i} seconds"
  45. }
  46. end
  47. task :rebuild, [:opts] => :environment do |_t, _args|
  48. Rake::Task['searchindex:drop'].execute
  49. Rake::Task['searchindex:create'].execute
  50. Rake::Task['searchindex:reload'].execute
  51. end
  52. end