search_index_backend.rb 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. # Copyright (C) 2012-2024 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. It should get used in batches to prevent performance issues on entities which have millions of objects in it.
  137. data = {
  138. organization: {
  139. name: "Zammad Foundation"
  140. }
  141. }
  142. where = {
  143. term: {
  144. organization_id: 1
  145. }
  146. }
  147. SearchIndexBackend.update_by_query('Ticket', data, where)
  148. =end
  149. def self.update_by_query(type, data, where)
  150. return if data.blank?
  151. return if where.blank?
  152. url_params = {
  153. conflicts: 'proceed',
  154. slices: 'auto',
  155. max_docs: 1_000,
  156. }
  157. url = build_url(type: type, action: '_update_by_query', with_pipeline: false, with_document_type: false, url_params: url_params)
  158. return if url.blank?
  159. script_list = []
  160. data.each_key do |key|
  161. script_list.push("ctx._source.#{key}=params.#{key}")
  162. end
  163. data = {
  164. script: {
  165. lang: 'painless',
  166. source: script_list.join(';'),
  167. params: data,
  168. },
  169. query: where,
  170. sort: {
  171. id: 'desc',
  172. },
  173. }
  174. response = make_request(url, data: data, method: :post, read_timeout: 10.minutes)
  175. if !response.success?
  176. Rails.logger.error humanized_error(
  177. verb: 'GET',
  178. url: url,
  179. payload: data,
  180. response: response,
  181. )
  182. return []
  183. end
  184. response.data
  185. end
  186. =begin
  187. remove whole data from index
  188. SearchIndexBackend.remove('Ticket', 123)
  189. SearchIndexBackend.remove('Ticket')
  190. =end
  191. def self.remove(type, o_id = nil)
  192. url = if o_id
  193. build_url(type: type, object_id: o_id, with_pipeline: false, with_document_type: true)
  194. else
  195. build_url(type: type, object_id: o_id, with_pipeline: false, with_document_type: false)
  196. end
  197. return if url.blank?
  198. response = make_request(url, method: :delete)
  199. return true if response.success?
  200. return true if response.code.to_s == '400'
  201. humanized_error = humanized_error(
  202. verb: 'DELETE',
  203. url: url,
  204. response: response,
  205. )
  206. Rails.logger.warn "Can't delete index: #{humanized_error}"
  207. false
  208. end
  209. =begin
  210. @param query [String] search query
  211. @param index [String, Array<String>] indexes to search in (see search_by_index)
  212. @param options [Hash] search options (see build_query)
  213. @return search result
  214. @example Sample queries
  215. result = SearchIndexBackend.search('search query', ['User', 'Organization'], limit: limit)
  216. - result = SearchIndexBackend.search('search query', 'User', limit: limit)
  217. result = SearchIndexBackend.search('search query', 'User', limit: limit, sort_by: ['updated_at'], order_by: ['desc'])
  218. result = SearchIndexBackend.search('search query', 'User', limit: limit, sort_by: ['active', updated_at'], order_by: ['desc', 'desc'])
  219. result = [
  220. {
  221. :id => 123,
  222. :type => 'User',
  223. },
  224. {
  225. :id => 125,
  226. :type => 'User',
  227. },
  228. {
  229. :id => 15,
  230. :type => 'Organization',
  231. }
  232. ]
  233. =end
  234. def self.search(query, index, options = {})
  235. if options.key? :with_total_count
  236. raise 'Option "with_total_count" is not supported by multi-index search. Please use search_by_index instead.' # rubocop:disable Zammad/DetectTranslatableString
  237. end
  238. if !index.is_a? Array
  239. return search_by_index(query, index, options)
  240. end
  241. index
  242. .filter_map { |local_index| search_by_index(query, local_index, options) }
  243. .flatten(1)
  244. end
  245. =begin
  246. @param query [String] search query
  247. @param index [String] index name
  248. @param options [Hash] search options (see build_query)
  249. @return search result
  250. =end
  251. def self.search_by_index(query, index, options = {})
  252. return if query.blank?
  253. action = '_search'
  254. if options[:only_total_count].present?
  255. action = '_count'
  256. end
  257. url = build_url(type: index, action: action, with_pipeline: false, with_document_type: false)
  258. return if url.blank?
  259. # real search condition
  260. condition = {
  261. 'query_string' => {
  262. 'query' => append_wildcard_to_simple_query(query),
  263. 'time_zone' => Setting.get('timezone_default'),
  264. 'default_operator' => 'AND',
  265. 'analyze_wildcard' => true,
  266. }
  267. }
  268. if (fields = options.dig(:query_fields_by_indexes, index.to_sym))
  269. condition['query_string']['fields'] = fields
  270. end
  271. query_data = build_query(index, condition, options)
  272. if (fields = options.dig(:highlight_fields_by_indexes, index.to_sym)) && options[:only_total_count].blank?
  273. fields_for_highlight = fields.index_with { |_elem| {} }
  274. query_data[:highlight] = { fields: fields_for_highlight }
  275. end
  276. if options[:only_total_count].present?
  277. query_data.slice!(:query)
  278. end
  279. response = make_request(url, data: query_data, method: :post)
  280. if options[:only_total_count].present?
  281. return {
  282. total_count: response.data&.dig('count') || 0,
  283. }
  284. end
  285. data = if response.success?
  286. Array.wrap(response.data&.dig('hits', 'hits'))
  287. else
  288. Rails.logger.error humanized_error(
  289. verb: 'GET',
  290. url: url,
  291. payload: query_data,
  292. response: response,
  293. )
  294. []
  295. end
  296. data.map! do |item|
  297. Rails.logger.debug { "... #{item['_type']} #{item['_id']}" }
  298. output = {
  299. id: item['_id'],
  300. type: index,
  301. }
  302. if options.dig(:highlight_fields_by_indexes, index.to_sym)
  303. output[:highlight] = item['highlight']
  304. end
  305. output
  306. end
  307. if options[:with_total_count].present?
  308. return {
  309. total_count: response.data&.dig('hits', 'total', 'value') || 0,
  310. object_metadata: data,
  311. }
  312. end
  313. data
  314. end
  315. def self.search_by_index_sort(index:, sort_by: nil, order_by: nil, fulltext: false)
  316. result = (sort_by || [])
  317. .map(&:to_s)
  318. .each_with_object([])
  319. .with_index do |(elem, memo), idx|
  320. next if elem.blank?
  321. next if order_by&.at(idx).blank?
  322. # for sorting values use .keyword values (no analyzer is used - plain values)
  323. is_keyword = get_mapping_properties_object(Array.wrap(index).first.constantize).dig(:properties, elem, :fields, :keyword, :type) == 'keyword'
  324. if is_keyword
  325. elem += '.keyword'
  326. end
  327. memo.push(
  328. elem => {
  329. order: order_by[idx],
  330. },
  331. )
  332. end
  333. # if we have no fulltext search then the primary default sort is updated at else score
  334. if result.blank? && !fulltext
  335. result.push(
  336. updated_at: {
  337. order: 'desc',
  338. },
  339. )
  340. end
  341. result.push('_score')
  342. result
  343. end
  344. =begin
  345. get count of tickets and tickets which match on selector
  346. result = SearchIndexBackend.selectors(index, selector)
  347. example with a simple search:
  348. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } })
  349. result = [
  350. { id: 1, type: 'Ticket' },
  351. { id: 2, type: 'Ticket' },
  352. { id: 3, type: 'Ticket' },
  353. ]
  354. you also can get aggregations
  355. result = SearchIndexBackend.selectors(index, selector, options, aggs_interval)
  356. example for aggregations within one year
  357. aggs_interval = {
  358. from: '2015-01-01',
  359. to: '2015-12-31',
  360. interval: 'month', # year, quarter, month, week, day, hour, minute, second
  361. field: 'created_at',
  362. }
  363. options = {
  364. limit: 123,
  365. current_user: User.find(123),
  366. }
  367. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } }, options, aggs_interval)
  368. result = {
  369. hits:{
  370. total:4819,
  371. },
  372. aggregations:{
  373. time_buckets:{
  374. buckets:[
  375. {
  376. key_as_string:"2014-10-01T00:00:00.000Z",
  377. key:1412121600000,
  378. doc_count:420
  379. },
  380. {
  381. key_as_string:"2014-11-01T00:00:00.000Z",
  382. key:1414800000000,
  383. doc_count:561
  384. },
  385. ...
  386. ]
  387. }
  388. }
  389. }
  390. =end
  391. def self.selectors(index, selectors = nil, options = {}, aggs_interval = nil)
  392. raise 'no selectors given' if !selectors
  393. url = build_url(type: index, action: '_search', with_pipeline: false, with_document_type: false)
  394. return if url.blank?
  395. data = selector2query(index, selectors, options, aggs_interval)
  396. response = make_request(url, data: data, method: :post)
  397. with_interval = aggs_interval.present? && aggs_interval[:interval].present?
  398. if !response.success?
  399. # Work around a bug with ES versions <= 8.5.0, where invalid date range conditions caused an error response from the server.
  400. # https://github.com/zammad/zammad/issues/5105, https://github.com/elastic/elasticsearch/issues/88131
  401. # This can probably be removed when the required minimum ES version is >= 8.5.0.
  402. if with_interval && response.code.to_i == 400 && response.body&.include?('illegal_argument_exception')
  403. return fake_empty_es_aggregation_response
  404. end
  405. raise humanized_error(
  406. verb: 'GET',
  407. url: url,
  408. payload: data,
  409. response: response,
  410. )
  411. end
  412. Rails.logger.debug { response.data.to_json }
  413. if !with_interval
  414. object_ids = response.data['hits']['hits'].pluck('_id')
  415. # in lower ES 6 versions, we get total count directly, in higher
  416. # versions we need to pick it from total has
  417. count = response.data['hits']['total']
  418. if response.data['hits']['total'].class != Integer
  419. count = response.data['hits']['total']['value']
  420. end
  421. return {
  422. count: count,
  423. object_ids: object_ids,
  424. }
  425. end
  426. response.data
  427. end
  428. def self.selector2query(index, selector, options, aggs_interval)
  429. Selector::SearchIndex.new(selector: selector, options: options.merge(aggs_interval: aggs_interval), target_class: index.constantize).get
  430. end
  431. =begin
  432. return true if backend is configured
  433. result = SearchIndexBackend.enabled?
  434. =end
  435. def self.enabled?
  436. return false if Setting.get('es_url').blank?
  437. true
  438. end
  439. def self.build_index_name(index = nil)
  440. local_index = "#{Setting.get('es_index')}_#{Rails.env}"
  441. return local_index if index.blank?
  442. "#{local_index}_#{index.underscore.tr('/', '_')}"
  443. end
  444. =begin
  445. generate url for index or document access (only for internal use)
  446. # url to access single document in index (in case with_pipeline or not)
  447. url = SearchIndexBackend.build_url(type: 'User', object_id: 123, with_pipeline: true)
  448. # url to access whole index
  449. url = SearchIndexBackend.build_url(type: 'User')
  450. # url to access document definition in index (only es6 and higher)
  451. url = SearchIndexBackend.build_url(type: 'User', with_pipeline: false, with_document_type: true)
  452. # base url
  453. url = SearchIndexBackend.build_url
  454. =end
  455. def self.build_url(type: nil, action: nil, object_id: nil, with_pipeline: true, with_document_type: true, url_params: {})
  456. return if !SearchIndexBackend.enabled?
  457. # set index
  458. index = build_index_name(type)
  459. # add pipeline if needed
  460. if index && with_pipeline == true
  461. url_pipline = Setting.get('es_pipeline')
  462. if url_pipline.present?
  463. url_params['pipeline'] = url_pipline
  464. end
  465. end
  466. # prepare url params
  467. params_string = ''
  468. if url_params.present?
  469. params_string = "?#{URI.encode_www_form(url_params)}"
  470. end
  471. url = Setting.get('es_url')
  472. return "#{url}#{params_string}" if index.blank?
  473. # add type information
  474. url = "#{url}/#{index}"
  475. # add document type
  476. if with_document_type
  477. url = "#{url}/_doc"
  478. end
  479. # add action
  480. if action
  481. url = "#{url}/#{action}"
  482. end
  483. # add object id
  484. if object_id.present?
  485. url = "#{url}/#{object_id}"
  486. end
  487. "#{url}#{params_string}"
  488. end
  489. def self.humanized_error(verb:, url:, response:, payload: nil)
  490. prefix = "Unable to process #{verb} request to elasticsearch URL '#{url}'."
  491. suffix = "\n\nResponse:\n#{response.inspect}\n\n"
  492. if payload.respond_to?(:to_json)
  493. suffix += "Payload:\n#{payload.to_json}"
  494. suffix += "\n\nPayload size: #{payload.to_json.bytesize / 1024 / 1024}M"
  495. else
  496. suffix += "Payload:\n#{payload.inspect}"
  497. end
  498. message = if response&.error&.match?('Connection refused') # rubocop:disable Zammad/DetectTranslatableString
  499. __("Elasticsearch is not reachable. It's possible that it's not running. Please check whether it is installed.")
  500. elsif url.end_with?('pipeline/zammad-attachment', 'pipeline=zammad-attachment') && response.code == 400
  501. __('The installed attachment plugin could not handle the request payload. Ensure that the correct attachment plugin is installed (ingest-attachment).')
  502. else
  503. __('Check the response and payload for detailed information:')
  504. end
  505. result = "#{prefix} #{message}#{suffix}"
  506. Rails.logger.error result.first(40_000)
  507. result
  508. end
  509. # add * on simple query like "somephrase23"
  510. def self.append_wildcard_to_simple_query(query)
  511. query = query.strip
  512. query += '*' if query.exclude?(':')
  513. query
  514. end
  515. =begin
  516. @param condition [Hash] search condition
  517. @param options [Hash] search options
  518. @option options [Integer] :from
  519. @option options [Integer] :limit
  520. @option options [Hash] :query_extension applied to ElasticSearch query
  521. @option options [Array<String>] :order_by ordering directions, desc or asc
  522. @option options [Array<String>] :sort_by fields to sort by
  523. @option options [Array<String>] :fulltext If no sorting is defined the current fallback is the sorting by updated_at. But for fulltext searches it makes more sense to search by _score as default. This parameter allows to change to the fallback to _score.
  524. =end
  525. DEFAULT_QUERY_OPTIONS = {
  526. from: 0,
  527. limit: 10
  528. }.freeze
  529. def self.build_query(index, condition, options = {})
  530. options[:from] = options[:from].presence || options[:offset].presence
  531. options = DEFAULT_QUERY_OPTIONS.merge(options.compact_blank.deep_symbolize_keys)
  532. data = {
  533. from: options[:from],
  534. size: options[:limit],
  535. sort: search_by_index_sort(index: index, sort_by: options[:sort_by], order_by: options[:order_by], fulltext: options[:fulltext]),
  536. query: {
  537. bool: {
  538. must: [],
  539. must_not: [],
  540. }
  541. }
  542. }
  543. if (extension = options[:query_extension])
  544. data[:query].deep_merge! extension.deep_dup
  545. end
  546. data[:query][:bool][:must].push condition
  547. if options[:ids].present?
  548. data[:query][:bool][:must].push({ ids: { values: options[:ids] } })
  549. end
  550. if options[:condition].present?
  551. selector_query = SearchIndexBackend.selector2query(index, options[:condition], {}, nil)
  552. data[:query][:bool][:must] += Array.wrap(selector_query[:query][:bool][:must])
  553. data[:query][:bool][:must_not] += Array.wrap(selector_query[:query][:bool][:must_not])
  554. end
  555. data
  556. end
  557. =begin
  558. refreshes all indexes to make previous request data visible in future requests
  559. SearchIndexBackend.refresh
  560. =end
  561. def self.refresh
  562. return if !enabled?
  563. url = "#{Setting.get('es_url')}/_all/_refresh"
  564. make_request_and_validate(url, method: :post)
  565. end
  566. =begin
  567. helper method for making HTTP calls
  568. @param url [String] url
  569. @option params [Hash] :data is a payload hash
  570. @option params [Symbol] :method is a HTTP method
  571. @option params [Integer] :open_timeout is HTTP request open timeout
  572. @option params [Integer] :read_timeout is HTTP request read timeout
  573. @return UserAgent response
  574. =end
  575. def self.make_request(url, data: {}, method: :get, open_timeout: 8, read_timeout: 180)
  576. Rails.logger.debug { "# curl -X #{method} \"#{url}\" " }
  577. Rails.logger.debug { "-d '#{data.to_json}'" } if data.present?
  578. options = {
  579. json: true,
  580. open_timeout: open_timeout,
  581. read_timeout: read_timeout,
  582. total_timeout: (open_timeout + read_timeout + 60),
  583. open_socket_tries: 3,
  584. user: Setting.get('es_user'),
  585. password: Setting.get('es_password'),
  586. verify_ssl: Setting.get('es_ssl_verify'),
  587. }
  588. response = UserAgent.send(method, url, data, options)
  589. Rails.logger.debug { "# #{response.code}" }
  590. response
  591. end
  592. =begin
  593. helper method for making HTTP calls and raising error if response was not success
  594. @param url [String] url
  595. @option args [Hash] see {make_request}
  596. @return [Boolean] always returns true. Raises error if something went wrong.
  597. =end
  598. def self.make_request_and_validate(url, **args)
  599. response = make_request(url, **args)
  600. return true if response.success?
  601. raise humanized_error(
  602. verb: args[:method],
  603. url: url,
  604. payload: args[:data],
  605. response: response
  606. )
  607. end
  608. =begin
  609. This function will return a index mapping based on the
  610. attributes of the database table of the existing object.
  611. mapping = SearchIndexBackend.get_mapping_properties_object(Ticket)
  612. Returns:
  613. mapping = {
  614. User: {
  615. properties: {
  616. firstname: {
  617. type: 'keyword',
  618. },
  619. }
  620. }
  621. }
  622. =end
  623. def self.get_mapping_properties_object(object)
  624. result = {
  625. properties: {}
  626. }
  627. store_columns = %w[preferences data]
  628. # for elasticsearch 6.x and later
  629. string_type = 'text'
  630. string_raw = { type: 'keyword', ignore_above: 5012 }
  631. boolean_raw = { type: 'boolean' }
  632. object.columns_hash.each do |key, value|
  633. if value.type == :string && value.limit && value.limit <= 5000 && store_columns.exclude?(key)
  634. result[:properties][key] = {
  635. type: string_type,
  636. fields: {
  637. keyword: string_raw,
  638. }
  639. }
  640. elsif value.type == :integer
  641. result[:properties][key] = {
  642. type: 'integer',
  643. }
  644. elsif value.type == :datetime || value.type == :date
  645. result[:properties][key] = {
  646. type: 'date',
  647. }
  648. elsif value.type == :boolean
  649. result[:properties][key] = {
  650. type: 'boolean',
  651. fields: {
  652. keyword: boolean_raw,
  653. }
  654. }
  655. elsif value.type == :binary
  656. result[:properties][key] = {
  657. type: 'binary',
  658. }
  659. elsif value.type == :bigint
  660. result[:properties][key] = {
  661. type: 'long',
  662. }
  663. elsif value.type == :decimal
  664. result[:properties][key] = {
  665. type: 'float',
  666. }
  667. end
  668. end
  669. case object.name
  670. when 'Ticket'
  671. result[:properties][:article] = {
  672. type: 'nested',
  673. include_in_parent: true,
  674. }
  675. end
  676. result
  677. end
  678. # get es version
  679. def self.version
  680. @version ||= SearchIndexBackend.info&.dig('version', 'number')
  681. end
  682. def self.configured?
  683. Setting.get('es_url').present?
  684. end
  685. def self.model_indexable?(model_name)
  686. Models.indexable.any? { |m| m.name == model_name }
  687. end
  688. def self.default_model_settings
  689. {
  690. 'index.mapping.total_fields.limit' => 2000,
  691. }
  692. end
  693. def self.model_settings(model)
  694. settings = Setting.get('es_model_settings')[model.name] || {}
  695. default_model_settings.merge(settings)
  696. end
  697. def self.all_settings
  698. Models.indexable.each_with_object({}).to_h { |m| [m.name, model_settings(m)] }
  699. end
  700. def self.set_setting(model_name, key, value)
  701. raise "It is not possible to configure settings for the non-indexable model '#{model_name}'." if !model_indexable?(model_name)
  702. raise __("The required parameter 'key' is missing.") if key.blank?
  703. raise __("The required parameter 'value' is missing.") if value.blank?
  704. config = Setting.get('es_model_settings')
  705. config[model_name] ||= {}
  706. config[model_name][key] = value
  707. Setting.set('es_model_settings', config)
  708. end
  709. def self.unset_setting(model_name, key)
  710. raise "It is not possible to configure settings for the non-indexable model '#{model_name}'." if !model_indexable?(model_name)
  711. raise __("The required parameter 'key' is missing.") if key.blank?
  712. config = Setting.get('es_model_settings')
  713. config[model_name] ||= {}
  714. config[model_name].delete(key)
  715. Setting.set('es_model_settings', config)
  716. end
  717. def self.create_index(models = Models.indexable)
  718. models.each do |local_object|
  719. SearchIndexBackend.index(
  720. action: 'create',
  721. name: local_object.name,
  722. data: {
  723. mappings: SearchIndexBackend.get_mapping_properties_object(local_object),
  724. settings: model_settings(local_object),
  725. }
  726. )
  727. end
  728. end
  729. def self.drop_index(models = Models.indexable)
  730. models.each do |local_object|
  731. SearchIndexBackend.index(
  732. action: 'delete',
  733. name: local_object.name,
  734. )
  735. end
  736. end
  737. def self.create_object_index(object)
  738. models = Models.indexable.select { |c| c.to_s == object }
  739. create_index(models)
  740. end
  741. def self.drop_object_index(object)
  742. models = Models.indexable.select { |c| c.to_s == object }
  743. drop_index(models)
  744. end
  745. def self.pipeline(create: false)
  746. pipeline = Setting.get('es_pipeline')
  747. if create && pipeline.blank?
  748. pipeline = "zammad#{SecureRandom.uuid}"
  749. Setting.set('es_pipeline', pipeline)
  750. end
  751. pipeline
  752. end
  753. def self.pipeline_settings
  754. {
  755. ignore_failure: true,
  756. ignore_missing: true,
  757. }
  758. end
  759. def self.create_pipeline
  760. SearchIndexBackend.processors(
  761. "_ingest/pipeline/#{pipeline(create: true)}": [
  762. {
  763. action: 'delete',
  764. },
  765. {
  766. action: 'create',
  767. description: __('Extract zammad-attachment information from arrays'),
  768. processors: [
  769. {
  770. foreach: {
  771. field: 'article',
  772. processor: {
  773. foreach: {
  774. field: '_ingest._value.attachment',
  775. processor: {
  776. attachment: {
  777. target_field: '_ingest._value',
  778. field: '_ingest._value._content',
  779. }.merge(pipeline_settings),
  780. }
  781. }.merge(pipeline_settings),
  782. }
  783. }.merge(pipeline_settings),
  784. },
  785. {
  786. foreach: {
  787. field: 'attachment',
  788. processor: {
  789. attachment: {
  790. target_field: '_ingest._value',
  791. field: '_ingest._value._content',
  792. }.merge(pipeline_settings),
  793. }
  794. }.merge(pipeline_settings),
  795. }
  796. ]
  797. }
  798. ]
  799. )
  800. end
  801. def self.drop_pipeline
  802. return if pipeline.blank?
  803. SearchIndexBackend.processors(
  804. "_ingest/pipeline/#{pipeline}": [
  805. {
  806. action: 'delete',
  807. },
  808. ]
  809. )
  810. end
  811. # Simulate an empty response from ES.
  812. def self.fake_empty_es_aggregation_response
  813. {
  814. 'hits' => { 'total' => { 'value' => 0, 'relation' => 'eq' }, 'max_score' => nil, 'hits' => [] },
  815. 'aggregations' => { 'time_buckets' => { 'buckets' => [] } }
  816. }
  817. end
  818. end