search_index_backend.rb 33 KB

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