2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::SearchIndex
|
2014-01-27 22:59:41 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-01-30 00:06:42 +00:00
|
|
|
build and send data for search index to backend
|
2014-01-27 22:59:41 +00:00
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.search_index_update_backend
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true # false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def search_index_update_backend
|
|
|
|
return if !self.class.search_index_support_config
|
|
|
|
|
|
|
|
# default ignored attributes
|
2015-10-18 15:20:09 +00:00
|
|
|
ignore_attributes = {}
|
2015-04-27 23:19:26 +00:00
|
|
|
if self.class.search_index_support_config[:ignore_attributes]
|
|
|
|
self.class.search_index_support_config[:ignore_attributes].each {|key, value|
|
|
|
|
ignore_attributes[key] = value
|
2014-01-27 22:59:41 +00:00
|
|
|
}
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2014-02-05 20:53:21 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# for performance reasons, Model.search_index_reload will only collect if of object
|
|
|
|
# get whole data here
|
2015-05-07 12:10:38 +00:00
|
|
|
ticket = self.class.find(id)
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
# remove ignored attributes
|
|
|
|
attributes = ticket.attributes
|
|
|
|
ignore_attributes.each {|key, value|
|
|
|
|
next if value != true
|
|
|
|
attributes.delete( key.to_s )
|
|
|
|
}
|
|
|
|
|
|
|
|
# add tags
|
2015-05-07 12:10:38 +00:00
|
|
|
tags = Tag.tag_list( object: 'Ticket', o_id: id )
|
2015-04-27 23:19:26 +00:00
|
|
|
if tags && !tags.empty?
|
|
|
|
attributes[:tag] = tags
|
|
|
|
end
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# lookup attributes of ref. objects (normally name and note)
|
|
|
|
attributes = search_index_attribute_lookup( attributes, ticket )
|
2015-04-06 19:00:16 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# list ignored file extentions
|
|
|
|
attachments_ignore = Setting.get('es_attachment_ignore') || [ '.png', '.jpg', '.jpeg', '.mpeg', '.mpg', '.mov', '.bin', '.exe' ]
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# max attachment size
|
|
|
|
attachment_max_size_in_mb = Setting.get('es_attachment_max_size_in_mb') || 40
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# collect article data
|
2015-05-07 12:10:38 +00:00
|
|
|
articles = Ticket::Article.where( ticket_id: id )
|
2015-04-27 23:19:26 +00:00
|
|
|
attributes['articles'] = []
|
|
|
|
articles.each {|article|
|
|
|
|
article_attributes = article.attributes
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# remove note needed attributes
|
2015-10-18 15:20:09 +00:00
|
|
|
ignore = %w(message_id_md5)
|
2015-04-27 23:19:26 +00:00
|
|
|
ignore.each {|attribute|
|
|
|
|
article_attributes.delete( attribute )
|
|
|
|
}
|
2014-02-02 18:58:59 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# lookup attributes of ref. objects (normally name and note)
|
|
|
|
article_attributes = search_index_attribute_lookup( article_attributes, article )
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# index raw text body
|
|
|
|
if article_attributes['content_type'] && article_attributes['content_type'] == 'text/html' && article_attributes['body']
|
|
|
|
article_attributes['body'] = article_attributes['body'].html2text
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# lookup attachments
|
|
|
|
article.attachments.each {|attachment|
|
|
|
|
if !article_attributes['attachments']
|
|
|
|
article_attributes['attachments'] = []
|
2015-04-05 23:28:52 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# check file size
|
2015-05-07 09:04:40 +00:00
|
|
|
next if !attachment.content
|
|
|
|
next if attachment.content.size / 1024 > attachment_max_size_in_mb * 1024
|
|
|
|
|
|
|
|
# check ignored files
|
|
|
|
next if !attachment.filename
|
|
|
|
|
|
|
|
filename_extention = attachment.filename.downcase
|
|
|
|
filename_extention.gsub!(/^.*(\..+?)$/, '\\1')
|
|
|
|
|
|
|
|
next if attachments_ignore.include?( filename_extention.downcase )
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'_name' => attachment.filename,
|
|
|
|
'_content' => Base64.encode64( attachment.content )
|
|
|
|
}
|
|
|
|
article_attributes['attachments'].push data
|
2014-02-02 18:58:59 +00:00
|
|
|
}
|
2015-04-27 23:19:26 +00:00
|
|
|
attributes['articles'].push article_attributes
|
|
|
|
}
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
return if !attributes
|
|
|
|
SearchIndexBackend.add(self.class.to_s, attributes)
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|