has_search_index_backend.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module HasSearchIndexBackend
  3. extend ActiveSupport::Concern
  4. included do
  5. after_create :search_index_update
  6. after_update :search_index_update
  7. after_touch :search_index_update
  8. after_destroy :search_index_destroy
  9. end
  10. =begin
  11. update search index, if configured - will be executed automatically
  12. model = Model.find(123)
  13. model.search_index_update
  14. =end
  15. def search_index_update
  16. return true if ignore_search_indexing?(:update)
  17. # start background job to transfer data to search index
  18. return true if !SearchIndexBackend.enabled?
  19. Delayed::Job.enqueue(BackgroundJobSearchIndex.new(self.class.to_s, id))
  20. true
  21. end
  22. =begin
  23. delete search index object, will be executed automatically
  24. model = Model.find(123)
  25. model.search_index_destroy
  26. =end
  27. def search_index_destroy
  28. return true if ignore_search_indexing?(:destroy)
  29. SearchIndexBackend.remove(self.class.to_s, id)
  30. true
  31. end
  32. =begin
  33. collect data to index and send to backend
  34. ticket = Ticket.find(123)
  35. result = ticket.search_index_update_backend
  36. returns
  37. result = true # false
  38. =end
  39. def search_index_update_backend
  40. # fill up with search data
  41. attributes = search_index_attribute_lookup
  42. return true if !attributes
  43. # update backend
  44. SearchIndexBackend.add(self.class.to_s, attributes)
  45. true
  46. end
  47. =begin
  48. get data to store in search index
  49. ticket = Ticket.find(123)
  50. result = ticket.search_index_data
  51. returns
  52. result = {
  53. attribute1: 'some value',
  54. attribute2: ['value 1', 'value 2'],
  55. ...
  56. }
  57. =end
  58. def search_index_data
  59. attributes = {}
  60. %w[name note].each do |key|
  61. next if !self[key]
  62. next if self[key].respond_to?('blank?') && self[key].blank?
  63. attributes[key] = self[key]
  64. end
  65. return true if attributes.blank?
  66. attributes
  67. end
  68. def ignore_search_indexing?(_action)
  69. false
  70. end
  71. # methods defined here are going to extend the class, not the instance of it
  72. class_methods do
  73. =begin
  74. serve methode to ignore model attributes in search index
  75. class Model < ApplicationModel
  76. include HasSearchIndexBackend
  77. search_index_attributes_ignored :password, :image
  78. end
  79. =end
  80. def search_index_attributes_ignored(*attributes)
  81. @search_index_attributes_ignored = attributes
  82. end
  83. =begin
  84. reload search index with full data
  85. Model.search_index_reload
  86. =end
  87. def search_index_reload
  88. tolerance = 5
  89. tolerance_count = 0
  90. ids = all.order('created_at DESC').pluck(:id)
  91. ids.each do |item_id|
  92. item = find(item_id)
  93. next if item.ignore_search_indexing?(:destroy)
  94. begin
  95. item.search_index_update_backend
  96. rescue => e
  97. logger.error "Unable to send #{item.class}.find(#{item.id}).search_index_update_backend backend: #{e.inspect}"
  98. tolerance_count += 1
  99. raise "Unable to send #{item.class}.find(#{item.id}).search_index_update_backend backend: #{e.inspect}" if tolerance_count == tolerance
  100. end
  101. end
  102. end
  103. end
  104. end