search_index_backend.rb 33 KB

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