search_index_base.rb 3.3 KB

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