diff --git a/app/models/ticket/search_index.rb b/app/models/ticket/search_index.rb index ac55675aa..6e7267651 100644 --- a/app/models/ticket/search_index.rb +++ b/app/models/ticket/search_index.rb @@ -62,7 +62,6 @@ returns file = Store.find( attachment.id ) data = { "_name" => file.filename, -# "_content_type" => file.preferences['Mime-Type'], "content" => Base64.encode64( file.store_file.data ) } article_attributes['attachments'].push data diff --git a/lib/search_index_backend.rb b/lib/search_index_backend.rb index c716613b9..74bc5c623 100644 --- a/lib/search_index_backend.rb +++ b/lib/search_index_backend.rb @@ -4,6 +4,63 @@ class SearchIndexBackend =begin +create/update/delete index + + SearchIndexBackend.index( + :action => 'create', # create/update/delete + :data => { + :mappings => { + :Ticket => { + :properties => { + :articles_all => { + :type => 'nested', + :properties => { + 'attachments' => { :type => 'attachment' } + } + } + } + } + } + } + ) + + SearchIndexBackend.index( + :action => 'delete', # create/update/delete + :name => 'Ticket', # optional + ) + + SearchIndexBackend.index( + :action => 'delete', # create/update/delete + ) +=end + + def self.index(data) + + url = build_url( data[:name] ) + return if !url + + if data[:action] && data[:action] == 'delete' + return SearchIndexBackend.remove( data[:name] ) + end + + puts "# curl -X PUT \"#{url}\" -d '#{data[:data].to_json}'" + + conn = connection( url ) + response = conn.put do |req| + req.url url + req.headers['Content-Type'] = 'application/json' + req.body = data[:data].to_json + end +# puts response.body.to_s + puts "# #{response.status.to_s}" + return true if response.success? + data = JSON.parse( response.body ) + raise data.inspect + #return { :data => data, :response => response } + end + +=begin + add new object to search index SearchIndexBackend.add( 'Ticket', some_data_object ) @@ -79,41 +136,28 @@ return search result data['sort'] = [ { - 'updated_at' => { - 'order' => 'desc' + :updated_at => { + :order => 'desc' } }, "_score" ] + data['query'] = query_extention || {} if !data['query']['bool'] data['query']['bool'] = {} end -# data['query']['text_phrase'] = {} -# data['query']['text_phrase']['to'] = query -# data['query']['bool'] = {} -# data['query']['bool']['must'] = {} -# data['query']['bool']['must']['term'] = {} -# data['query']['bool']['must']['term']['title'] = '*z*' if !data['query']['bool']['must'] data['query']['bool']['must'] = [] end -# if !data['query']['query_string'] -# data['query']['query_string'] = {} -# end + # real search condition condition = { 'query_string' => { 'query' => query } } data['query']['bool']['must'].push condition -# data['query']['query_string']['query'] = query -#query_string":{"query":"10005 -# data['filtered'] = {} -# data['filtered']['query'] = {} -# data['filtered']['query']['match'] = {} -# data['filtered']['query']['match']['_all'] = query puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'" diff --git a/lib/tasks/search_index_es.rake b/lib/tasks/search_index_es.rake new file mode 100644 index 000000000..2cbc445f2 --- /dev/null +++ b/lib/tasks/search_index_es.rake @@ -0,0 +1,58 @@ +$LOAD_PATH << './lib' +require 'rubygems' + +namespace :searchindex do + task :drop, [:opts] => :environment do |t, args| + + # drop indexes + puts "drop indexes..." + SearchIndexBackend.index( + :action => 'delete', + ) + + end + + task :create, [:opts] => :environment do |t, args| + + # create indexes + puts "create indexes..." + SearchIndexBackend.index( + :action => 'create', + :data => { + :mappings => { + :Ticket => { + :_source => { :excludes => [ 'articles_all.attachments', 'articles_external.attachments' ] }, + :properties => { + :articles_all => { + :type => 'nested', + :properties => { + :attachments => { + :type => 'attachment', + } + } + } + } + } + } + } + ) + + end + + task :reload, [:opts] => :environment do |t, args| + + puts "create data..." + User.search_index_reload + Organization.search_index_reload + Ticket.search_index_reload + + end + + task :rebuild, [:opts] => :environment do |t, args| + + Rake::Task["searchindex:drop"].execute + Rake::Task["searchindex:create"].execute + Rake::Task["searchindex:reload"].execute + + end +end