search_index_backend.rb 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class SearchIndexBackend
  3. SUPPORTED_ES_VERSION_MINIMUM = '7.8'.freeze
  4. SUPPORTED_ES_VERSION_LESS_THAN = '9'.freeze
  5. =begin
  6. info about used search index machine
  7. SearchIndexBackend.info
  8. =end
  9. def self.info
  10. url = Setting.get('es_url').to_s
  11. return if url.blank?
  12. response = make_request(url)
  13. if response.success?
  14. installed_version = response.data.dig('version', 'number')
  15. raise "Unable to get elasticsearch version from response: #{response.inspect}" if installed_version.blank?
  16. installed_version_parsed = Gem::Version.new(installed_version)
  17. if (installed_version_parsed >= Gem::Version.new(SUPPORTED_ES_VERSION_LESS_THAN)) ||
  18. (installed_version_parsed < Gem::Version.new(SUPPORTED_ES_VERSION_MINIMUM))
  19. raise "Version #{installed_version} of configured elasticsearch is not supported."
  20. end
  21. return response.data
  22. end
  23. raise humanized_error(
  24. verb: 'GET',
  25. url: url,
  26. response: response,
  27. )
  28. end
  29. =begin
  30. update processors
  31. SearchIndexBackend.processors(
  32. _ingest/pipeline/attachment: {
  33. description: 'Extract attachment information from arrays',
  34. processors: [
  35. {
  36. foreach: {
  37. field: 'ticket.articles.attachments',
  38. processor: {
  39. attachment: {
  40. target_field: '_ingest._value.attachment',
  41. field: '_ingest._value.data'
  42. }
  43. }
  44. }
  45. }
  46. ]
  47. }
  48. )
  49. =end
  50. def self.processors(data)
  51. data.each do |key, items|
  52. url = "#{Setting.get('es_url')}/#{key}"
  53. items.each do |item|
  54. if item[:action] == 'delete'
  55. response = make_request(url, method: :delete)
  56. next if response.success?
  57. next if response.code.to_s == '404'
  58. raise humanized_error(
  59. verb: 'DELETE',
  60. url: url,
  61. response: response,
  62. )
  63. end
  64. item.delete(:action)
  65. make_request_and_validate(url, data: item, method: :put)
  66. end
  67. end
  68. true
  69. end
  70. =begin
  71. create/update/delete index
  72. SearchIndexBackend.index(
  73. :action => 'create', # create/update/delete
  74. :name => 'Ticket',
  75. :data => {
  76. :mappings => {
  77. :Ticket => {
  78. :properties => {
  79. :articles => {
  80. :type => 'nested',
  81. :properties => {
  82. 'attachment' => { :type => 'attachment' }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. )
  90. SearchIndexBackend.index(
  91. :action => 'delete', # create/update/delete
  92. :name => 'Ticket',
  93. )
  94. =end
  95. def self.index(data)
  96. url = build_url(type: data[:name], with_pipeline: false, with_document_type: false)
  97. return if url.blank?
  98. if data[:action] && data[:action] == 'delete'
  99. return SearchIndexBackend.remove(data[:name])
  100. end
  101. make_request_and_validate(url, data: data[:data], method: :put)
  102. end
  103. =begin
  104. add new object to search index
  105. SearchIndexBackend.add('Ticket', some_data_object)
  106. =end
  107. def self.add(type, data)
  108. url = build_url(type: type, object_id: data['id'])
  109. return if url.blank?
  110. make_request_and_validate(url, data: data, method: :post)
  111. end
  112. =begin
  113. This function updates specifc attributes of an index based on a query.
  114. data = {
  115. organization: {
  116. name: "Zammad Foundation"
  117. }
  118. }
  119. where = {
  120. organization_id: 1
  121. }
  122. SearchIndexBackend.update_by_query('Ticket', data, where)
  123. =end
  124. def self.update_by_query(type, data, where)
  125. return if data.blank?
  126. return if where.blank?
  127. url = build_url(type: type, action: '_update_by_query', with_pipeline: false, with_document_type: false, url_params: { conflicts: 'proceed' })
  128. return if url.blank?
  129. script_list = []
  130. data.each do |key, _value|
  131. script_list.push("ctx._source.#{key}=params.#{key}")
  132. end
  133. data = {
  134. script: {
  135. lang: 'painless',
  136. source: script_list.join(';'),
  137. params: data,
  138. },
  139. query: {
  140. term: where,
  141. },
  142. }
  143. make_request_and_validate(url, data: data, method: :post, read_timeout: 10.minutes)
  144. end
  145. =begin
  146. remove whole data from index
  147. SearchIndexBackend.remove('Ticket', 123)
  148. SearchIndexBackend.remove('Ticket')
  149. =end
  150. def self.remove(type, o_id = nil)
  151. url = if o_id
  152. build_url(type: type, object_id: o_id, with_pipeline: false, with_document_type: true)
  153. else
  154. build_url(type: type, object_id: o_id, with_pipeline: false, with_document_type: false)
  155. end
  156. return if url.blank?
  157. response = make_request(url, method: :delete)
  158. return true if response.success?
  159. return true if response.code.to_s == '400'
  160. humanized_error = humanized_error(
  161. verb: 'DELETE',
  162. url: url,
  163. response: response,
  164. )
  165. Rails.logger.warn "Can't delete index: #{humanized_error}"
  166. false
  167. end
  168. =begin
  169. @param query [String] search query
  170. @param index [String, Array<String>] indexes to search in (see search_by_index)
  171. @param options [Hash] search options (see build_query)
  172. @return search result
  173. @example Sample queries
  174. result = SearchIndexBackend.search('search query', ['User', 'Organization'], limit: limit)
  175. - result = SearchIndexBackend.search('search query', 'User', limit: limit)
  176. result = SearchIndexBackend.search('search query', 'User', limit: limit, sort_by: ['updated_at'], order_by: ['desc'])
  177. result = SearchIndexBackend.search('search query', 'User', limit: limit, sort_by: ['active', updated_at'], order_by: ['desc', 'desc'])
  178. result = [
  179. {
  180. :id => 123,
  181. :type => 'User',
  182. },
  183. {
  184. :id => 125,
  185. :type => 'User',
  186. },
  187. {
  188. :id => 15,
  189. :type => 'Organization',
  190. }
  191. ]
  192. =end
  193. def self.search(query, index, options = {})
  194. if !index.is_a? Array
  195. return search_by_index(query, index, options)
  196. end
  197. index
  198. .filter_map { |local_index| search_by_index(query, local_index, options) }
  199. .flatten(1)
  200. end
  201. =begin
  202. @param query [String] search query
  203. @param index [String] index name
  204. @param options [Hash] search options (see build_query)
  205. @return search result
  206. =end
  207. def self.search_by_index(query, index, options = {})
  208. return [] if query.blank?
  209. url = build_url(type: index, action: '_search', with_pipeline: false, with_document_type: false)
  210. return [] if url.blank?
  211. # real search condition
  212. condition = {
  213. 'query_string' => {
  214. 'query' => append_wildcard_to_simple_query(query),
  215. 'time_zone' => Setting.get('timezone_default').presence || 'UTC',
  216. 'default_operator' => 'AND',
  217. 'analyze_wildcard' => true,
  218. }
  219. }
  220. if (fields = options.dig(:highlight_fields_by_indexes, index.to_sym))
  221. condition['query_string']['fields'] = fields
  222. end
  223. query_data = build_query(condition, options)
  224. if (fields = options.dig(:highlight_fields_by_indexes, index.to_sym))
  225. fields_for_highlight = fields.index_with { |_elem| {} }
  226. query_data[:highlight] = { fields: fields_for_highlight }
  227. end
  228. response = make_request(url, data: query_data)
  229. if !response.success?
  230. Rails.logger.error humanized_error(
  231. verb: 'GET',
  232. url: url,
  233. payload: query_data,
  234. response: response,
  235. )
  236. return []
  237. end
  238. data = response.data&.dig('hits', 'hits')
  239. return [] if !data
  240. data.map do |item|
  241. Rails.logger.debug { "... #{item['_type']} #{item['_id']}" }
  242. output = {
  243. id: item['_id'],
  244. type: index,
  245. }
  246. if options.dig(:highlight_fields_by_indexes, index.to_sym)
  247. output[:highlight] = item['highlight']
  248. end
  249. output
  250. end
  251. end
  252. def self.search_by_index_sort(sort_by = nil, order_by = nil)
  253. result = (sort_by || [])
  254. .map(&:to_s)
  255. .each_with_object([])
  256. .each_with_index do |(elem, memo), index|
  257. next if elem.blank?
  258. next if order_by&.at(index).blank?
  259. # for sorting values use .keyword values (no analyzer is used - plain values)
  260. if elem !~ %r{\.} && elem !~ %r{_(time|date|till|id|ids|at)$} && elem != 'id'
  261. elem += '.keyword'
  262. end
  263. memo.push(
  264. elem => {
  265. order: order_by[index],
  266. },
  267. )
  268. end
  269. if result.blank?
  270. result.push(
  271. updated_at: {
  272. order: 'desc',
  273. },
  274. )
  275. end
  276. result.push('_score')
  277. result
  278. end
  279. =begin
  280. get count of tickets and tickets which match on selector
  281. result = SearchIndexBackend.selectors(index, selector)
  282. example with a simple search:
  283. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } })
  284. result = [
  285. { id: 1, type: 'Ticket' },
  286. { id: 2, type: 'Ticket' },
  287. { id: 3, type: 'Ticket' },
  288. ]
  289. you also can get aggregations
  290. result = SearchIndexBackend.selectors(index, selector, options, aggs_interval)
  291. example for aggregations within one year
  292. aggs_interval = {
  293. from: '2015-01-01',
  294. to: '2015-12-31',
  295. interval: 'month', # year, quarter, month, week, day, hour, minute, second
  296. field: 'created_at',
  297. }
  298. options = {
  299. limit: 123,
  300. current_user: User.find(123),
  301. }
  302. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } }, options, aggs_interval)
  303. result = {
  304. hits:{
  305. total:4819,
  306. },
  307. aggregations:{
  308. time_buckets:{
  309. buckets:[
  310. {
  311. key_as_string:"2014-10-01T00:00:00.000Z",
  312. key:1412121600000,
  313. doc_count:420
  314. },
  315. {
  316. key_as_string:"2014-11-01T00:00:00.000Z",
  317. key:1414800000000,
  318. doc_count:561
  319. },
  320. ...
  321. ]
  322. }
  323. }
  324. }
  325. =end
  326. def self.selectors(index, selectors = nil, options = {}, aggs_interval = nil)
  327. raise 'no selectors given' if !selectors
  328. url = build_url(type: index, action: '_search', with_pipeline: false, with_document_type: false)
  329. return if url.blank?
  330. data = selector2query(selectors, options, aggs_interval)
  331. verify_date_range(url, data)
  332. response = make_request(url, data: data)
  333. if !response.success?
  334. raise humanized_error(
  335. verb: 'GET',
  336. url: url,
  337. payload: data,
  338. response: response,
  339. )
  340. end
  341. Rails.logger.debug { response.data.to_json }
  342. if aggs_interval.blank? || aggs_interval[:interval].blank?
  343. ticket_ids = []
  344. response.data['hits']['hits'].each do |item|
  345. ticket_ids.push item['_id']
  346. end
  347. # in lower ES 6 versions, we get total count directly, in higher
  348. # versions we need to pick it from total has
  349. count = response.data['hits']['total']
  350. if response.data['hits']['total'].class != Integer
  351. count = response.data['hits']['total']['value']
  352. end
  353. return {
  354. count: count,
  355. ticket_ids: ticket_ids,
  356. }
  357. end
  358. response.data
  359. end
  360. DEFAULT_SELECTOR_OPTIONS = {
  361. limit: 10
  362. }.freeze
  363. def self.selector2query(selector, options, aggs_interval)
  364. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  365. current_user = options[:current_user]
  366. current_user_id = UserInfo.current_user_id
  367. if current_user
  368. current_user_id = current_user.id
  369. end
  370. query_must = []
  371. query_must_not = []
  372. relative_map = {
  373. day: 'd',
  374. year: 'y',
  375. month: 'M',
  376. hour: 'h',
  377. minute: 'm',
  378. }
  379. if selector.present?
  380. operators_is_isnot = ['is', 'is not']
  381. selector.each do |key, data|
  382. data = data.clone
  383. table, key_tmp = key.split('.')
  384. if key_tmp.blank?
  385. key_tmp = table
  386. table = 'ticket'
  387. end
  388. wildcard_or_term = 'term'
  389. if data['value'].is_a?(Array)
  390. wildcard_or_term = 'terms'
  391. end
  392. t = {}
  393. # use .keyword in case of compare exact values
  394. if data['operator'] == 'is' || data['operator'] == 'is not'
  395. case data['pre_condition']
  396. when 'not_set'
  397. data['value'] = if key_tmp.match?(%r{^(created_by|updated_by|owner|customer|user)_id})
  398. 1
  399. end
  400. when 'current_user.id'
  401. raise "Use current_user.id in selector, but no current_user is set #{data.inspect}" if !current_user_id
  402. data['value'] = []
  403. wildcard_or_term = 'terms'
  404. if key_tmp == 'out_of_office_replacement_id'
  405. data['value'].push User.find(current_user_id).out_of_office_agent_of.pluck(:id)
  406. else
  407. data['value'].push current_user_id
  408. end
  409. when 'current_user.organization_id'
  410. raise "Use current_user.id in selector, but no current_user is set #{data.inspect}" if !current_user_id
  411. user = User.find_by(id: current_user_id)
  412. data['value'] = user.organization_id
  413. end
  414. if data['value'].is_a?(Array)
  415. data['value'].each do |value|
  416. next if !value.is_a?(String) || value !~ %r{[A-z]}
  417. key_tmp += '.keyword'
  418. break
  419. end
  420. elsif data['value'].is_a?(String) && %r{[A-z]}.match?(data['value'])
  421. key_tmp += '.keyword'
  422. end
  423. end
  424. # use .keyword and wildcard search in cases where query contains non A-z chars
  425. if data['operator'] == 'contains' || data['operator'] == 'contains not'
  426. if data['value'].is_a?(Array)
  427. data['value'].each_with_index do |value, index|
  428. next if !value.is_a?(String) || value !~ %r{[A-z]}
  429. data['value'][index] = "*#{value}*"
  430. key_tmp += '.keyword'
  431. wildcard_or_term = 'wildcards'
  432. break
  433. end
  434. elsif data['value'].is_a?(String) && %r{[A-z]}.match?(data['value'])
  435. data['value'] = "*#{data['value']}*"
  436. key_tmp += '.keyword'
  437. wildcard_or_term = 'wildcard'
  438. end
  439. end
  440. # for pre condition not_set we want to check if values are defined for the object by exists
  441. if data['pre_condition'] == 'not_set' && operators_is_isnot.include?(data['operator']) && data['value'].nil?
  442. t['exists'] = {
  443. field: key_tmp,
  444. }
  445. case data['operator']
  446. when 'is'
  447. query_must_not.push t
  448. when 'is not'
  449. query_must.push t
  450. end
  451. next
  452. end
  453. if table != 'ticket'
  454. key_tmp = "#{table}.#{key_tmp}"
  455. end
  456. # is/is not/contains/contains not
  457. case data['operator']
  458. when 'is', 'is not', 'contains', 'contains not'
  459. t[wildcard_or_term] = {}
  460. t[wildcard_or_term][key_tmp] = data['value']
  461. case data['operator']
  462. when 'is', 'contains'
  463. query_must.push t
  464. when 'is not', 'contains not'
  465. query_must_not.push t
  466. end
  467. when 'contains all', 'contains one', 'contains all not', 'contains one not'
  468. values = data['value'].split(',').map(&:strip)
  469. t[:query_string] = {}
  470. case data['operator']
  471. when 'contains all'
  472. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" AND "')}\""
  473. query_must.push t
  474. when 'contains one not'
  475. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" OR "')}\""
  476. query_must_not.push t
  477. when 'contains one'
  478. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" OR "')}\""
  479. query_must.push t
  480. when 'contains all not'
  481. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" AND "')}\""
  482. query_must_not.push t
  483. end
  484. # within last/within next (relative)
  485. when 'within last (relative)', 'within next (relative)'
  486. range = relative_map[data['range'].to_sym]
  487. if range.blank?
  488. raise "Invalid relative_map for range '#{data['range']}'."
  489. end
  490. t[:range] = {}
  491. t[:range][key_tmp] = {}
  492. if data['operator'] == 'within last (relative)'
  493. t[:range][key_tmp][:gte] = "now-#{data['value']}#{range}"
  494. else
  495. t[:range][key_tmp][:lt] = "now+#{data['value']}#{range}"
  496. end
  497. query_must.push t
  498. # before/after (relative)
  499. when 'before (relative)', 'after (relative)'
  500. range = relative_map[data['range'].to_sym]
  501. if range.blank?
  502. raise "Invalid relative_map for range '#{data['range']}'."
  503. end
  504. t[:range] = {}
  505. t[:range][key_tmp] = {}
  506. if data['operator'] == 'before (relative)'
  507. t[:range][key_tmp][:lt] = "now-#{data['value']}#{range}"
  508. else
  509. t[:range][key_tmp][:gt] = "now+#{data['value']}#{range}"
  510. end
  511. query_must.push t
  512. # till/from (relative)
  513. when 'till (relative)', 'from (relative)'
  514. range = relative_map[data['range'].to_sym]
  515. if range.blank?
  516. raise "Invalid relative_map for range '#{data['range']}'."
  517. end
  518. t[:range] = {}
  519. t[:range][key_tmp] = {}
  520. if data['operator'] == 'till (relative)'
  521. t[:range][key_tmp][:lt] = "now+#{data['value']}#{range}"
  522. else
  523. t[:range][key_tmp][:gt] = "now-#{data['value']}#{range}"
  524. end
  525. query_must.push t
  526. # before/after (absolute)
  527. when 'before (absolute)', 'after (absolute)'
  528. t[:range] = {}
  529. t[:range][key_tmp] = {}
  530. if data['operator'] == 'before (absolute)'
  531. t[:range][key_tmp][:lt] = (data['value'])
  532. else
  533. t[:range][key_tmp][:gt] = (data['value'])
  534. end
  535. query_must.push t
  536. else
  537. raise "unknown operator '#{data['operator']}' for #{key}"
  538. end
  539. end
  540. end
  541. data = {
  542. query: {},
  543. size: options[:limit],
  544. }
  545. # add aggs to filter
  546. if aggs_interval.present?
  547. if aggs_interval[:interval].present?
  548. data[:size] = 0
  549. data[:aggs] = {
  550. time_buckets: {
  551. date_histogram: {
  552. field: aggs_interval[:field],
  553. calendar_interval: aggs_interval[:interval],
  554. }
  555. }
  556. }
  557. if aggs_interval[:timezone].present?
  558. data[:aggs][:time_buckets][:date_histogram][:time_zone] = aggs_interval[:timezone]
  559. end
  560. end
  561. r = {}
  562. r[:range] = {}
  563. r[:range][aggs_interval[:field]] = {
  564. from: aggs_interval[:from],
  565. to: aggs_interval[:to],
  566. }
  567. query_must.push r
  568. end
  569. data[:query][:bool] ||= {}
  570. if query_must.present?
  571. data[:query][:bool][:must] = query_must
  572. end
  573. if query_must_not.present?
  574. data[:query][:bool][:must_not] = query_must_not
  575. end
  576. # add sort
  577. if aggs_interval.present? && aggs_interval[:field].present? && aggs_interval[:interval].blank?
  578. sort = []
  579. sort[0] = {}
  580. sort[0][aggs_interval[:field]] = {
  581. order: 'desc'
  582. }
  583. sort[1] = '_score'
  584. data['sort'] = sort
  585. else
  586. data['sort'] = search_by_index_sort(options[:sort_by], options[:order_by])
  587. end
  588. data
  589. end
  590. =begin
  591. return true if backend is configured
  592. result = SearchIndexBackend.enabled?
  593. =end
  594. def self.enabled?
  595. return false if Setting.get('es_url').blank?
  596. true
  597. end
  598. def self.build_index_name(index = nil)
  599. local_index = "#{Setting.get('es_index')}_#{Rails.env}"
  600. return local_index if index.blank?
  601. "#{local_index}_#{index.underscore.tr('/', '_')}"
  602. end
  603. =begin
  604. generate url for index or document access (only for internal use)
  605. # url to access single document in index (in case with_pipeline or not)
  606. url = SearchIndexBackend.build_url(type: 'User', object_id: 123, with_pipeline: true)
  607. # url to access whole index
  608. url = SearchIndexBackend.build_url(type: 'User')
  609. # url to access document definition in index (only es6 and higher)
  610. url = SearchIndexBackend.build_url(type: 'User', with_pipeline: false, with_document_type: true)
  611. # base url
  612. url = SearchIndexBackend.build_url
  613. =end
  614. # rubocop:disable Metrics/ParameterLists
  615. def self.build_url(type: nil, action: nil, object_id: nil, with_pipeline: true, with_document_type: true, url_params: {})
  616. # rubocop:enable Metrics/ParameterLists
  617. return if !SearchIndexBackend.enabled?
  618. # set index
  619. index = build_index_name(type)
  620. # add pipeline if needed
  621. if index && with_pipeline == true
  622. url_pipline = Setting.get('es_pipeline')
  623. if url_pipline.present?
  624. url_params['pipeline'] = url_pipline
  625. end
  626. end
  627. # prepare url params
  628. params_string = ''
  629. if url_params.present?
  630. params_string = "?#{URI.encode_www_form(url_params)}"
  631. end
  632. url = Setting.get('es_url')
  633. return "#{url}#{params_string}" if index.blank?
  634. # add type information
  635. url = "#{url}/#{index}"
  636. # add document type
  637. if with_document_type
  638. url = "#{url}/_doc"
  639. end
  640. # add action
  641. if action
  642. url = "#{url}/#{action}"
  643. end
  644. # add object id
  645. if object_id.present?
  646. url = "#{url}/#{object_id}"
  647. end
  648. "#{url}#{params_string}"
  649. end
  650. def self.humanized_error(verb:, url:, response:, payload: nil)
  651. prefix = "Unable to process #{verb} request to elasticsearch URL '#{url}'."
  652. suffix = "\n\nResponse:\n#{response.inspect}\n\n"
  653. if payload.respond_to?(:to_json)
  654. suffix += "Payload:\n#{payload.to_json}"
  655. suffix += "\n\nPayload size: #{payload.to_json.bytesize / 1024 / 1024}M"
  656. else
  657. suffix += "Payload:\n#{payload.inspect}"
  658. end
  659. message = if response&.error&.match?(__('Connection refused'))
  660. __("Elasticsearch is not reachable. It's possible that it's not running. Please check whether it is installed.")
  661. elsif url.end_with?('pipeline/zammad-attachment', 'pipeline=zammad-attachment') && response.code == 400
  662. __('The installed attachment plugin could not handle the request payload. Ensure that the correct attachment plugin is installed (ingest-attachment).')
  663. else
  664. __('Check the response and payload for detailed information:')
  665. end
  666. result = "#{prefix} #{message}#{suffix}"
  667. Rails.logger.error result.first(40_000)
  668. result
  669. end
  670. # add * on simple query like "somephrase23"
  671. def self.append_wildcard_to_simple_query(query)
  672. query = query.strip
  673. query += '*' if query.exclude?(':')
  674. query
  675. end
  676. =begin
  677. @param condition [Hash] search condition
  678. @param options [Hash] search options
  679. @option options [Integer] :from
  680. @option options [Integer] :limit
  681. @option options [Hash] :query_extension applied to ElasticSearch query
  682. @option options [Array<String>] :order_by ordering directions, desc or asc
  683. @option options [Array<String>] :sort_by fields to sort by
  684. =end
  685. DEFAULT_QUERY_OPTIONS = {
  686. from: 0,
  687. limit: 10
  688. }.freeze
  689. def self.build_query(condition, options = {})
  690. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  691. data = {
  692. from: options[:from],
  693. size: options[:limit],
  694. sort: search_by_index_sort(options[:sort_by], options[:order_by]),
  695. query: {
  696. bool: {
  697. must: []
  698. }
  699. }
  700. }
  701. if (extension = options[:query_extension])
  702. data[:query].deep_merge! extension.deep_dup
  703. end
  704. data[:query][:bool][:must].push condition
  705. if options[:ids].present?
  706. data[:query][:bool][:must].push({ ids: { values: options[:ids] } })
  707. end
  708. data
  709. end
  710. =begin
  711. refreshes all indexes to make previous request data visible in future requests
  712. SearchIndexBackend.refresh
  713. =end
  714. def self.refresh
  715. return if !enabled?
  716. url = "#{Setting.get('es_url')}/_all/_refresh"
  717. make_request_and_validate(url, method: :post)
  718. end
  719. =begin
  720. helper method for making HTTP calls
  721. @param url [String] url
  722. @option params [Hash] :data is a payload hash
  723. @option params [Symbol] :method is a HTTP method
  724. @option params [Integer] :open_timeout is HTTP request open timeout
  725. @option params [Integer] :read_timeout is HTTP request read timeout
  726. @return UserAgent response
  727. =end
  728. def self.make_request(url, data: {}, method: :get, open_timeout: 8, read_timeout: 180)
  729. Rails.logger.debug { "# curl -X #{method} \"#{url}\" " }
  730. Rails.logger.debug { "-d '#{data.to_json}'" } if data.present?
  731. options = {
  732. json: true,
  733. open_timeout: open_timeout,
  734. read_timeout: read_timeout,
  735. total_timeout: (open_timeout + read_timeout + 60),
  736. open_socket_tries: 3,
  737. user: Setting.get('es_user'),
  738. password: Setting.get('es_password'),
  739. }
  740. response = UserAgent.send(method, url, data, options)
  741. Rails.logger.debug { "# #{response.code}" }
  742. response
  743. end
  744. =begin
  745. helper method for making HTTP calls and raising error if response was not success
  746. @param url [String] url
  747. @option args [Hash] see {make_request}
  748. @return [Boolean] always returns true. Raises error if something went wrong.
  749. =end
  750. def self.make_request_and_validate(url, **args)
  751. response = make_request(url, **args)
  752. return true if response.success?
  753. raise humanized_error(
  754. verb: args[:method],
  755. url: url,
  756. payload: args[:data],
  757. response: response
  758. )
  759. end
  760. =begin
  761. This function will return a index mapping based on the
  762. attributes of the database table of the existing object.
  763. mapping = SearchIndexBackend.get_mapping_properties_object(Ticket)
  764. Returns:
  765. mapping = {
  766. User: {
  767. properties: {
  768. firstname: {
  769. type: 'keyword',
  770. },
  771. }
  772. }
  773. }
  774. =end
  775. def self.get_mapping_properties_object(object)
  776. name = '_doc'
  777. result = {
  778. name => {
  779. properties: {}
  780. }
  781. }
  782. store_columns = %w[preferences data]
  783. # for elasticsearch 6.x and later
  784. string_type = 'text'
  785. string_raw = { type: 'keyword', ignore_above: 5012 }
  786. boolean_raw = { type: 'boolean' }
  787. object.columns_hash.each do |key, value|
  788. if value.type == :string && value.limit && value.limit <= 5000 && store_columns.exclude?(key)
  789. result[name][:properties][key] = {
  790. type: string_type,
  791. fields: {
  792. keyword: string_raw,
  793. }
  794. }
  795. elsif value.type == :integer
  796. result[name][:properties][key] = {
  797. type: 'integer',
  798. }
  799. elsif value.type == :datetime || value.type == :date
  800. result[name][:properties][key] = {
  801. type: 'date',
  802. }
  803. elsif value.type == :boolean
  804. result[name][:properties][key] = {
  805. type: 'boolean',
  806. fields: {
  807. keyword: boolean_raw,
  808. }
  809. }
  810. elsif value.type == :binary
  811. result[name][:properties][key] = {
  812. type: 'binary',
  813. }
  814. elsif value.type == :bigint
  815. result[name][:properties][key] = {
  816. type: 'long',
  817. }
  818. elsif value.type == :decimal
  819. result[name][:properties][key] = {
  820. type: 'float',
  821. }
  822. end
  823. end
  824. case object.name
  825. when 'Ticket'
  826. result[name][:_source] = {
  827. excludes: ['article.attachment']
  828. }
  829. result[name][:properties][:article] = {
  830. type: 'nested',
  831. include_in_parent: true,
  832. }
  833. when 'KnowledgeBase::Answer::Translation'
  834. result[name][:_source] = {
  835. excludes: ['attachment']
  836. }
  837. end
  838. result[name]
  839. end
  840. # get es version
  841. def self.version
  842. @version ||= SearchIndexBackend.info&.dig('version', 'number')
  843. end
  844. def self.configured?
  845. Setting.get('es_url').present?
  846. end
  847. def self.settings
  848. {
  849. 'index.mapping.total_fields.limit': 2000,
  850. }
  851. end
  852. def self.create_index(models = Models.indexable)
  853. models.each do |local_object|
  854. SearchIndexBackend.index(
  855. action: 'create',
  856. name: local_object.name,
  857. data: {
  858. mappings: SearchIndexBackend.get_mapping_properties_object(local_object),
  859. settings: SearchIndexBackend.settings,
  860. }
  861. )
  862. end
  863. end
  864. def self.drop_index(models = Models.indexable)
  865. models.each do |local_object|
  866. SearchIndexBackend.index(
  867. action: 'delete',
  868. name: local_object.name,
  869. )
  870. end
  871. end
  872. def self.create_object_index(object)
  873. models = Models.indexable.select { |c| c.to_s == object }
  874. create_index(models)
  875. end
  876. def self.drop_object_index(object)
  877. models = Models.indexable.select { |c| c.to_s == object }
  878. drop_index(models)
  879. end
  880. def self.pipeline(create: false)
  881. pipeline = Setting.get('es_pipeline')
  882. if create && pipeline.blank?
  883. pipeline = "zammad#{SecureRandom.uuid}"
  884. Setting.set('es_pipeline', pipeline)
  885. end
  886. pipeline
  887. end
  888. def self.pipeline_settings
  889. {
  890. ignore_failure: true,
  891. ignore_missing: true,
  892. }
  893. end
  894. def self.create_pipeline
  895. SearchIndexBackend.processors(
  896. "_ingest/pipeline/#{pipeline(create: true)}": [
  897. {
  898. action: 'delete',
  899. },
  900. {
  901. action: 'create',
  902. description: __('Extract zammad-attachment information from arrays'),
  903. processors: [
  904. {
  905. foreach: {
  906. field: 'article',
  907. processor: {
  908. foreach: {
  909. field: '_ingest._value.attachment',
  910. processor: {
  911. attachment: {
  912. target_field: '_ingest._value',
  913. field: '_ingest._value._content',
  914. }.merge(pipeline_settings),
  915. }
  916. }.merge(pipeline_settings),
  917. }
  918. }.merge(pipeline_settings),
  919. },
  920. {
  921. foreach: {
  922. field: 'attachment',
  923. processor: {
  924. attachment: {
  925. target_field: '_ingest._value',
  926. field: '_ingest._value._content',
  927. }.merge(pipeline_settings),
  928. }
  929. }.merge(pipeline_settings),
  930. }
  931. ]
  932. }
  933. ]
  934. )
  935. end
  936. def self.drop_pipeline
  937. return if pipeline.blank?
  938. SearchIndexBackend.processors(
  939. "_ingest/pipeline/#{pipeline}": [
  940. {
  941. action: 'delete',
  942. },
  943. ]
  944. )
  945. end
  946. # verifies date range ElasticSearch payload
  947. #
  948. # @param url [String] of ElasticSearch
  949. # @param payload [Hash] Elasticsearch query payload
  950. #
  951. # @return [Boolean] or raises error
  952. def self.verify_date_range(url, payload)
  953. ranges_payload = payload.dig(:query, :bool, :must)
  954. return true if ranges_payload.nil?
  955. ranges = ranges_payload
  956. .select { |elem| elem.key? :range }
  957. .map { |elem| [elem[:range].keys.first, convert_es_date_range(elem)] }
  958. .each_with_object({}) { |elem, sum| (sum[elem.first] ||= []) << elem.last }
  959. return true if ranges.all? { |_, ranges_by_key| verify_single_key_range(ranges_by_key) }
  960. error_prefix = "Unable to process request to elasticsearch URL '#{url}'."
  961. error_suffix = "Payload:\n#{payload.to_json}"
  962. error_message = __('Conflicting date ranges')
  963. result = "#{error_prefix} #{error_message} #{error_suffix}"
  964. Rails.logger.error result.first(40_000)
  965. raise result
  966. end
  967. # checks if all ranges are overlaping
  968. #
  969. # @param ranges [Array<Range<DateTime>>] to use in search
  970. #
  971. # @return [Boolean]
  972. def self.verify_single_key_range(ranges)
  973. ranges
  974. .each_with_index
  975. .all? do |range, i|
  976. ranges
  977. .slice((i + 1)..)
  978. .all? { |elem| elem.overlaps? range }
  979. end
  980. end
  981. # Converts paylaod component to dates range
  982. #
  983. # @param elem [Hash] payload component
  984. #
  985. # @return [Range<DateTime>]
  986. def self.convert_es_date_range(elem)
  987. range = elem[:range].first.last
  988. from = parse_es_range_date range[:from] || range[:gt] || '-9999-01-01'
  989. to = parse_es_range_date range[:to] || range[:lt] || '9999-01-01'
  990. from..to
  991. end
  992. # Parses absolute date or converts relative date
  993. #
  994. # @param input [String] string representation of date
  995. #
  996. # @return [Range<DateTime>]
  997. def self.parse_es_range_date(input)
  998. match = input.match(%r{^now(-|\+)(\d+)(\w{1})$})
  999. return DateTime.parse input if !match
  1000. map = {
  1001. d: 'day',
  1002. y: 'year',
  1003. M: 'month',
  1004. h: 'hour',
  1005. m: 'minute',
  1006. }
  1007. range = match.captures[1].to_i.send map[match.captures[2].to_sym]
  1008. case match.captures[0]
  1009. when '-'
  1010. range.ago
  1011. when '+'
  1012. range.from_now
  1013. end
  1014. end
  1015. end