diff --git a/db/migrate/20140128000001_add_search_index.rb b/db/migrate/20140128000001_add_search_index.rb new file mode 100644 index 000000000..ad3521125 --- /dev/null +++ b/db/migrate/20140128000001_add_search_index.rb @@ -0,0 +1,47 @@ +class AddSearchIndex < ActiveRecord::Migration + def up + Setting.create_or_update( + :title => 'Elastic Search Endpoint URL', + :name => 'es_url', + :area => 'SearchIndex::ElasticSearch', + :description => 'Define endpoint of Elastic Search.', + :state => '', + :frontend => false + ) + Setting.create_or_update( + :title => 'Elastic Search Endpoint User', + :name => 'es_user', + :area => 'SearchIndex::ElasticSearch', + :description => 'Define http basic auth user of Elastic Search.', + :state => '', + :frontend => false + ) + Setting.create_or_update( + :title => 'Elastic Search Endpoint Password', + :name => 'es_password', + :area => 'SearchIndex::ElasticSearch', + :description => 'Define http basic auth password of Elastic Search.', + :state => '', + :frontend => false + ) + Setting.create_or_update( + :title => 'Elastic Search Endpoint Index', + :name => 'es_index', + :area => 'SearchIndex::ElasticSearch', + :description => 'Define Elastic Search index name.', + :state => 'zammad', + :frontend => false + ) + + Setting.set('es_url', 'http://217.111.80.181') + Setting.set('es_user', 'elasticsearch') + Setting.set('es_password', 'zammad') + Setting.set('es_index', Socket.gethostname + 'zammad') + + Ticket.search_index_reload + User.search_index_reload + Organization.search_index_reload + end + def down + end +end diff --git a/lib/search_index_backend.rb b/lib/search_index_backend.rb index 28f201778..50ae91320 100644 --- a/lib/search_index_backend.rb +++ b/lib/search_index_backend.rb @@ -1,10 +1,6 @@ # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ class SearchIndexBackend - @@index = "zammad_#{Rails.env}" - @@url = 'http://217.111.80.181' - @@user = 'elasticsearch' - @@pw = 'zammad' =begin @@ -16,15 +12,12 @@ add new object to search index def self.add(type, data) - url = "#{@@url}/#{@@index}/#{type}/#{data[:id]}" + url = build_url( type, data['id'] ) + return if !url puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'" - conn = Faraday.new( :url => url ) - if @@user && @@pw - conn.basic_auth( @@user, @@pw ) - end - + conn = connection( url ) response = conn.post do |req| req.url url req.headers['Content-Type'] = 'application/json' @@ -48,19 +41,13 @@ remove whole data from index =end def self.remove( type, o_id = nil ) - if o_id - url = "#{@@url}/#{@@index}/#{type}/#{o_id}" - else - url = "#{@@url}/#{@@index}/#{type}" - end + url = build_url( type, o_id ) + return if !url puts "# curl -X DELETE \"#{url}\"" - conn = Faraday.new( :url => url ) - if @@user && @@pw - conn.basic_auth( @@user, @@pw ) - end - response = conn.delete url + conn = connection( url ) + response = conn.delete( url ) # puts response.body.to_s puts "# #{response.status.to_s}" return true if response.success? @@ -79,4 +66,27 @@ return all activity entries of an user def self.search(user,limit) end + private + + def self.build_url( type, o_id = nil ) + index = Setting.get('es_index') + "_#{Rails.env}" + url = Setting.get('es_url') + return if !url + if o_id + url = "#{url}/#{index}/#{type}/#{o_id}" + else + url = "#{url}/#{index}/#{type}" + end + url + end + + def self.connection( url ) + conn = Faraday.new( :url => url ) + user = Setting.get('es_user') + pw = Setting.get('es_password') + if user && !user.empty? && pw && !pw.empty? + conn.basic_auth( user, pw ) + end + conn + end end \ No newline at end of file