Implemented attachment support. Added rake script to rebuild all indexes once (rake searchindex:rebuild).

This commit is contained in:
Martin Edenhofer 2014-02-03 13:08:41 +01:00
parent d902f78874
commit f6e290e152
3 changed files with 119 additions and 18 deletions

View file

@ -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

View file

@ -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}'"

View file

@ -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