search_index.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module Ticket::SearchIndex
  3. =begin
  4. build and send data for search index 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. attributes = self.attributes
  24. ignore_attributes.each {|key, value|
  25. next if value != true
  26. attributes.delete( key.to_s )
  27. }
  28. # add tags
  29. tags = Tag.tag_list( :object=> 'Ticket', :o_id => self.id )
  30. if tags && !tags.empty?
  31. attributes[:tag] = tags
  32. end
  33. attributes = search_index_attribute_lookup( attributes, self )
  34. # add article data
  35. articles = Ticket::Article.where( :ticket_id => self.id )
  36. attributes['articles_all'] = []
  37. attributes['articles_external'] = []
  38. articles.each {|article|
  39. article_attributes = article.attributes
  40. article_attributes.delete('created_by_id')
  41. article_attributes.delete('updated_by_id')
  42. article_attributes.delete('updated_at')
  43. article_attributes.delete('references')
  44. article_attributes.delete('message_id_md5')
  45. article_attributes.delete('message_id')
  46. article_attributes.delete('in_reply_to')
  47. article_attributes.delete('ticket_id')
  48. article_attributes = search_index_attribute_lookup( article_attributes, article )
  49. # lookup attachments
  50. article.attachments.each {|attachment|
  51. if !article_attributes['attachments']
  52. article_attributes['attachments'] = []
  53. end
  54. data = {
  55. "_name" => attachment.filename,
  56. "content" => Base64.encode64( attachment.content )
  57. }
  58. article_attributes['attachments'].push data
  59. }
  60. attributes['articles_all'].push article_attributes
  61. if !article.internal
  62. attributes['articles_external'].push article_attributes
  63. end
  64. }
  65. return if !attributes
  66. SearchIndexBackend.add(self.class.to_s, attributes)
  67. end
  68. end