2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2017-05-02 15:21:13 +00:00
|
|
|
module HasSearchIndexBackend
|
2017-01-31 17:13:45 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_create :search_index_update
|
|
|
|
after_update :search_index_update
|
|
|
|
after_touch :search_index_update
|
|
|
|
after_destroy :search_index_destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
update search index, if configured - will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.search_index_update
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_update
|
2017-09-11 00:50:05 +00:00
|
|
|
return true if ignore_search_indexing?(:update)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
# start background job to transfer data to search index
|
2017-09-11 00:50:05 +00:00
|
|
|
return true if !SearchIndexBackend.enabled?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-01-15 12:32:14 +00:00
|
|
|
SearchIndexJob.perform_later(self.class.to_s, id)
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
delete search index object, will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.search_index_destroy
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_destroy
|
2017-09-11 00:50:05 +00:00
|
|
|
return true if ignore_search_indexing?(:destroy)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
SearchIndexBackend.remove(self.class.to_s, id)
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
collect data to index and send to backend
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.search_index_update_backend
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true # false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_update_backend
|
|
|
|
# fill up with search data
|
|
|
|
attributes = search_index_attribute_lookup
|
2017-09-11 00:50:05 +00:00
|
|
|
return true if !attributes
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
# update backend
|
|
|
|
SearchIndexBackend.add(self.class.to_s, attributes)
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get data to store in search index
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.search_index_data
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = {
|
|
|
|
attribute1: 'some value',
|
|
|
|
attribute2: ['value 1', 'value 2'],
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_data
|
|
|
|
attributes = {}
|
2017-11-23 08:09:44 +00:00
|
|
|
%w[name note].each do |key|
|
2017-01-31 17:13:45 +00:00
|
|
|
next if !self[key]
|
2017-09-11 00:50:05 +00:00
|
|
|
next if self[key].respond_to?('blank?') && self[key].blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
attributes[key] = self[key]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-09-11 00:50:05 +00:00
|
|
|
return true if attributes.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignore_search_indexing?(_action)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
serve method to ignore model attributes in search index
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
class Model < ApplicationModel
|
2017-05-02 15:21:13 +00:00
|
|
|
include HasSearchIndexBackend
|
2017-01-31 17:13:45 +00:00
|
|
|
search_index_attributes_ignored :password, :image
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_attributes_ignored(*attributes)
|
|
|
|
@search_index_attributes_ignored = attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
reload search index with full data
|
|
|
|
|
|
|
|
Model.search_index_reload
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_reload
|
2019-09-02 15:32:54 +00:00
|
|
|
tolerance = 10
|
2017-01-31 17:13:45 +00:00
|
|
|
tolerance_count = 0
|
2019-04-07 15:23:03 +00:00
|
|
|
ids = all.order(created_at: :desc).pluck(:id)
|
2017-10-01 12:25:52 +00:00
|
|
|
ids.each do |item_id|
|
2019-09-02 15:32:54 +00:00
|
|
|
item = find_by(id: item_id)
|
|
|
|
next if !item
|
2017-01-31 17:13:45 +00:00
|
|
|
next if item.ignore_search_indexing?(:destroy)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
begin
|
|
|
|
item.search_index_update_backend
|
|
|
|
rescue => e
|
2017-08-15 19:33:57 +00:00
|
|
|
logger.error "Unable to send #{item.class}.find(#{item.id}).search_index_update_backend backend: #{e.inspect}"
|
2017-01-31 17:13:45 +00:00
|
|
|
tolerance_count += 1
|
2019-09-02 15:32:54 +00:00
|
|
|
sleep 15
|
2017-08-15 19:33:57 +00:00
|
|
|
raise "Unable to send #{item.class}.find(#{item.id}).search_index_update_backend backend: #{e.inspect}" if tolerance_count == tolerance
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|