search_index.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. # rubocop:disable ClassAndModuleChildren
  3. module Ticket::SearchIndex
  4. =begin
  5. build and send data for search index 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. ticket = self.class.find(id)
  27. # remove ignored attributes
  28. attributes = ticket.attributes
  29. ignore_attributes.each {|key, value|
  30. next if value != true
  31. attributes.delete( key.to_s )
  32. }
  33. # add tags
  34. tags = Tag.tag_list( object: 'Ticket', o_id: id )
  35. if tags && !tags.empty?
  36. attributes[:tag] = tags
  37. end
  38. # lookup attributes of ref. objects (normally name and note)
  39. attributes = search_index_attribute_lookup( attributes, ticket )
  40. # list ignored file extentions
  41. attachments_ignore = Setting.get('es_attachment_ignore') || [ '.png', '.jpg', '.jpeg', '.mpeg', '.mpg', '.mov', '.bin', '.exe' ]
  42. # max attachment size
  43. attachment_max_size_in_mb = Setting.get('es_attachment_max_size_in_mb') || 40
  44. # collect article data
  45. articles = Ticket::Article.where( ticket_id: id )
  46. attributes['articles'] = []
  47. articles.each {|article|
  48. article_attributes = article.attributes
  49. # remove note needed attributes
  50. ignore = %w(created_by_id updated_by_id updated_at references message_id_md5 message_id in_reply_to ticket_id)
  51. ignore.each {|attribute|
  52. article_attributes.delete( attribute )
  53. }
  54. # lookup attributes of ref. objects (normally name and note)
  55. article_attributes = search_index_attribute_lookup( article_attributes, article )
  56. # index raw text body
  57. if article_attributes['content_type'] && article_attributes['content_type'] == 'text/html' && article_attributes['body']
  58. article_attributes['body'] = article_attributes['body'].html2text
  59. end
  60. # lookup attachments
  61. article.attachments.each {|attachment|
  62. if !article_attributes['attachments']
  63. article_attributes['attachments'] = []
  64. end
  65. # check file size
  66. next if !attachment.content
  67. next if attachment.content.size / 1024 > attachment_max_size_in_mb * 1024
  68. # check ignored files
  69. next if !attachment.filename
  70. filename_extention = attachment.filename.downcase
  71. filename_extention.gsub!(/^.*(\..+?)$/, '\\1')
  72. next if attachments_ignore.include?( filename_extention.downcase )
  73. data = {
  74. '_name' => attachment.filename,
  75. '_content' => Base64.encode64( attachment.content )
  76. }
  77. article_attributes['attachments'].push data
  78. }
  79. attributes['articles'].push article_attributes
  80. }
  81. return if !attributes
  82. SearchIndexBackend.add(self.class.to_s, attributes)
  83. end
  84. end