search_index_backend.rb 33 KB

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