search_index_backend.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. class SearchIndexBackend
  3. =begin
  4. add new object to search index
  5. SearchIndexBackend.add( 'Ticket', some_data_object )
  6. =end
  7. def self.add(type, data)
  8. url = build_url( type, data['id'] )
  9. return if !url
  10. puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'"
  11. conn = connection( url )
  12. response = conn.post do |req|
  13. req.url url
  14. req.headers['Content-Type'] = 'application/json'
  15. req.body = data.to_json
  16. end
  17. # puts response.body.to_s
  18. puts "# #{response.status.to_s}"
  19. return true if response.success?
  20. data = JSON.parse( response.body )
  21. return { :data => data, :response => response }
  22. end
  23. =begin
  24. remove whole data from index
  25. SearchIndexBackend.remove( 'Ticket', 123 )
  26. SearchIndexBackend.remove( 'Ticket' )
  27. =end
  28. def self.remove( type, o_id = nil )
  29. url = build_url( type, o_id )
  30. return if !url
  31. puts "# curl -X DELETE \"#{url}\""
  32. conn = connection( url )
  33. response = conn.delete( url )
  34. # puts response.body.to_s
  35. puts "# #{response.status.to_s}"
  36. return true if response.success?
  37. data = JSON.parse( response.body )
  38. return { :data => data, :response => response }
  39. end
  40. =begin
  41. return all activity entries of an user
  42. result = SearchIndexBackend.search( user )
  43. =end
  44. def self.search(user,limit)
  45. end
  46. private
  47. def self.build_url( type, o_id = nil )
  48. index = Setting.get('es_index').to_s + "_#{Rails.env}"
  49. url = Setting.get('es_url')
  50. return if !url
  51. if o_id
  52. url = "#{url}/#{index}/#{type}/#{o_id}"
  53. else
  54. url = "#{url}/#{index}/#{type}"
  55. end
  56. url
  57. end
  58. def self.connection( url )
  59. conn = Faraday.new( :url => url )
  60. user = Setting.get('es_user')
  61. pw = Setting.get('es_password')
  62. if user && !user.empty? && pw && !pw.empty?
  63. conn.basic_auth( user, pw )
  64. end
  65. conn
  66. end
  67. end