search_index_backend.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class SearchIndexBackend
  3. =begin
  4. info about used search index machine
  5. SearchIndexBackend.info
  6. =end
  7. def self.info
  8. url = Setting.get('es_url').to_s
  9. Rails.logger.info "# curl -X GET \"#{url}\""
  10. response = UserAgent.get(
  11. url,
  12. {},
  13. {
  14. json: true,
  15. open_timeout: 8,
  16. read_timeout: 12,
  17. user: Setting.get('es_user'),
  18. password: Setting.get('es_password'),
  19. }
  20. )
  21. Rails.logger.info "# #{response.code}"
  22. raise "Unable to process GET at #{url}\n#{response.inspect}" if !response.success?
  23. response.data
  24. end
  25. =begin
  26. update processors
  27. SearchIndexBackend.processors(
  28. _ingest/pipeline/attachment: {
  29. description: 'Extract attachment information from arrays',
  30. processors: [
  31. {
  32. foreach: {
  33. field: 'ticket.articles.attachments',
  34. processor: {
  35. attachment: {
  36. target_field: '_ingest._value.attachment',
  37. field: '_ingest._value.data'
  38. }
  39. }
  40. }
  41. }
  42. ]
  43. }
  44. )
  45. =end
  46. def self.processors(data)
  47. data.each do |key, items|
  48. url = "#{Setting.get('es_url')}/#{key}"
  49. items.each do |item|
  50. if item[:action] == 'delete'
  51. Rails.logger.info "# curl -X DELETE \"#{url}\""
  52. response = UserAgent.delete(
  53. url,
  54. {
  55. json: true,
  56. open_timeout: 8,
  57. read_timeout: 12,
  58. user: Setting.get('es_user'),
  59. password: Setting.get('es_password'),
  60. }
  61. )
  62. Rails.logger.info "# #{response.code}"
  63. next if response.success?
  64. raise "Unable to process DELETE at #{url}\n#{response.inspect}"
  65. end
  66. Rails.logger.info "# curl -X PUT \"#{url}\" \\"
  67. Rails.logger.debug "-d '#{data.to_json}'"
  68. item.delete(:action)
  69. response = UserAgent.put(
  70. url,
  71. item,
  72. {
  73. json: true,
  74. open_timeout: 8,
  75. read_timeout: 12,
  76. user: Setting.get('es_user'),
  77. password: Setting.get('es_password'),
  78. }
  79. )
  80. Rails.logger.info "# #{response.code}"
  81. next if response.success?
  82. raise "Unable to process PUT at #{url}\n#{response.inspect}"
  83. end
  84. end
  85. true
  86. end
  87. =begin
  88. create/update/delete index
  89. SearchIndexBackend.index(
  90. :action => 'create', # create/update/delete
  91. :data => {
  92. :mappings => {
  93. :Ticket => {
  94. :properties => {
  95. :articles => {
  96. :type => 'nested',
  97. :properties => {
  98. 'attachment' => { :type => 'attachment' }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. )
  106. SearchIndexBackend.index(
  107. :action => 'delete', # create/update/delete
  108. :name => 'Ticket', # optional
  109. )
  110. SearchIndexBackend.index(
  111. :action => 'delete', # create/update/delete
  112. )
  113. =end
  114. def self.index(data)
  115. url = build_url(data[:name])
  116. return if !url
  117. if data[:action] && data[:action] == 'delete'
  118. return SearchIndexBackend.remove(data[:name])
  119. end
  120. Rails.logger.info "# curl -X PUT \"#{url}\" \\"
  121. Rails.logger.debug "-d '#{data[:data].to_json}'"
  122. response = UserAgent.put(
  123. url,
  124. data[:data],
  125. {
  126. json: true,
  127. open_timeout: 8,
  128. read_timeout: 12,
  129. user: Setting.get('es_user'),
  130. password: Setting.get('es_password'),
  131. }
  132. )
  133. Rails.logger.info "# #{response.code}"
  134. return true if response.success?
  135. raise "Unable to process PUT at #{url}\n#{response.inspect}"
  136. end
  137. =begin
  138. add new object to search index
  139. SearchIndexBackend.add('Ticket', some_data_object)
  140. =end
  141. def self.add(type, data)
  142. url = build_url(type, data['id'])
  143. return if !url
  144. Rails.logger.info "# curl -X POST \"#{url}\" \\"
  145. Rails.logger.debug "-d '#{data.to_json}'"
  146. response = UserAgent.post(
  147. url,
  148. data,
  149. {
  150. json: true,
  151. open_timeout: 8,
  152. read_timeout: 16,
  153. user: Setting.get('es_user'),
  154. password: Setting.get('es_password'),
  155. }
  156. )
  157. Rails.logger.info "# #{response.code}"
  158. return true if response.success?
  159. raise "Unable to process POST at #{url} (size: #{data.to_json.bytesize / 1024 / 1024}M)\n#{response.inspect}"
  160. end
  161. =begin
  162. remove whole data from index
  163. SearchIndexBackend.remove('Ticket', 123)
  164. SearchIndexBackend.remove('Ticket')
  165. =end
  166. def self.remove(type, o_id = nil)
  167. url = build_url(type, o_id)
  168. return if !url
  169. Rails.logger.info "# curl -X DELETE \"#{url}\""
  170. response = UserAgent.delete(
  171. url,
  172. {
  173. open_timeout: 8,
  174. read_timeout: 16,
  175. user: Setting.get('es_user'),
  176. password: Setting.get('es_password'),
  177. }
  178. )
  179. Rails.logger.info "# #{response.code}"
  180. return true if response.success?
  181. #Rails.logger.info "NOTICE: can't delete index #{url}: " + response.inspect
  182. false
  183. end
  184. =begin
  185. return search result
  186. result = SearchIndexBackend.search('search query', limit, ['User', 'Organization'])
  187. result = SearchIndexBackend.search('search query', limit, 'User')
  188. result = [
  189. {
  190. :id => 123,
  191. :type => 'User',
  192. },
  193. {
  194. :id => 125,
  195. :type => 'User',
  196. },
  197. {
  198. :id => 15,
  199. :type => 'Organization',
  200. }
  201. ]
  202. =end
  203. def self.search(query, limit = 10, index = nil, query_extention = {})
  204. return [] if !query
  205. if index.class == Array
  206. ids = []
  207. index.each do |local_index|
  208. local_ids = search_by_index(query, limit, local_index, query_extention)
  209. ids = ids.concat(local_ids)
  210. end
  211. return ids
  212. end
  213. search_by_index(query, limit, index, query_extention)
  214. end
  215. def self.search_by_index(query, limit = 10, index = nil, query_extention = {})
  216. return [] if !query
  217. url = build_url
  218. return if !url
  219. url += if index
  220. if index.class == Array
  221. "/#{index.join(',')}/_search"
  222. else
  223. "/#{index}/_search"
  224. end
  225. else
  226. '/_search'
  227. end
  228. data = {}
  229. data['from'] = 0
  230. data['size'] = limit
  231. data['sort'] =
  232. [
  233. {
  234. updated_at: {
  235. order: 'desc'
  236. }
  237. },
  238. '_score'
  239. ]
  240. data['query'] = query_extention || {}
  241. if !data['query']['bool']
  242. data['query']['bool'] = {}
  243. end
  244. if !data['query']['bool']['must']
  245. data['query']['bool']['must'] = []
  246. end
  247. # add * on simple query like "somephrase23" or "attribute: somephrase23"
  248. if query.present?
  249. query.strip!
  250. if query.match?(/^([[:alpha:],0-9]+|[[:alpha:],0-9]+\:\s+[[:alpha:],0-9]+)$/)
  251. query += '*'
  252. end
  253. end
  254. # real search condition
  255. condition = {
  256. 'query_string' => {
  257. 'query' => query,
  258. 'default_operator' => 'AND',
  259. }
  260. }
  261. data['query']['bool']['must'].push condition
  262. Rails.logger.info "# curl -X POST \"#{url}\" \\"
  263. Rails.logger.debug " -d'#{data.to_json}'"
  264. response = UserAgent.get(
  265. url,
  266. data,
  267. {
  268. json: true,
  269. open_timeout: 5,
  270. read_timeout: 14,
  271. user: Setting.get('es_user'),
  272. password: Setting.get('es_password'),
  273. }
  274. )
  275. Rails.logger.info "# #{response.code}"
  276. if !response.success?
  277. Rails.logger.error "ERROR: POST on #{url}\n#{response.inspect}"
  278. return []
  279. end
  280. data = response.data
  281. ids = []
  282. return ids if !data
  283. return ids if !data['hits']
  284. return ids if !data['hits']['hits']
  285. data['hits']['hits'].each do |item|
  286. Rails.logger.info "... #{item['_type']} #{item['_id']}"
  287. data = {
  288. id: item['_id'],
  289. type: item['_type'],
  290. }
  291. ids.push data
  292. end
  293. ids
  294. end
  295. =begin
  296. get count of tickets and tickets which match on selector
  297. aggs_interval = {
  298. from: '2015-01-01',
  299. to: '2015-12-31',
  300. interval: 'month', # year, quarter, month, week, day, hour, minute, second
  301. field: 'created_at',
  302. }
  303. result = SearchIndexBackend.selectors(index, params[:condition], limit, current_user, aggs_interval)
  304. # for aggregations
  305. result = {
  306. hits:{
  307. total:4819,
  308. },
  309. aggregations:{
  310. time_buckets:{
  311. buckets:[
  312. {
  313. key_as_string:"2014-10-01T00:00:00.000Z",
  314. key:1412121600000,
  315. doc_count:420
  316. },
  317. {
  318. key_as_string:"2014-11-01T00:00:00.000Z",
  319. key:1414800000000,
  320. doc_count:561
  321. },
  322. ...
  323. ]
  324. }
  325. }
  326. }
  327. =end
  328. def self.selectors(index = nil, selectors = nil, limit = 10, current_user = nil, aggs_interval = nil)
  329. raise 'no selectors given' if !selectors
  330. url = build_url
  331. return if !url
  332. url += if index
  333. if index.class == Array
  334. "/#{index.join(',')}/_search"
  335. else
  336. "/#{index}/_search"
  337. end
  338. else
  339. '/_search'
  340. end
  341. data = selector2query(selectors, current_user, aggs_interval, limit)
  342. Rails.logger.info "# curl -X POST \"#{url}\" \\"
  343. Rails.logger.debug " -d'#{data.to_json}'"
  344. response = UserAgent.get(
  345. url,
  346. data,
  347. {
  348. json: true,
  349. open_timeout: 5,
  350. read_timeout: 14,
  351. user: Setting.get('es_user'),
  352. password: Setting.get('es_password'),
  353. }
  354. )
  355. Rails.logger.info "# #{response.code}"
  356. if !response.success?
  357. raise "Unable to process POST at #{url}\n#{response.inspect}"
  358. end
  359. Rails.logger.debug response.data.to_json
  360. if !aggs_interval || !aggs_interval[:interval]
  361. ticket_ids = []
  362. response.data['hits']['hits'].each do |item|
  363. ticket_ids.push item['_id']
  364. end
  365. return {
  366. count: response.data['hits']['total'],
  367. ticket_ids: ticket_ids,
  368. }
  369. end
  370. response.data
  371. end
  372. def self.selector2query(selector, _current_user, aggs_interval, limit)
  373. query_must = []
  374. query_must_not = []
  375. if selector.present?
  376. selector.each do |key, data|
  377. key_tmp = key.sub(/^.+?\./, '')
  378. t = {}
  379. if data['value'].class == Array
  380. t[:terms] = {}
  381. t[:terms][key_tmp] = data['value']
  382. else
  383. t[:term] = {}
  384. t[:term][key_tmp] = data['value']
  385. end
  386. if data['operator'] == 'is'
  387. query_must.push t
  388. elsif data['operator'] == 'is not'
  389. query_must_not.push t
  390. elsif data['operator'] == 'contains'
  391. query_must.push t
  392. elsif data['operator'] == 'contains not'
  393. query_must_not.push t
  394. else
  395. raise "unknown operator '#{data['operator']}'"
  396. end
  397. end
  398. end
  399. data = {
  400. query: {},
  401. size: limit,
  402. }
  403. # add aggs to filter
  404. if aggs_interval
  405. if aggs_interval[:interval]
  406. data[:size] = 0
  407. data[:aggs] = {
  408. time_buckets: {
  409. date_histogram: {
  410. field: aggs_interval[:field],
  411. interval: aggs_interval[:interval],
  412. }
  413. }
  414. }
  415. end
  416. r = {}
  417. r[:range] = {}
  418. r[:range][aggs_interval[:field]] = {
  419. from: aggs_interval[:from],
  420. to: aggs_interval[:to],
  421. }
  422. query_must.push r
  423. end
  424. if !data[:query][:bool]
  425. data[:query][:bool] = {}
  426. end
  427. if query_must.present?
  428. data[:query][:bool][:must] = query_must
  429. end
  430. if query_must_not.present?
  431. data[:query][:bool][:must_not] = query_must_not
  432. end
  433. # add sort
  434. if aggs_interval && aggs_interval[:field] && !aggs_interval[:interval]
  435. sort = []
  436. sort[0] = {}
  437. sort[0][aggs_interval[:field]] = {
  438. order: 'desc'
  439. }
  440. sort[1] = '_score'
  441. data['sort'] = sort
  442. end
  443. data
  444. end
  445. =begin
  446. return true if backend is configured
  447. result = SearchIndexBackend.enabled?
  448. =end
  449. def self.enabled?
  450. return false if Setting.get('es_url').blank?
  451. true
  452. end
  453. def self.build_url(type = nil, o_id = nil)
  454. return if !SearchIndexBackend.enabled?
  455. index = "#{Setting.get('es_index')}_#{Rails.env}"
  456. url = Setting.get('es_url')
  457. url = if type
  458. url_pipline = Setting.get('es_pipeline')
  459. if url_pipline.present?
  460. url_pipline = "?pipeline=#{url_pipline}"
  461. end
  462. if o_id
  463. "#{url}/#{index}/#{type}/#{o_id}#{url_pipline}"
  464. else
  465. "#{url}/#{index}/#{type}#{url_pipline}"
  466. end
  467. else
  468. "#{url}/#{index}"
  469. end
  470. url
  471. end
  472. end