Implemented attachment support. Added rake script to rebuild all indexes once (rake searchindex:rebuild).
This commit is contained in:
parent
d902f78874
commit
f6e290e152
3 changed files with 119 additions and 18 deletions
|
@ -62,7 +62,6 @@ returns
|
||||||
file = Store.find( attachment.id )
|
file = Store.find( attachment.id )
|
||||||
data = {
|
data = {
|
||||||
"_name" => file.filename,
|
"_name" => file.filename,
|
||||||
# "_content_type" => file.preferences['Mime-Type'],
|
|
||||||
"content" => Base64.encode64( file.store_file.data )
|
"content" => Base64.encode64( file.store_file.data )
|
||||||
}
|
}
|
||||||
article_attributes['attachments'].push data
|
article_attributes['attachments'].push data
|
||||||
|
|
|
@ -4,6 +4,63 @@ class SearchIndexBackend
|
||||||
|
|
||||||
=begin
|
=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
|
add new object to search index
|
||||||
|
|
||||||
SearchIndexBackend.add( 'Ticket', some_data_object )
|
SearchIndexBackend.add( 'Ticket', some_data_object )
|
||||||
|
@ -79,41 +136,28 @@ return search result
|
||||||
data['sort'] =
|
data['sort'] =
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
'updated_at' => {
|
:updated_at => {
|
||||||
'order' => 'desc'
|
:order => 'desc'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"_score"
|
"_score"
|
||||||
]
|
]
|
||||||
|
|
||||||
data['query'] = query_extention || {}
|
data['query'] = query_extention || {}
|
||||||
if !data['query']['bool']
|
if !data['query']['bool']
|
||||||
data['query']['bool'] = {}
|
data['query']['bool'] = {}
|
||||||
end
|
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']
|
if !data['query']['bool']['must']
|
||||||
data['query']['bool']['must'] = []
|
data['query']['bool']['must'] = []
|
||||||
end
|
end
|
||||||
|
|
||||||
# if !data['query']['query_string']
|
# real search condition
|
||||||
# data['query']['query_string'] = {}
|
|
||||||
# end
|
|
||||||
condition = {
|
condition = {
|
||||||
'query_string' => {
|
'query_string' => {
|
||||||
'query' => query
|
'query' => query
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data['query']['bool']['must'].push condition
|
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}'"
|
puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'"
|
||||||
|
|
||||||
|
|
58
lib/tasks/search_index_es.rake
Normal file
58
lib/tasks/search_index_es.rake
Normal 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
|
Loading…
Reference in a new issue