search_index_backend.rb 30 KB

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