search_index_backend.rb 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. # Copyright (C) 2012-2023 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_sanitized'),
  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. .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. def self.build_url(type: nil, action: nil, object_id: nil, with_pipeline: true, with_document_type: true, url_params: {})
  637. return if !SearchIndexBackend.enabled?
  638. # set index
  639. index = build_index_name(type)
  640. # add pipeline if needed
  641. if index && with_pipeline == true
  642. url_pipline = Setting.get('es_pipeline')
  643. if url_pipline.present?
  644. url_params['pipeline'] = url_pipline
  645. end
  646. end
  647. # prepare url params
  648. params_string = ''
  649. if url_params.present?
  650. params_string = "?#{URI.encode_www_form(url_params)}"
  651. end
  652. url = Setting.get('es_url')
  653. return "#{url}#{params_string}" if index.blank?
  654. # add type information
  655. url = "#{url}/#{index}"
  656. # add document type
  657. if with_document_type
  658. url = "#{url}/_doc"
  659. end
  660. # add action
  661. if action
  662. url = "#{url}/#{action}"
  663. end
  664. # add object id
  665. if object_id.present?
  666. url = "#{url}/#{object_id}"
  667. end
  668. "#{url}#{params_string}"
  669. end
  670. def self.humanized_error(verb:, url:, response:, payload: nil)
  671. prefix = "Unable to process #{verb} request to elasticsearch URL '#{url}'."
  672. suffix = "\n\nResponse:\n#{response.inspect}\n\n"
  673. if payload.respond_to?(:to_json)
  674. suffix += "Payload:\n#{payload.to_json}"
  675. suffix += "\n\nPayload size: #{payload.to_json.bytesize / 1024 / 1024}M"
  676. else
  677. suffix += "Payload:\n#{payload.inspect}"
  678. end
  679. message = if response&.error&.match?(__('Connection refused'))
  680. __("Elasticsearch is not reachable. It's possible that it's not running. Please check whether it is installed.")
  681. elsif url.end_with?('pipeline/zammad-attachment', 'pipeline=zammad-attachment') && response.code == 400
  682. __('The installed attachment plugin could not handle the request payload. Ensure that the correct attachment plugin is installed (ingest-attachment).')
  683. else
  684. __('Check the response and payload for detailed information:')
  685. end
  686. result = "#{prefix} #{message}#{suffix}"
  687. Rails.logger.error result.first(40_000)
  688. result
  689. end
  690. # add * on simple query like "somephrase23"
  691. def self.append_wildcard_to_simple_query(query)
  692. query = query.strip
  693. query += '*' if query.exclude?(':')
  694. query
  695. end
  696. =begin
  697. @param condition [Hash] search condition
  698. @param options [Hash] search options
  699. @option options [Integer] :from
  700. @option options [Integer] :limit
  701. @option options [Hash] :query_extension applied to ElasticSearch query
  702. @option options [Array<String>] :order_by ordering directions, desc or asc
  703. @option options [Array<String>] :sort_by fields to sort by
  704. =end
  705. DEFAULT_QUERY_OPTIONS = {
  706. from: 0,
  707. limit: 10
  708. }.freeze
  709. def self.build_query(condition, options = {})
  710. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  711. data = {
  712. from: options[:from],
  713. size: options[:limit],
  714. sort: search_by_index_sort(options[:sort_by], options[:order_by]),
  715. query: {
  716. bool: {
  717. must: []
  718. }
  719. }
  720. }
  721. if (extension = options[:query_extension])
  722. data[:query].deep_merge! extension.deep_dup
  723. end
  724. data[:query][:bool][:must].push condition
  725. if options[:ids].present?
  726. data[:query][:bool][:must].push({ ids: { values: options[:ids] } })
  727. end
  728. data
  729. end
  730. =begin
  731. refreshes all indexes to make previous request data visible in future requests
  732. SearchIndexBackend.refresh
  733. =end
  734. def self.refresh
  735. return if !enabled?
  736. url = "#{Setting.get('es_url')}/_all/_refresh"
  737. make_request_and_validate(url, method: :post)
  738. end
  739. =begin
  740. helper method for making HTTP calls
  741. @param url [String] url
  742. @option params [Hash] :data is a payload hash
  743. @option params [Symbol] :method is a HTTP method
  744. @option params [Integer] :open_timeout is HTTP request open timeout
  745. @option params [Integer] :read_timeout is HTTP request read timeout
  746. @return UserAgent response
  747. =end
  748. def self.make_request(url, data: {}, method: :get, open_timeout: 8, read_timeout: 180)
  749. Rails.logger.debug { "# curl -X #{method} \"#{url}\" " }
  750. Rails.logger.debug { "-d '#{data.to_json}'" } if data.present?
  751. options = {
  752. json: true,
  753. open_timeout: open_timeout,
  754. read_timeout: read_timeout,
  755. total_timeout: (open_timeout + read_timeout + 60),
  756. open_socket_tries: 3,
  757. user: Setting.get('es_user'),
  758. password: Setting.get('es_password'),
  759. }
  760. response = UserAgent.send(method, url, data, options)
  761. Rails.logger.debug { "# #{response.code}" }
  762. response
  763. end
  764. =begin
  765. helper method for making HTTP calls and raising error if response was not success
  766. @param url [String] url
  767. @option args [Hash] see {make_request}
  768. @return [Boolean] always returns true. Raises error if something went wrong.
  769. =end
  770. def self.make_request_and_validate(url, **args)
  771. response = make_request(url, **args)
  772. return true if response.success?
  773. raise humanized_error(
  774. verb: args[:method],
  775. url: url,
  776. payload: args[:data],
  777. response: response
  778. )
  779. end
  780. =begin
  781. This function will return a index mapping based on the
  782. attributes of the database table of the existing object.
  783. mapping = SearchIndexBackend.get_mapping_properties_object(Ticket)
  784. Returns:
  785. mapping = {
  786. User: {
  787. properties: {
  788. firstname: {
  789. type: 'keyword',
  790. },
  791. }
  792. }
  793. }
  794. =end
  795. def self.get_mapping_properties_object(object)
  796. name = '_doc'
  797. result = {
  798. name => {
  799. properties: {}
  800. }
  801. }
  802. store_columns = %w[preferences data]
  803. # for elasticsearch 6.x and later
  804. string_type = 'text'
  805. string_raw = { type: 'keyword', ignore_above: 5012 }
  806. boolean_raw = { type: 'boolean' }
  807. object.columns_hash.each do |key, value|
  808. if value.type == :string && value.limit && value.limit <= 5000 && store_columns.exclude?(key)
  809. result[name][:properties][key] = {
  810. type: string_type,
  811. fields: {
  812. keyword: string_raw,
  813. }
  814. }
  815. elsif value.type == :integer
  816. result[name][:properties][key] = {
  817. type: 'integer',
  818. }
  819. elsif value.type == :datetime || value.type == :date
  820. result[name][:properties][key] = {
  821. type: 'date',
  822. }
  823. elsif value.type == :boolean
  824. result[name][:properties][key] = {
  825. type: 'boolean',
  826. fields: {
  827. keyword: boolean_raw,
  828. }
  829. }
  830. elsif value.type == :binary
  831. result[name][:properties][key] = {
  832. type: 'binary',
  833. }
  834. elsif value.type == :bigint
  835. result[name][:properties][key] = {
  836. type: 'long',
  837. }
  838. elsif value.type == :decimal
  839. result[name][:properties][key] = {
  840. type: 'float',
  841. }
  842. end
  843. end
  844. case object.name
  845. when 'Ticket'
  846. result[name][:properties][:article] = {
  847. type: 'nested',
  848. include_in_parent: true,
  849. }
  850. end
  851. result[name]
  852. end
  853. # get es version
  854. def self.version
  855. @version ||= SearchIndexBackend.info&.dig('version', 'number')
  856. end
  857. def self.configured?
  858. Setting.get('es_url').present?
  859. end
  860. def self.settings
  861. {
  862. 'index.mapping.total_fields.limit': 2000,
  863. }
  864. end
  865. def self.create_index(models = Models.indexable)
  866. models.each do |local_object|
  867. SearchIndexBackend.index(
  868. action: 'create',
  869. name: local_object.name,
  870. data: {
  871. mappings: SearchIndexBackend.get_mapping_properties_object(local_object),
  872. settings: SearchIndexBackend.settings,
  873. }
  874. )
  875. end
  876. end
  877. def self.drop_index(models = Models.indexable)
  878. models.each do |local_object|
  879. SearchIndexBackend.index(
  880. action: 'delete',
  881. name: local_object.name,
  882. )
  883. end
  884. end
  885. def self.create_object_index(object)
  886. models = Models.indexable.select { |c| c.to_s == object }
  887. create_index(models)
  888. end
  889. def self.drop_object_index(object)
  890. models = Models.indexable.select { |c| c.to_s == object }
  891. drop_index(models)
  892. end
  893. def self.pipeline(create: false)
  894. pipeline = Setting.get('es_pipeline')
  895. if create && pipeline.blank?
  896. pipeline = "zammad#{SecureRandom.uuid}"
  897. Setting.set('es_pipeline', pipeline)
  898. end
  899. pipeline
  900. end
  901. def self.pipeline_settings
  902. {
  903. ignore_failure: true,
  904. ignore_missing: true,
  905. }
  906. end
  907. def self.create_pipeline
  908. SearchIndexBackend.processors(
  909. "_ingest/pipeline/#{pipeline(create: true)}": [
  910. {
  911. action: 'delete',
  912. },
  913. {
  914. action: 'create',
  915. description: __('Extract zammad-attachment information from arrays'),
  916. processors: [
  917. {
  918. foreach: {
  919. field: 'article',
  920. processor: {
  921. foreach: {
  922. field: '_ingest._value.attachment',
  923. processor: {
  924. attachment: {
  925. target_field: '_ingest._value',
  926. field: '_ingest._value._content',
  927. }.merge(pipeline_settings),
  928. }
  929. }.merge(pipeline_settings),
  930. }
  931. }.merge(pipeline_settings),
  932. },
  933. {
  934. foreach: {
  935. field: 'attachment',
  936. processor: {
  937. attachment: {
  938. target_field: '_ingest._value',
  939. field: '_ingest._value._content',
  940. }.merge(pipeline_settings),
  941. }
  942. }.merge(pipeline_settings),
  943. }
  944. ]
  945. }
  946. ]
  947. )
  948. end
  949. def self.drop_pipeline
  950. return if pipeline.blank?
  951. SearchIndexBackend.processors(
  952. "_ingest/pipeline/#{pipeline}": [
  953. {
  954. action: 'delete',
  955. },
  956. ]
  957. )
  958. end
  959. # verifies date range ElasticSearch payload
  960. #
  961. # @param url [String] of ElasticSearch
  962. # @param payload [Hash] Elasticsearch query payload
  963. #
  964. # @return [Boolean] or raises error
  965. def self.verify_date_range(url, payload)
  966. ranges_payload = payload.dig(:query, :bool, :must)
  967. return true if ranges_payload.nil?
  968. ranges = ranges_payload
  969. .select { |elem| elem.key? :range }
  970. .map { |elem| [elem[:range].keys.first, convert_es_date_range(elem)] }
  971. .each_with_object({}) { |elem, sum| (sum[elem.first] ||= []) << elem.last }
  972. return true if ranges.all? { |_, ranges_by_key| verify_single_key_range(ranges_by_key) }
  973. error_prefix = "Unable to process request to elasticsearch URL '#{url}'."
  974. error_suffix = "Payload:\n#{payload.to_json}"
  975. error_message = __('Conflicting date ranges')
  976. result = "#{error_prefix} #{error_message} #{error_suffix}"
  977. Rails.logger.error result.first(40_000)
  978. raise result
  979. end
  980. # checks if all ranges are overlaping
  981. #
  982. # @param ranges [Array<Range<DateTime>>] to use in search
  983. #
  984. # @return [Boolean]
  985. def self.verify_single_key_range(ranges)
  986. ranges
  987. .each_with_index
  988. .all? do |range, i|
  989. ranges
  990. .slice((i + 1)..)
  991. .all? { |elem| elem.overlaps? range }
  992. end
  993. end
  994. # Converts paylaod component to dates range
  995. #
  996. # @param elem [Hash] payload component
  997. #
  998. # @return [Range<DateTime>]
  999. def self.convert_es_date_range(elem)
  1000. range = elem[:range].first.last
  1001. from = parse_es_range_date range[:from] || range[:gt] || '-9999-01-01'
  1002. to = parse_es_range_date range[:to] || range[:lt] || '9999-01-01'
  1003. from..to
  1004. end
  1005. # Parses absolute date or converts relative date
  1006. #
  1007. # @param input [String] string representation of date
  1008. #
  1009. # @return [Range<DateTime>]
  1010. def self.parse_es_range_date(input)
  1011. match = input.match(%r{^now(-|\+)(\d+)(\w{1})$})
  1012. return DateTime.parse input if !match
  1013. map = {
  1014. d: 'day',
  1015. y: 'year',
  1016. M: 'month',
  1017. h: 'hour',
  1018. m: 'minute',
  1019. }
  1020. range = match.captures[1].to_i.send map[match.captures[2].to_sym]
  1021. case match.captures[0]
  1022. when '-'
  1023. range.ago
  1024. when '+'
  1025. range.from_now
  1026. end
  1027. end
  1028. end