search_index_base.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. # rubocop:disable ClassAndModuleChildren
  3. module ApplicationModel::SearchIndexBase
  4. =begin
  5. collect data to index and send to backend
  6. ticket = Ticket.find(123)
  7. result = ticket.search_index_update_backend
  8. returns
  9. result = true # false
  10. =end
  11. def search_index_update_backend
  12. return if !self.class.search_index_support_config
  13. # default ignored attributes
  14. ignore_attributes = {
  15. created_by_id: true,
  16. updated_by_id: true,
  17. active: true,
  18. }
  19. if self.class.search_index_support_config[:ignore_attributes]
  20. self.class.search_index_support_config[:ignore_attributes].each {|key, value|
  21. ignore_attributes[key] = value
  22. }
  23. end
  24. # for performance reasons, Model.search_index_reload will only collect if of object
  25. # get whole data here
  26. data = self.class.find(id)
  27. # remove ignored attributes
  28. attributes = data.attributes
  29. ignore_attributes.each {|key, value|
  30. next if value != true
  31. attributes.delete( key.to_s )
  32. }
  33. # fill up with search data
  34. attributes = search_index_attribute_lookup(attributes, data)
  35. return if !attributes
  36. # update backend
  37. if self.class.column_names.include? 'active'
  38. if active
  39. SearchIndexBackend.add( self.class.to_s, attributes )
  40. else
  41. SearchIndexBackend.remove( self.class.to_s, id )
  42. end
  43. else
  44. SearchIndexBackend.add( self.class.to_s, attributes )
  45. end
  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 = true # false
  53. =end
  54. def search_index_data
  55. attributes = {}
  56. %w(name note).each { |key|
  57. if self[key] && !self[key].empty?
  58. attributes[key] = self[key]
  59. end
  60. }
  61. return if attributes.empty?
  62. attributes
  63. end
  64. private
  65. =begin
  66. lookup name of ref. objects
  67. attributes = search_index_attribute_lookup(attributes, Ticket)
  68. returns
  69. attributes # object with lookup data
  70. =end
  71. def search_index_attribute_lookup(attributes, ref_object)
  72. # default keep attributes
  73. keep_attributes = {}
  74. if self.class.search_index_support_config[:keep_attributes]
  75. self.class.search_index_support_config[:keep_attributes].each {|key, value|
  76. keep_attributes[key] = value
  77. }
  78. end
  79. attributes_new = {}
  80. attributes.each {|key, value|
  81. next if !value
  82. # get attribute name
  83. attribute_name_with_id = key.to_s
  84. attribute_name = key.to_s
  85. next if attribute_name[-3, 3] != '_id'
  86. attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
  87. # check if attribute method exists
  88. next if !ref_object.respond_to?( attribute_name )
  89. # check if method has own class
  90. relation_class = ref_object.send(attribute_name).class
  91. next if !relation_class
  92. # lookup ref object
  93. relation_model = relation_class.lookup( id: value )
  94. next if !relation_model
  95. # get name of ref object
  96. value = nil
  97. if relation_model.respond_to?('search_index_data')
  98. value = relation_model.send('search_index_data')
  99. end
  100. next if !value
  101. # save name of ref object
  102. attributes_new[ attribute_name ] = value
  103. if !keep_attributes[ attribute_name_with_id.to_sym ]
  104. attributes.delete(key)
  105. end
  106. }
  107. attributes_new.merge(attributes)
  108. end
  109. end