search_index_backend.rb 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. # Copyright (C) 2012-2021 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. 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. response = make_request(url, data: data)
  330. if !response.success?
  331. raise humanized_error(
  332. verb: 'GET',
  333. url: url,
  334. payload: data,
  335. response: response,
  336. )
  337. end
  338. Rails.logger.debug { response.data.to_json }
  339. if aggs_interval.blank? || aggs_interval[:interval].blank?
  340. ticket_ids = []
  341. response.data['hits']['hits'].each do |item|
  342. ticket_ids.push item['_id']
  343. end
  344. # in lower ES 6 versions, we get total count directly, in higher
  345. # versions we need to pick it from total has
  346. count = response.data['hits']['total']
  347. if response.data['hits']['total'].class != Integer
  348. count = response.data['hits']['total']['value']
  349. end
  350. return {
  351. count: count,
  352. ticket_ids: ticket_ids,
  353. }
  354. end
  355. response.data
  356. end
  357. DEFAULT_SELECTOR_OPTIONS = {
  358. limit: 10
  359. }.freeze
  360. def self.selector2query(selector, options, aggs_interval)
  361. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  362. current_user = options[:current_user]
  363. current_user_id = UserInfo.current_user_id
  364. if current_user
  365. current_user_id = current_user.id
  366. end
  367. query_must = []
  368. query_must_not = []
  369. relative_map = {
  370. day: 'd',
  371. year: 'y',
  372. month: 'M',
  373. hour: 'h',
  374. minute: 'm',
  375. }
  376. if selector.present?
  377. operators_is_isnot = ['is', 'is not']
  378. selector.each do |key, data|
  379. data = data.clone
  380. table, key_tmp = key.split('.')
  381. if key_tmp.blank?
  382. key_tmp = table
  383. table = 'ticket'
  384. end
  385. wildcard_or_term = 'term'
  386. if data['value'].is_a?(Array)
  387. wildcard_or_term = 'terms'
  388. end
  389. t = {}
  390. # use .keyword in case of compare exact values
  391. if data['operator'] == 'is' || data['operator'] == 'is not'
  392. case data['pre_condition']
  393. when 'not_set'
  394. data['value'] = if key_tmp.match?(%r{^(created_by|updated_by|owner|customer|user)_id})
  395. 1
  396. end
  397. when 'current_user.id'
  398. raise "Use current_user.id in selector, but no current_user is set #{data.inspect}" if !current_user_id
  399. data['value'] = []
  400. wildcard_or_term = 'terms'
  401. if key_tmp == 'out_of_office_replacement_id'
  402. data['value'].push User.find(current_user_id).out_of_office_agent_of.pluck(:id)
  403. else
  404. data['value'].push current_user_id
  405. end
  406. when 'current_user.organization_id'
  407. raise "Use current_user.id in selector, but no current_user is set #{data.inspect}" if !current_user_id
  408. user = User.find_by(id: current_user_id)
  409. data['value'] = user.organization_id
  410. end
  411. if data['value'].is_a?(Array)
  412. data['value'].each do |value|
  413. next if !value.is_a?(String) || value !~ %r{[A-z]}
  414. key_tmp += '.keyword'
  415. break
  416. end
  417. elsif data['value'].is_a?(String) && %r{[A-z]}.match?(data['value'])
  418. key_tmp += '.keyword'
  419. end
  420. end
  421. # use .keyword and wildcard search in cases where query contains non A-z chars
  422. if data['operator'] == 'contains' || data['operator'] == 'contains not'
  423. if data['value'].is_a?(Array)
  424. data['value'].each_with_index do |value, index|
  425. next if !value.is_a?(String) || value !~ %r{[A-z]}
  426. data['value'][index] = "*#{value}*"
  427. key_tmp += '.keyword'
  428. wildcard_or_term = 'wildcards'
  429. break
  430. end
  431. elsif data['value'].is_a?(String) && %r{[A-z]}.match?(data['value'])
  432. data['value'] = "*#{data['value']}*"
  433. key_tmp += '.keyword'
  434. wildcard_or_term = 'wildcard'
  435. end
  436. end
  437. # for pre condition not_set we want to check if values are defined for the object by exists
  438. if data['pre_condition'] == 'not_set' && operators_is_isnot.include?(data['operator']) && data['value'].nil?
  439. t['exists'] = {
  440. field: key_tmp,
  441. }
  442. case data['operator']
  443. when 'is'
  444. query_must_not.push t
  445. when 'is not'
  446. query_must.push t
  447. end
  448. next
  449. end
  450. if table != 'ticket'
  451. key_tmp = "#{table}.#{key_tmp}"
  452. end
  453. # is/is not/contains/contains not
  454. case data['operator']
  455. when 'is', 'is not', 'contains', 'contains not'
  456. t[wildcard_or_term] = {}
  457. t[wildcard_or_term][key_tmp] = data['value']
  458. case data['operator']
  459. when 'is', 'contains'
  460. query_must.push t
  461. when 'is not', 'contains not'
  462. query_must_not.push t
  463. end
  464. when 'contains all', 'contains one', 'contains all not', 'contains one not'
  465. values = data['value'].split(',').map(&:strip)
  466. t[:query_string] = {}
  467. case data['operator']
  468. when 'contains all'
  469. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" AND "')}\""
  470. query_must.push t
  471. when 'contains one not'
  472. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" OR "')}\""
  473. query_must_not.push t
  474. when 'contains one'
  475. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" OR "')}\""
  476. query_must.push t
  477. when 'contains all not'
  478. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" AND "')}\""
  479. query_must_not.push t
  480. end
  481. # within last/within next (relative)
  482. when 'within last (relative)', 'within next (relative)'
  483. range = relative_map[data['range'].to_sym]
  484. if range.blank?
  485. raise "Invalid relative_map for range '#{data['range']}'."
  486. end
  487. t[:range] = {}
  488. t[:range][key_tmp] = {}
  489. if data['operator'] == 'within last (relative)'
  490. t[:range][key_tmp][:gte] = "now-#{data['value']}#{range}"
  491. else
  492. t[:range][key_tmp][:lt] = "now+#{data['value']}#{range}"
  493. end
  494. query_must.push t
  495. # before/after (relative)
  496. when 'before (relative)', 'after (relative)'
  497. range = relative_map[data['range'].to_sym]
  498. if range.blank?
  499. raise "Invalid relative_map for range '#{data['range']}'."
  500. end
  501. t[:range] = {}
  502. t[:range][key_tmp] = {}
  503. if data['operator'] == 'before (relative)'
  504. t[:range][key_tmp][:lt] = "now-#{data['value']}#{range}"
  505. else
  506. t[:range][key_tmp][:gt] = "now+#{data['value']}#{range}"
  507. end
  508. query_must.push t
  509. # till/from (relative)
  510. when 'till (relative)', 'from (relative)'
  511. range = relative_map[data['range'].to_sym]
  512. if range.blank?
  513. raise "Invalid relative_map for range '#{data['range']}'."
  514. end
  515. t[:range] = {}
  516. t[:range][key_tmp] = {}
  517. if data['operator'] == 'till (relative)'
  518. t[:range][key_tmp][:lt] = "now+#{data['value']}#{range}"
  519. else
  520. t[:range][key_tmp][:gt] = "now-#{data['value']}#{range}"
  521. end
  522. query_must.push t
  523. # before/after (absolute)
  524. when 'before (absolute)', 'after (absolute)'
  525. t[:range] = {}
  526. t[:range][key_tmp] = {}
  527. if data['operator'] == 'before (absolute)'
  528. t[:range][key_tmp][:lt] = (data['value'])
  529. else
  530. t[:range][key_tmp][:gt] = (data['value'])
  531. end
  532. query_must.push t
  533. else
  534. raise "unknown operator '#{data['operator']}' for #{key}"
  535. end
  536. end
  537. end
  538. data = {
  539. query: {},
  540. size: options[:limit],
  541. }
  542. # add aggs to filter
  543. if aggs_interval.present?
  544. if aggs_interval[:interval].present?
  545. data[:size] = 0
  546. data[:aggs] = {
  547. time_buckets: {
  548. date_histogram: {
  549. field: aggs_interval[:field],
  550. interval: aggs_interval[:interval],
  551. }
  552. }
  553. }
  554. if aggs_interval[:timezone].present?
  555. data[:aggs][:time_buckets][:date_histogram][:time_zone] = aggs_interval[:timezone]
  556. end
  557. end
  558. r = {}
  559. r[:range] = {}
  560. r[:range][aggs_interval[:field]] = {
  561. from: aggs_interval[:from],
  562. to: aggs_interval[:to],
  563. }
  564. query_must.push r
  565. end
  566. data[:query][:bool] ||= {}
  567. if query_must.present?
  568. data[:query][:bool][:must] = query_must
  569. end
  570. if query_must_not.present?
  571. data[:query][:bool][:must_not] = query_must_not
  572. end
  573. # add sort
  574. if aggs_interval.present? && aggs_interval[:field].present? && aggs_interval[:interval].blank?
  575. sort = []
  576. sort[0] = {}
  577. sort[0][aggs_interval[:field]] = {
  578. order: 'desc'
  579. }
  580. sort[1] = '_score'
  581. data['sort'] = sort
  582. else
  583. data['sort'] = search_by_index_sort(options[:sort_by], options[:order_by])
  584. end
  585. data
  586. end
  587. =begin
  588. return true if backend is configured
  589. result = SearchIndexBackend.enabled?
  590. =end
  591. def self.enabled?
  592. return false if Setting.get('es_url').blank?
  593. true
  594. end
  595. def self.build_index_name(index = nil)
  596. local_index = "#{Setting.get('es_index')}_#{Rails.env}"
  597. return local_index if index.blank?
  598. "#{local_index}_#{index.underscore.tr('/', '_')}"
  599. end
  600. =begin
  601. generate url for index or document access (only for internal use)
  602. # url to access single document in index (in case with_pipeline or not)
  603. url = SearchIndexBackend.build_url(type: 'User', object_id: 123, with_pipeline: true)
  604. # url to access whole index
  605. url = SearchIndexBackend.build_url(type: 'User')
  606. # url to access document definition in index (only es6 and higher)
  607. url = SearchIndexBackend.build_url(type: 'User', with_pipeline: false, with_document_type: true)
  608. # base url
  609. url = SearchIndexBackend.build_url
  610. =end
  611. # rubocop:disable Metrics/ParameterLists
  612. def self.build_url(type: nil, action: nil, object_id: nil, with_pipeline: true, with_document_type: true, url_params: {})
  613. # rubocop:enable Metrics/ParameterLists
  614. return if !SearchIndexBackend.enabled?
  615. # set index
  616. index = build_index_name(type)
  617. # add pipeline if needed
  618. if index && with_pipeline == true
  619. url_pipline = Setting.get('es_pipeline')
  620. if url_pipline.present?
  621. url_params['pipeline'] = url_pipline
  622. end
  623. end
  624. # prepare url params
  625. params_string = ''
  626. if url_params.present?
  627. params_string = "?#{URI.encode_www_form(url_params)}"
  628. end
  629. url = Setting.get('es_url')
  630. return "#{url}#{params_string}" if index.blank?
  631. # add type information
  632. url = "#{url}/#{index}"
  633. # add document type
  634. if with_document_type
  635. url = "#{url}/_doc"
  636. end
  637. # add action
  638. if action
  639. url = "#{url}/#{action}"
  640. end
  641. # add object id
  642. if object_id.present?
  643. url = "#{url}/#{object_id}"
  644. end
  645. "#{url}#{params_string}"
  646. end
  647. def self.humanized_error(verb:, url:, response:, payload: nil)
  648. prefix = "Unable to process #{verb} request to elasticsearch URL '#{url}'."
  649. suffix = "\n\nResponse:\n#{response.inspect}\n\n"
  650. if payload.respond_to?(:to_json)
  651. suffix += "Payload:\n#{payload.to_json}"
  652. suffix += "\n\nPayload size: #{payload.to_json.bytesize / 1024 / 1024}M"
  653. else
  654. suffix += "Payload:\n#{payload.inspect}"
  655. end
  656. message = if response&.error&.match?('Connection refused')
  657. "Elasticsearch is not reachable, probably because it's not running or even installed."
  658. elsif url.end_with?('pipeline/zammad-attachment', 'pipeline=zammad-attachment') && response.code == 400
  659. 'The installed attachment plugin could not handle the request payload. Ensure that the correct attachment plugin is installed (ingest-attachment).'
  660. else
  661. 'Check the response and payload for detailed information: '
  662. end
  663. result = "#{prefix} #{message}#{suffix}"
  664. Rails.logger.error result.first(40_000)
  665. result
  666. end
  667. # add * on simple query like "somephrase23"
  668. def self.append_wildcard_to_simple_query(query)
  669. query.strip!
  670. query += '*' if query.exclude?(':')
  671. query
  672. end
  673. =begin
  674. @param condition [Hash] search condition
  675. @param options [Hash] search options
  676. @option options [Integer] :from
  677. @option options [Integer] :limit
  678. @option options [Hash] :query_extension applied to ElasticSearch query
  679. @option options [Array<String>] :order_by ordering directions, desc or asc
  680. @option options [Array<String>] :sort_by fields to sort by
  681. =end
  682. DEFAULT_QUERY_OPTIONS = {
  683. from: 0,
  684. limit: 10
  685. }.freeze
  686. def self.build_query(condition, options = {})
  687. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  688. data = {
  689. from: options[:from],
  690. size: options[:limit],
  691. sort: search_by_index_sort(options[:sort_by], options[:order_by]),
  692. query: {
  693. bool: {
  694. must: []
  695. }
  696. }
  697. }
  698. if (extension = options[:query_extension])
  699. data[:query].deep_merge! extension.deep_dup
  700. end
  701. data[:query][:bool][:must].push condition
  702. data
  703. end
  704. =begin
  705. refreshes all indexes to make previous request data visible in future requests
  706. SearchIndexBackend.refresh
  707. =end
  708. def self.refresh
  709. return if !enabled?
  710. url = "#{Setting.get('es_url')}/_all/_refresh"
  711. make_request_and_validate(url, method: :post)
  712. end
  713. =begin
  714. helper method for making HTTP calls
  715. @param url [String] url
  716. @option params [Hash] :data is a payload hash
  717. @option params [Symbol] :method is a HTTP method
  718. @option params [Integer] :open_timeout is HTTP request open timeout
  719. @option params [Integer] :read_timeout is HTTP request read timeout
  720. @return UserAgent response
  721. =end
  722. def self.make_request(url, data: {}, method: :get, open_timeout: 8, read_timeout: 180)
  723. Rails.logger.debug { "# curl -X #{method} \"#{url}\" " }
  724. Rails.logger.debug { "-d '#{data.to_json}'" } if data.present?
  725. options = {
  726. json: true,
  727. open_timeout: open_timeout,
  728. read_timeout: read_timeout,
  729. total_timeout: (open_timeout + read_timeout + 60),
  730. open_socket_tries: 3,
  731. user: Setting.get('es_user'),
  732. password: Setting.get('es_password'),
  733. }
  734. response = UserAgent.send(method, url, data, options)
  735. Rails.logger.debug { "# #{response.code}" }
  736. response
  737. end
  738. =begin
  739. helper method for making HTTP calls and raising error if response was not success
  740. @param url [String] url
  741. @option args [Hash] see {make_request}
  742. @return [Boolean] always returns true. Raises error if something went wrong.
  743. =end
  744. def self.make_request_and_validate(url, **args)
  745. response = make_request(url, **args)
  746. return true if response.success?
  747. raise humanized_error(
  748. verb: args[:method],
  749. url: url,
  750. payload: args[:data],
  751. response: response
  752. )
  753. end
  754. =begin
  755. This function will return a index mapping based on the
  756. attributes of the database table of the existing object.
  757. mapping = SearchIndexBackend.get_mapping_properties_object(Ticket)
  758. Returns:
  759. mapping = {
  760. User: {
  761. properties: {
  762. firstname: {
  763. type: 'keyword',
  764. },
  765. }
  766. }
  767. }
  768. =end
  769. def self.get_mapping_properties_object(object)
  770. name = '_doc'
  771. result = {
  772. name => {
  773. properties: {}
  774. }
  775. }
  776. store_columns = %w[preferences data]
  777. # for elasticsearch 6.x and later
  778. string_type = 'text'
  779. string_raw = { type: 'keyword', ignore_above: 5012 }
  780. boolean_raw = { type: 'boolean' }
  781. object.columns_hash.each do |key, value|
  782. if value.type == :string && value.limit && value.limit <= 5000 && store_columns.exclude?(key)
  783. result[name][:properties][key] = {
  784. type: string_type,
  785. fields: {
  786. keyword: string_raw,
  787. }
  788. }
  789. elsif value.type == :integer
  790. result[name][:properties][key] = {
  791. type: 'integer',
  792. }
  793. elsif value.type == :datetime || value.type == :date
  794. result[name][:properties][key] = {
  795. type: 'date',
  796. }
  797. elsif value.type == :boolean
  798. result[name][:properties][key] = {
  799. type: 'boolean',
  800. fields: {
  801. keyword: boolean_raw,
  802. }
  803. }
  804. elsif value.type == :binary
  805. result[name][:properties][key] = {
  806. type: 'binary',
  807. }
  808. elsif value.type == :bigint
  809. result[name][:properties][key] = {
  810. type: 'long',
  811. }
  812. elsif value.type == :decimal
  813. result[name][:properties][key] = {
  814. type: 'float',
  815. }
  816. end
  817. end
  818. case object.name
  819. when 'Ticket'
  820. result[name][:_source] = {
  821. excludes: ['article.attachment']
  822. }
  823. result[name][:properties][:article] = {
  824. type: 'nested',
  825. include_in_parent: true,
  826. }
  827. when 'KnowledgeBase::Answer::Translation'
  828. result[name][:_source] = {
  829. excludes: ['attachment']
  830. }
  831. end
  832. return result if type_in_mapping?
  833. result[name]
  834. end
  835. # get es version
  836. def self.version
  837. @version ||= begin
  838. info = SearchIndexBackend.info
  839. number = nil
  840. if info.present?
  841. number = info['version']['number'].to_s
  842. end
  843. number
  844. end
  845. end
  846. def self.version_int
  847. number = version
  848. return 0 if !number
  849. number_split = version.split('.')
  850. "#{number_split[0]}#{format('%<minor>03d', minor: number_split[1])}#{format('%<patch>03d', patch: number_split[2])}".to_i
  851. end
  852. def self.version_supported?
  853. # only versions greater/equal than 6.5.0 are supported
  854. return if version_int < 6_005_000
  855. true
  856. end
  857. # no type in mapping
  858. def self.type_in_mapping?
  859. return true if version_int < 7_000_000
  860. false
  861. end
  862. # is es configured?
  863. def self.configured?
  864. return false if Setting.get('es_url').blank?
  865. true
  866. end
  867. def self.settings
  868. {
  869. 'index.mapping.total_fields.limit': 2000,
  870. }
  871. end
  872. def self.create_index(models = Models.indexable)
  873. models.each do |local_object|
  874. SearchIndexBackend.index(
  875. action: 'create',
  876. name: local_object.name,
  877. data: {
  878. mappings: SearchIndexBackend.get_mapping_properties_object(local_object),
  879. settings: SearchIndexBackend.settings,
  880. }
  881. )
  882. end
  883. end
  884. def self.drop_index(models = Models.indexable)
  885. models.each do |local_object|
  886. SearchIndexBackend.index(
  887. action: 'delete',
  888. name: local_object.name,
  889. )
  890. end
  891. end
  892. def self.create_object_index(object)
  893. models = Models.indexable.select { |c| c.to_s == object }
  894. create_index(models)
  895. end
  896. def self.drop_object_index(object)
  897. models = Models.indexable.select { |c| c.to_s == object }
  898. drop_index(models)
  899. end
  900. def self.pipeline(create: false)
  901. pipeline = Setting.get('es_pipeline')
  902. if create && pipeline.blank?
  903. pipeline = "zammad#{rand(999_999_999_999)}"
  904. Setting.set('es_pipeline', pipeline)
  905. end
  906. pipeline
  907. end
  908. def self.pipeline_settings
  909. {
  910. ignore_failure: true,
  911. ignore_missing: true,
  912. }
  913. end
  914. def self.create_pipeline
  915. SearchIndexBackend.processors(
  916. "_ingest/pipeline/#{pipeline(create: true)}": [
  917. {
  918. action: 'delete',
  919. },
  920. {
  921. action: 'create',
  922. description: 'Extract zammad-attachment information from arrays',
  923. processors: [
  924. {
  925. foreach: {
  926. field: 'article',
  927. processor: {
  928. foreach: {
  929. field: '_ingest._value.attachment',
  930. processor: {
  931. attachment: {
  932. target_field: '_ingest._value',
  933. field: '_ingest._value._content',
  934. }.merge(pipeline_settings),
  935. }
  936. }.merge(pipeline_settings),
  937. }
  938. }.merge(pipeline_settings),
  939. },
  940. {
  941. foreach: {
  942. field: 'attachment',
  943. processor: {
  944. attachment: {
  945. target_field: '_ingest._value',
  946. field: '_ingest._value._content',
  947. }.merge(pipeline_settings),
  948. }
  949. }.merge(pipeline_settings),
  950. }
  951. ]
  952. }
  953. ]
  954. )
  955. end
  956. def self.drop_pipeline
  957. return if pipeline.blank?
  958. SearchIndexBackend.processors(
  959. "_ingest/pipeline/#{pipeline}": [
  960. {
  961. action: 'delete',
  962. },
  963. ]
  964. )
  965. end
  966. end