2014-01-27 22:59:41 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class SearchIndexBackend
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-02-03 12:08:41 +00:00
|
|
|
create/update/delete index
|
|
|
|
|
|
|
|
SearchIndexBackend.index(
|
|
|
|
:action => 'create', # create/update/delete
|
|
|
|
:data => {
|
|
|
|
:mappings => {
|
|
|
|
:Ticket => {
|
|
|
|
:properties => {
|
2015-04-06 19:00:16 +00:00
|
|
|
:articles => {
|
2014-02-03 12:08:41 +00:00
|
|
|
:type => 'nested',
|
|
|
|
:properties => {
|
|
|
|
'attachments' => { :type => 'attachment' }
|
2014-04-28 15:30:06 +00:00
|
|
|
}
|
2014-02-03 12:08:41 +00:00
|
|
|
}
|
2014-04-28 15:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 12:08:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-05-04 18:58:28 +00:00
|
|
|
Rails.logger.info "# curl -X PUT \"#{url}\" \\"
|
|
|
|
#Rails.logger.info "-d '#{data[:data].to_json}'"
|
2014-02-03 12:08:41 +00:00
|
|
|
|
2015-03-23 00:31:30 +00:00
|
|
|
response = UserAgent.put(
|
|
|
|
url,
|
|
|
|
data[:data],
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
json: true,
|
|
|
|
open_timeout: 5,
|
|
|
|
read_timeout: 20,
|
|
|
|
user: Setting.get('es_user'),
|
|
|
|
password: Setting.get('es_password'),
|
2015-03-23 00:31:30 +00:00
|
|
|
}
|
|
|
|
)
|
2015-05-05 08:26:53 +00:00
|
|
|
Rails.logger.info "# #{response.code}"
|
2014-02-03 12:08:41 +00:00
|
|
|
return true if response.success?
|
2015-04-05 11:36:11 +00:00
|
|
|
raise response.inspect
|
2014-02-03 12:08:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
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
|
|
|
|
2015-05-04 18:58:28 +00:00
|
|
|
Rails.logger.info "# curl -X POST \"#{url}\" \\"
|
|
|
|
#Rails.logger.info "-d '#{data.to_json}'"
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2015-03-23 00:31:30 +00:00
|
|
|
response = UserAgent.post(
|
|
|
|
url,
|
|
|
|
data,
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
json: true,
|
|
|
|
open_timeout: 5,
|
|
|
|
read_timeout: 20,
|
|
|
|
user: Setting.get('es_user'),
|
|
|
|
password: Setting.get('es_password'),
|
2015-03-23 00:31:30 +00:00
|
|
|
}
|
|
|
|
)
|
2015-05-06 09:30:39 +00:00
|
|
|
Rails.logger.info "# #{response.code}"
|
2014-01-27 22:59:41 +00:00
|
|
|
return true if response.success?
|
2015-04-05 11:36:11 +00:00
|
|
|
raise response.inspect
|
2014-01-27 22:59:41 +00:00
|
|
|
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
|
|
|
|
2015-05-05 08:26:53 +00:00
|
|
|
Rails.logger.info "# curl -X DELETE \"#{url}\""
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2015-03-23 00:31:30 +00:00
|
|
|
response = UserAgent.delete(
|
|
|
|
url,
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
open_timeout: 5,
|
|
|
|
read_timeout: 14,
|
|
|
|
user: Setting.get('es_user'),
|
|
|
|
password: Setting.get('es_password'),
|
2015-03-23 00:31:30 +00:00
|
|
|
}
|
|
|
|
)
|
2015-05-04 18:58:28 +00:00
|
|
|
#Rails.logger.info "# #{response.code.to_s}"
|
2015-03-23 00:31:30 +00:00
|
|
|
return true if response.success?
|
2015-05-04 18:58:28 +00:00
|
|
|
#Rails.logger.info "NOTICE: can't drop index: " + response.inspect
|
2015-04-06 19:00:16 +00:00
|
|
|
false
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-01-29 23:51:55 +00:00
|
|
|
return search result
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2014-09-19 21:35:40 +00:00
|
|
|
result = SearchIndexBackend.search( 'search query', limit, ['User', 'Organization'] )
|
|
|
|
|
2014-01-29 23:51:55 +00:00
|
|
|
result = SearchIndexBackend.search( 'search query', limit, 'User' )
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2014-09-19 21:35:40 +00:00
|
|
|
result = [
|
|
|
|
{
|
|
|
|
:id => 123,
|
|
|
|
:type => 'User',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:id => 125,
|
|
|
|
:type => 'User',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:id => 15,
|
|
|
|
:type => 'Organization',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
=end
|
|
|
|
|
2015-05-07 08:01:35 +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
|
2014-04-28 15:30:06 +00:00
|
|
|
if index
|
2014-09-19 21:35:40 +00:00
|
|
|
if index.class == Array
|
|
|
|
url += "/#{index.join(',')}/_search"
|
|
|
|
else
|
|
|
|
url += "/#{index}/_search"
|
|
|
|
end
|
2014-01-29 23:51:55 +00:00
|
|
|
else
|
|
|
|
url += '/_search'
|
|
|
|
end
|
|
|
|
data = {}
|
|
|
|
data['from'] = 0
|
|
|
|
data['size'] = 10
|
2014-04-28 15:30:06 +00:00
|
|
|
data['sort'] =
|
2014-02-02 18:58:31 +00:00
|
|
|
[
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
updated_at: {
|
|
|
|
order: 'desc'
|
2014-02-02 18:58:31 +00:00
|
|
|
}
|
|
|
|
},
|
2015-04-27 13:20:16 +00:00
|
|
|
'_score'
|
2014-02-02 18:58:31 +00:00
|
|
|
]
|
2014-02-03 12:08:41 +00:00
|
|
|
|
2014-02-02 18:58:31 +00:00
|
|
|
data['query'] = query_extention || {}
|
|
|
|
if !data['query']['bool']
|
|
|
|
data['query']['bool'] = {}
|
|
|
|
end
|
|
|
|
if !data['query']['bool']['must']
|
|
|
|
data['query']['bool']['must'] = []
|
|
|
|
end
|
2014-01-29 23:51:55 +00:00
|
|
|
|
2014-02-03 12:08:41 +00:00
|
|
|
# real search condition
|
2014-02-02 18:58:31 +00:00
|
|
|
condition = {
|
|
|
|
'query_string' => {
|
|
|
|
'query' => query
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data['query']['bool']['must'].push condition
|
2014-01-29 23:51:55 +00:00
|
|
|
|
2015-05-04 18:58:28 +00:00
|
|
|
Rails.logger.info "# curl -X POST \"#{url}\" \\"
|
|
|
|
#Rails.logger.info " -d'#{data.to_json}'"
|
2014-01-29 23:51:55 +00:00
|
|
|
|
2015-03-23 00:31:30 +00:00
|
|
|
response = UserAgent.get(
|
|
|
|
url,
|
|
|
|
data,
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
json: true,
|
|
|
|
open_timeout: 5,
|
|
|
|
read_timeout: 14,
|
|
|
|
user: Setting.get('es_user'),
|
|
|
|
password: Setting.get('es_password'),
|
2015-03-23 00:31:30 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-06 09:30:39 +00:00
|
|
|
Rails.logger.info "# #{response.code}"
|
2014-02-03 18:26:22 +00:00
|
|
|
if !response.success?
|
2015-05-05 08:26:53 +00:00
|
|
|
Rails.logger.error "ERROR: #{response.inspect}"
|
2014-02-03 19:40:42 +00:00
|
|
|
return []
|
2014-02-03 18:26:22 +00:00
|
|
|
end
|
2015-03-23 00:31:30 +00:00
|
|
|
data = response.data
|
2014-02-03 18:26:22 +00:00
|
|
|
|
2014-01-29 23:51:55 +00:00
|
|
|
ids = []
|
|
|
|
return ids if !data
|
|
|
|
return ids if !data['hits']
|
|
|
|
return ids if !data['hits']['hits']
|
|
|
|
data['hits']['hits'].each { |item|
|
2015-05-06 09:30:39 +00:00
|
|
|
Rails.logger.info "... #{item['_type']} #{item['_id']}"
|
2014-09-19 21:35:40 +00:00
|
|
|
data = {
|
2015-04-27 13:42:53 +00:00
|
|
|
id: item['_id'],
|
|
|
|
type: item['_type'],
|
2014-09-19 21:35:40 +00:00
|
|
|
}
|
|
|
|
ids.push data
|
2014-01-29 23:51:55 +00:00
|
|
|
}
|
2014-02-03 18:26:22 +00:00
|
|
|
ids
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
|
|
|
|
2014-02-02 18:58:31 +00:00
|
|
|
=begin
|
|
|
|
|
2014-04-28 15:30:06 +00:00
|
|
|
return true if backend is configured
|
2014-02-02 18:58:31 +00:00
|
|
|
|
|
|
|
result = SearchIndexBackend.enabled?
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.enabled?
|
|
|
|
return if !Setting.get('es_url')
|
|
|
|
return if Setting.get('es_url').empty?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|