2014-02-03 12:08:41 +00:00
|
|
|
$LOAD_PATH << './lib'
|
|
|
|
require 'rubygems'
|
|
|
|
|
|
|
|
namespace :searchindex do
|
2015-05-07 09:49:46 +00:00
|
|
|
task :drop, [:opts] => :environment do |_t, _args|
|
2014-02-03 12:08:41 +00:00
|
|
|
|
|
|
|
# drop indexes
|
2017-12-15 13:58:41 +00:00
|
|
|
print 'drop indexes...'
|
2014-02-03 12:08:41 +00:00
|
|
|
SearchIndexBackend.index(
|
2015-04-27 13:42:53 +00:00
|
|
|
action: 'delete',
|
2014-02-03 12:08:41 +00:00
|
|
|
)
|
2017-12-15 13:58:41 +00:00
|
|
|
puts 'done'
|
2014-02-03 12:08:41 +00:00
|
|
|
|
2017-12-15 13:58:41 +00:00
|
|
|
Rake::Task['searchindex:drop_pipeline'].execute
|
2014-02-03 12:08:41 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 09:49:46 +00:00
|
|
|
task :create, [:opts] => :environment do |_t, _args|
|
2017-12-15 13:58:41 +00:00
|
|
|
print 'create indexes...'
|
2014-02-03 12:08:41 +00:00
|
|
|
|
2017-12-04 00:24:58 +00:00
|
|
|
# es with mapper-attachments plugin
|
2017-12-04 01:12:55 +00:00
|
|
|
info = SearchIndexBackend.info
|
|
|
|
number = nil
|
|
|
|
if info.present?
|
|
|
|
number = info['version']['number'].to_s
|
|
|
|
end
|
2017-12-04 00:24:58 +00:00
|
|
|
|
2018-07-18 14:00:06 +00:00
|
|
|
mapping = {}
|
|
|
|
Models.searchable.each do |local_object|
|
|
|
|
mapping.merge!(get_mapping_properties_object(local_object))
|
|
|
|
end
|
2017-12-04 00:24:58 +00:00
|
|
|
|
2018-07-18 14:00:06 +00:00
|
|
|
# create indexes
|
|
|
|
SearchIndexBackend.index(
|
|
|
|
action: 'create',
|
|
|
|
data: {
|
|
|
|
mappings: mapping
|
|
|
|
}
|
|
|
|
)
|
2017-12-04 00:24:58 +00:00
|
|
|
|
2018-07-18 14:00:06 +00:00
|
|
|
if number.blank? || number =~ /^[2-4]\./ || number =~ /^5\.[0-5]\./
|
|
|
|
Setting.set('es_pipeline', '')
|
2017-12-15 13:58:41 +00:00
|
|
|
end
|
|
|
|
|
2018-07-18 14:00:06 +00:00
|
|
|
puts 'done'
|
|
|
|
|
2017-12-15 13:58:41 +00:00
|
|
|
Rake::Task['searchindex:create_pipeline'].execute
|
|
|
|
end
|
|
|
|
|
|
|
|
task :create_pipeline, [:opts] => :environment do |_t, _args|
|
|
|
|
|
|
|
|
# es with mapper-attachments plugin
|
|
|
|
info = SearchIndexBackend.info
|
|
|
|
number = nil
|
|
|
|
if info.present?
|
|
|
|
number = info['version']['number'].to_s
|
|
|
|
end
|
|
|
|
next if number.blank? || number =~ /^[2-4]\./ || number =~ /^5\.[0-5]\./
|
2017-12-04 00:24:58 +00:00
|
|
|
|
2017-12-15 13:58:41 +00:00
|
|
|
# update processors
|
|
|
|
pipeline = Setting.get('es_pipeline')
|
|
|
|
if pipeline.blank?
|
|
|
|
pipeline = "zammad#{rand(999_999_999_999)}"
|
2017-12-04 00:24:58 +00:00
|
|
|
Setting.set('es_pipeline', pipeline)
|
2017-12-15 13:58:41 +00:00
|
|
|
end
|
|
|
|
print 'create pipeline (pipeline)... '
|
|
|
|
SearchIndexBackend.processors(
|
|
|
|
"_ingest/pipeline/#{pipeline}": [
|
|
|
|
{
|
|
|
|
action: 'delete',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: 'create',
|
|
|
|
description: 'Extract zammad-attachment information from arrays',
|
|
|
|
processors: [
|
|
|
|
{
|
|
|
|
foreach: {
|
|
|
|
field: 'article',
|
|
|
|
ignore_failure: true,
|
|
|
|
processor: {
|
|
|
|
foreach: {
|
|
|
|
field: '_ingest._value.attachment',
|
|
|
|
ignore_failure: true,
|
|
|
|
processor: {
|
|
|
|
attachment: {
|
|
|
|
target_field: '_ingest._value',
|
|
|
|
field: '_ingest._value._content',
|
|
|
|
ignore_failure: true,
|
2017-12-04 00:24:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 13:58:41 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
puts 'done'
|
|
|
|
end
|
|
|
|
|
|
|
|
task :drop_pipeline, [:opts] => :environment do |_t, _args|
|
2014-02-03 12:08:41 +00:00
|
|
|
|
2017-12-15 13:58:41 +00:00
|
|
|
# es with mapper-attachments plugin
|
|
|
|
info = SearchIndexBackend.info
|
|
|
|
number = nil
|
|
|
|
if info.present?
|
|
|
|
number = info['version']['number'].to_s
|
|
|
|
end
|
|
|
|
next if number.blank? || number =~ /^[2-4]\./ || number =~ /^5\.[0-5]\./
|
|
|
|
|
|
|
|
# update processors
|
|
|
|
pipeline = Setting.get('es_pipeline')
|
|
|
|
next if pipeline.blank?
|
|
|
|
print 'delete pipeline (pipeline)... '
|
|
|
|
SearchIndexBackend.processors(
|
|
|
|
"_ingest/pipeline/#{pipeline}": [
|
|
|
|
{
|
|
|
|
action: 'delete',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
puts 'done'
|
2014-02-03 12:08:41 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 09:49:46 +00:00
|
|
|
task :reload, [:opts] => :environment do |_t, _args|
|
2014-02-03 12:08:41 +00:00
|
|
|
|
2015-04-27 13:20:16 +00:00
|
|
|
puts 'reload data...'
|
2017-10-01 12:31:29 +00:00
|
|
|
Models.searchable.each do |model_class|
|
2015-08-16 09:30:31 +00:00
|
|
|
puts " reload #{model_class}"
|
2015-08-16 09:33:16 +00:00
|
|
|
started_at = Time.zone.now
|
|
|
|
puts " - started at #{started_at}"
|
2015-08-16 09:30:31 +00:00
|
|
|
model_class.search_index_reload
|
2015-08-16 09:33:16 +00:00
|
|
|
took = Time.zone.now - started_at
|
|
|
|
puts " - took #{took.to_i} seconds"
|
2017-10-01 12:31:29 +00:00
|
|
|
end
|
2014-02-03 12:08:41 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-07 09:49:46 +00:00
|
|
|
task :rebuild, [:opts] => :environment do |_t, _args|
|
2014-02-03 12:08:41 +00:00
|
|
|
|
2015-04-27 13:20:16 +00:00
|
|
|
Rake::Task['searchindex:drop'].execute
|
|
|
|
Rake::Task['searchindex:create'].execute
|
|
|
|
Rake::Task['searchindex:reload'].execute
|
2014-02-03 12:08:41 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2018-07-18 14:00:06 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
This function will return a index mapping based on the
|
|
|
|
attributes of the database table of the existing object.
|
|
|
|
|
|
|
|
mapping = get_mapping_properties_object(Ticket)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
mapping = {
|
|
|
|
User: {
|
|
|
|
properties: {
|
|
|
|
firstname: {
|
|
|
|
type: 'keyword',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def get_mapping_properties_object(object)
|
|
|
|
result = {
|
|
|
|
object.name => {
|
|
|
|
properties: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
store_columns = %w[preferences data]
|
|
|
|
|
|
|
|
object.columns_hash.each do |key, value|
|
|
|
|
if value.type == :string && value.limit && value.limit <= 5000 && store_columns.exclude?(key)
|
|
|
|
result[object.name][:properties][key] = {
|
2018-07-22 14:58:57 +00:00
|
|
|
type: 'string',
|
2018-07-18 14:00:06 +00:00
|
|
|
fields: {
|
|
|
|
raw: { 'type': 'string', 'index': 'not_analyzed' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif value.type == :integer
|
|
|
|
result[object.name][:properties][key] = {
|
|
|
|
type: 'integer',
|
|
|
|
}
|
|
|
|
elsif value.type == :datetime
|
|
|
|
result[object.name][:properties][key] = {
|
|
|
|
type: 'date',
|
|
|
|
}
|
|
|
|
elsif value.type == :boolean
|
|
|
|
result[object.name][:properties][key] = {
|
|
|
|
type: 'boolean',
|
|
|
|
fields: {
|
|
|
|
raw: { 'type': 'boolean', 'index': 'not_analyzed' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif value.type == :binary
|
|
|
|
result[object.name][:properties][key] = {
|
|
|
|
type: 'binary',
|
|
|
|
}
|
|
|
|
elsif value.type == :bigint
|
|
|
|
result[object.name][:properties][key] = {
|
|
|
|
type: 'long',
|
|
|
|
}
|
|
|
|
elsif value.type == :decimal
|
|
|
|
result[object.name][:properties][key] = {
|
|
|
|
type: 'float',
|
|
|
|
}
|
|
|
|
elsif value.type == :date
|
|
|
|
result[object.name][:properties][key] = {
|
|
|
|
type: 'date',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# es with mapper-attachments plugin
|
|
|
|
info = SearchIndexBackend.info
|
|
|
|
number = nil
|
|
|
|
if info.present?
|
|
|
|
number = info['version']['number'].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
if object.name == 'Ticket'
|
|
|
|
|
|
|
|
result[object.name][:_source] = {
|
|
|
|
excludes: ['article.attachment']
|
|
|
|
}
|
|
|
|
|
|
|
|
if number.blank? || number =~ /^[2-4]\./ || number =~ /^5\.[0-5]\./
|
|
|
|
result[object.name][:_source] = {
|
|
|
|
excludes: ['article.attachment']
|
|
|
|
}
|
|
|
|
result[object.name][:properties][:article] = {
|
|
|
|
type: 'nested',
|
|
|
|
include_in_parent: true,
|
|
|
|
properties: {
|
|
|
|
attachment: {
|
|
|
|
type: 'attachment',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|