2014-01-27 22:59:41 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class SearchIndexBackend
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
add new object to search index
|
|
|
|
|
|
|
|
SearchIndexBackend.add( 'Ticket', some_data_object )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.add(type, data)
|
|
|
|
|
2014-01-28 09:58:49 +00:00
|
|
|
url = build_url( type, data['id'] )
|
|
|
|
return if !url
|
2014-01-27 22:59:41 +00:00
|
|
|
|
|
|
|
puts "# curl -X POST \"#{url}\" -d '#{data.to_json}'"
|
|
|
|
|
2014-01-28 09:58:49 +00:00
|
|
|
conn = connection( url )
|
2014-01-27 22:59:41 +00:00
|
|
|
response = conn.post do |req|
|
|
|
|
req.url url
|
|
|
|
req.headers['Content-Type'] = 'application/json'
|
|
|
|
req.body = data.to_json
|
|
|
|
end
|
|
|
|
# puts response.body.to_s
|
|
|
|
puts "# #{response.status.to_s}"
|
|
|
|
return true if response.success?
|
|
|
|
data = JSON.parse( response.body )
|
|
|
|
return { :data => data, :response => response }
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
remove whole data from index
|
|
|
|
|
|
|
|
SearchIndexBackend.remove( 'Ticket', 123 )
|
|
|
|
|
|
|
|
SearchIndexBackend.remove( 'Ticket' )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove( type, o_id = nil )
|
2014-01-28 09:58:49 +00:00
|
|
|
url = build_url( type, o_id )
|
|
|
|
return if !url
|
2014-01-27 22:59:41 +00:00
|
|
|
|
|
|
|
puts "# curl -X DELETE \"#{url}\""
|
|
|
|
|
2014-01-28 09:58:49 +00:00
|
|
|
conn = connection( url )
|
|
|
|
response = conn.delete( url )
|
2014-01-27 22:59:41 +00:00
|
|
|
# puts response.body.to_s
|
|
|
|
puts "# #{response.status.to_s}"
|
|
|
|
return true if response.success?
|
|
|
|
data = JSON.parse( response.body )
|
|
|
|
return { :data => data, :response => response }
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-01-29 23:51:55 +00:00
|
|
|
return search result
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2014-01-29 23:51:55 +00:00
|
|
|
result = SearchIndexBackend.search( 'search query', limit, 'User' )
|
2014-01-27 22:59:41 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2014-02-02 18:58:31 +00:00
|
|
|
def self.search( query, limit = 10, index = nil, query_extention = {} )
|
2014-01-29 23:51:55 +00:00
|
|
|
return [] if !query
|
|
|
|
|
|
|
|
url = build_url()
|
|
|
|
return if !url
|
|
|
|
if index
|
|
|
|
url += "/#{index}/_search"
|
|
|
|
else
|
|
|
|
url += '/_search'
|
|
|
|
end
|
|
|
|
data = {}
|
|
|
|
data['from'] = 0
|
|
|
|
data['size'] = 10
|
2014-02-02 18:58:31 +00:00
|
|
|
data['sort'] =
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'updated_at' => {
|
|
|
|
'order' => 'desc'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"_score"
|
|
|
|
]
|
|
|
|
data['query'] = query_extention || {}
|
|
|
|
if !data['query']['bool']
|
|
|
|
data['query']['bool'] = {}
|
|
|
|
end
|
2014-01-29 23:51:55 +00:00
|
|
|
# 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*'
|
2014-02-02 18:58:31 +00:00
|
|
|
if !data['query']['bool']['must']
|
|
|
|
data['query']['bool']['must'] = []
|
|
|
|
end
|
2014-01-29 23:51:55 +00:00
|
|
|
|
2014-02-02 18:58:31 +00:00
|
|
|
# if !data['query']['query_string']
|
|
|
|
# data['query']['query_string'] = {}
|
|
|
|
# end
|
|
|
|
condition = {
|
|
|
|
'query_string' => {
|
|
|
|
'query' => query
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data['query']['bool']['must'].push condition
|
|
|
|
# data['query']['query_string']['query'] = query
|
|
|
|
#query_string":{"query":"10005
|
2014-01-29 23:51:55 +00:00
|
|
|
# data['filtered'] = {}
|
|
|
|
# data['filtered']['query'] = {}
|
|
|
|
# data['filtered']['query']['match'] = {}
|
|
|
|
# data['filtered']['query']['match']['_all'] = query
|
|
|
|
|
|
|
|
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.body.to_s
|
|
|
|
puts "# #{response.status.to_s}"
|
|
|
|
puts response.body
|
|
|
|
# return true if response.success?
|
|
|
|
data = JSON.parse( response.body )
|
|
|
|
# return { :data => data, :response => response }
|
|
|
|
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}"
|
|
|
|
ids.push item['_id']
|
|
|
|
}
|
|
|
|
return ids
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
|
|
|
|
2014-02-02 18:58:31 +00:00
|
|
|
=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
|
|
|
|
|
|
|
|
|
2014-01-28 09:58:49 +00:00
|
|
|
private
|
|
|
|
|
2014-01-29 23:51:55 +00:00
|
|
|
def self.build_url( type = nil, o_id = nil )
|
2014-02-02 18:58:31 +00:00
|
|
|
return if !SearchIndexBackend.enabled?
|
2014-01-28 10:02:31 +00:00
|
|
|
index = Setting.get('es_index').to_s + "_#{Rails.env}"
|
2014-01-28 09:58:49 +00:00
|
|
|
url = Setting.get('es_url')
|
2014-01-29 23:51:55 +00:00
|
|
|
if type
|
|
|
|
if o_id
|
|
|
|
url = "#{url}/#{index}/#{type}/#{o_id}"
|
|
|
|
else
|
|
|
|
url = "#{url}/#{index}/#{type}"
|
|
|
|
end
|
2014-01-28 09:58:49 +00:00
|
|
|
else
|
2014-01-29 23:51:55 +00:00
|
|
|
url = "#{url}/#{index}"
|
2014-01-28 09:58:49 +00:00
|
|
|
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
|
2014-01-29 23:51:55 +00:00
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|