123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
- 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.status.to_s}"
- return true if response.success?
- data = JSON.parse( response.body )
- raise data.inspect
- end
- =begin
- add new object to search index
- SearchIndexBackend.add( 'Ticket', some_data_object )
- =end
- def self.add(type, data)
- url = build_url( type, data['id'] )
- return if !url
- puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'"
- conn = connection( url )
- response = conn.post do |req|
- req.url url
- req.headers['Content-Type'] = 'application/json'
- req.body = data.to_json
- end
- puts "# #{response.status.to_s}"
- return true if response.success?
- data = JSON.parse( response.body )
- raise data.inspect
- end
- =begin
- remove whole data from index
- SearchIndexBackend.remove( 'Ticket', 123 )
- SearchIndexBackend.remove( 'Ticket' )
- =end
- def self.remove( type, o_id = nil )
- url = build_url( type, o_id )
- return if !url
- puts "# curl -X DELETE \"#{url}\""
- conn = connection( url )
- response = conn.delete( url )
- puts "# #{response.status.to_s}"
- return false if !response.success?
- data = JSON.parse( response.body )
- # raise data.inspect
- return { :data => data, :response => response }
- end
- =begin
- return search result
- result = SearchIndexBackend.search( 'search query', limit, ['User', 'Organization'] )
- result = SearchIndexBackend.search( 'search query', limit, 'User' )
- result = [
- {
- :id => 123,
- :type => 'User',
- },
- {
- :id => 125,
- :type => 'User',
- },
- {
- :id => 15,
- :type => 'Organization',
- }
- ]
- =end
- def self.search( query, limit = 10, index = nil, query_extention = {} )
- return [] if !query
- url = build_url()
- return if !url
- if index
- if index.class == Array
- url += "/#{index.join(',')}/_search"
- else
- url += "/#{index}/_search"
- end
- else
- url += '/_search'
- end
- data = {}
- data['from'] = 0
- data['size'] = 10
- data['sort'] =
- [
- {
- :updated_at => {
- :order => 'desc'
- }
- },
- "_score"
- ]
- data['query'] = query_extention || {}
- if !data['query']['bool']
- data['query']['bool'] = {}
- end
- if !data['query']['bool']['must']
- data['query']['bool']['must'] = []
- end
- # real search condition
- condition = {
- 'query_string' => {
- 'query' => query
- }
- }
- data['query']['bool']['must'].push condition
- puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'"
- conn = connection( url )
- response = conn.get do |req|
- req.headers['Content-Type'] = 'application/json'
- req.body = data.to_json
- end
- puts "# #{response.status.to_s}"
- data = JSON.parse( response.body )
- if !response.success?
- return []
- # raise data.inspect
- end
- ids = []
- return ids if !data
- return ids if !data['hits']
- return ids if !data['hits']['hits']
- data['hits']['hits'].each { |item|
- puts "... #{item['_type'].to_s} #{item['_id'].to_s}"
- data = {
- :id => item['_id'],
- :type => item['_type'],
- }
- ids.push data
- }
- ids
- end
- =begin
- return true if backend is configured
- result = SearchIndexBackend.enabled?
- =end
- def self.enabled?
- return if !Setting.get('es_url')
- return if Setting.get('es_url').empty?
- true
- end
- private
- def self.build_url( type = nil, o_id = nil )
- return if !SearchIndexBackend.enabled?
- index = Setting.get('es_index').to_s + "_#{Rails.env}"
- url = Setting.get('es_url')
- if type
- if o_id
- url = "#{url}/#{index}/#{type}/#{o_id}"
- else
- url = "#{url}/#{index}/#{type}"
- end
- else
- url = "#{url}/#{index}"
- 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
|