search_index_es.rake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. $LOAD_PATH << './lib'
  2. require 'rubygems'
  3. namespace :searchindex do
  4. task :drop, [:opts] => :environment do |_t, _args|
  5. # drop indexes
  6. print 'drop indexes...'
  7. SearchIndexBackend.index(
  8. action: 'delete',
  9. )
  10. puts 'done'
  11. Rake::Task['searchindex:drop_pipeline'].execute
  12. end
  13. task :create, [:opts] => :environment do |_t, _args|
  14. print 'create indexes...'
  15. # es with mapper-attachments plugin
  16. info = SearchIndexBackend.info
  17. number = nil
  18. if info.present?
  19. number = info['version']['number'].to_s
  20. end
  21. if number.blank? || number =~ /^[2-4]\./ || number =~ /^5\.[0-5]\./
  22. # create indexes
  23. SearchIndexBackend.index(
  24. action: 'create',
  25. data: {
  26. mappings: {
  27. Ticket: {
  28. _source: { excludes: [ 'article.attachment' ] },
  29. properties: {
  30. article: {
  31. type: 'nested',
  32. include_in_parent: true,
  33. properties: {
  34. attachment: {
  35. type: 'attachment',
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. )
  44. puts 'done'
  45. Setting.set('es_pipeline', '')
  46. # es with ingest-attachment plugin
  47. else
  48. # create indexes
  49. SearchIndexBackend.index(
  50. action: 'create',
  51. data: {
  52. mappings: {
  53. Ticket: {
  54. _source: { excludes: [ 'article.attachment' ] },
  55. }
  56. }
  57. }
  58. )
  59. puts 'done'
  60. end
  61. Rake::Task['searchindex:create_pipeline'].execute
  62. end
  63. task :create_pipeline, [:opts] => :environment do |_t, _args|
  64. # es with mapper-attachments plugin
  65. info = SearchIndexBackend.info
  66. number = nil
  67. if info.present?
  68. number = info['version']['number'].to_s
  69. end
  70. next if number.blank? || number =~ /^[2-4]\./ || number =~ /^5\.[0-5]\./
  71. # update processors
  72. pipeline = Setting.get('es_pipeline')
  73. if pipeline.blank?
  74. pipeline = "zammad#{rand(999_999_999_999)}"
  75. Setting.set('es_pipeline', pipeline)
  76. end
  77. print 'create pipeline (pipeline)... '
  78. SearchIndexBackend.processors(
  79. "_ingest/pipeline/#{pipeline}": [
  80. {
  81. action: 'delete',
  82. },
  83. {
  84. action: 'create',
  85. description: 'Extract zammad-attachment information from arrays',
  86. processors: [
  87. {
  88. foreach: {
  89. field: 'article',
  90. ignore_failure: true,
  91. processor: {
  92. foreach: {
  93. field: '_ingest._value.attachment',
  94. ignore_failure: true,
  95. processor: {
  96. attachment: {
  97. target_field: '_ingest._value',
  98. field: '_ingest._value._content',
  99. ignore_failure: true,
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. ]
  107. }
  108. ]
  109. )
  110. puts 'done'
  111. end
  112. task :drop_pipeline, [:opts] => :environment do |_t, _args|
  113. # es with mapper-attachments plugin
  114. info = SearchIndexBackend.info
  115. number = nil
  116. if info.present?
  117. number = info['version']['number'].to_s
  118. end
  119. next if number.blank? || number =~ /^[2-4]\./ || number =~ /^5\.[0-5]\./
  120. # update processors
  121. pipeline = Setting.get('es_pipeline')
  122. next if pipeline.blank?
  123. print 'delete pipeline (pipeline)... '
  124. SearchIndexBackend.processors(
  125. "_ingest/pipeline/#{pipeline}": [
  126. {
  127. action: 'delete',
  128. },
  129. ]
  130. )
  131. puts 'done'
  132. end
  133. task :reload, [:opts] => :environment do |_t, _args|
  134. puts 'reload data...'
  135. Models.searchable.each do |model_class|
  136. puts " reload #{model_class}"
  137. started_at = Time.zone.now
  138. puts " - started at #{started_at}"
  139. model_class.search_index_reload
  140. took = Time.zone.now - started_at
  141. puts " - took #{took.to_i} seconds"
  142. end
  143. end
  144. task :rebuild, [:opts] => :environment do |_t, _args|
  145. Rake::Task['searchindex:drop'].execute
  146. Rake::Task['searchindex:create'].execute
  147. Rake::Task['searchindex:reload'].execute
  148. end
  149. end