search_index_backend.rb 14 KB

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