search_index_backend.rb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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(:query_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, fulltext: false)
  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.exclude?('.') && 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 we have no fulltext search then the primary default sort is updated at else score
  292. if result.blank? && !fulltext
  293. result.push(
  294. updated_at: {
  295. order: 'desc',
  296. },
  297. )
  298. end
  299. result.push('_score')
  300. result
  301. end
  302. =begin
  303. get count of tickets and tickets which match on selector
  304. result = SearchIndexBackend.selectors(index, selector)
  305. example with a simple search:
  306. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } })
  307. result = [
  308. { id: 1, type: 'Ticket' },
  309. { id: 2, type: 'Ticket' },
  310. { id: 3, type: 'Ticket' },
  311. ]
  312. you also can get aggregations
  313. result = SearchIndexBackend.selectors(index, selector, options, aggs_interval)
  314. example for aggregations within one year
  315. aggs_interval = {
  316. from: '2015-01-01',
  317. to: '2015-12-31',
  318. interval: 'month', # year, quarter, month, week, day, hour, minute, second
  319. field: 'created_at',
  320. }
  321. options = {
  322. limit: 123,
  323. current_user: User.find(123),
  324. }
  325. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } }, options, aggs_interval)
  326. result = {
  327. hits:{
  328. total:4819,
  329. },
  330. aggregations:{
  331. time_buckets:{
  332. buckets:[
  333. {
  334. key_as_string:"2014-10-01T00:00:00.000Z",
  335. key:1412121600000,
  336. doc_count:420
  337. },
  338. {
  339. key_as_string:"2014-11-01T00:00:00.000Z",
  340. key:1414800000000,
  341. doc_count:561
  342. },
  343. ...
  344. ]
  345. }
  346. }
  347. }
  348. =end
  349. def self.selectors(index, selectors = nil, options = {}, aggs_interval = nil)
  350. raise 'no selectors given' if !selectors
  351. url = build_url(type: index, action: '_search', with_pipeline: false, with_document_type: false)
  352. return if url.blank?
  353. data = selector2query(selectors, options, aggs_interval)
  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. def self.selector2query(selector, options, aggs_interval)
  383. Ticket::Selector::SearchIndex.new(selector: selector, options: options.merge(aggs_interval: aggs_interval)).get
  384. end
  385. =begin
  386. return true if backend is configured
  387. result = SearchIndexBackend.enabled?
  388. =end
  389. def self.enabled?
  390. return false if Setting.get('es_url').blank?
  391. true
  392. end
  393. def self.build_index_name(index = nil)
  394. local_index = "#{Setting.get('es_index')}_#{Rails.env}"
  395. return local_index if index.blank?
  396. "#{local_index}_#{index.underscore.tr('/', '_')}"
  397. end
  398. =begin
  399. generate url for index or document access (only for internal use)
  400. # url to access single document in index (in case with_pipeline or not)
  401. url = SearchIndexBackend.build_url(type: 'User', object_id: 123, with_pipeline: true)
  402. # url to access whole index
  403. url = SearchIndexBackend.build_url(type: 'User')
  404. # url to access document definition in index (only es6 and higher)
  405. url = SearchIndexBackend.build_url(type: 'User', with_pipeline: false, with_document_type: true)
  406. # base url
  407. url = SearchIndexBackend.build_url
  408. =end
  409. def self.build_url(type: nil, action: nil, object_id: nil, with_pipeline: true, with_document_type: true, url_params: {})
  410. return if !SearchIndexBackend.enabled?
  411. # set index
  412. index = build_index_name(type)
  413. # add pipeline if needed
  414. if index && with_pipeline == true
  415. url_pipline = Setting.get('es_pipeline')
  416. if url_pipline.present?
  417. url_params['pipeline'] = url_pipline
  418. end
  419. end
  420. # prepare url params
  421. params_string = ''
  422. if url_params.present?
  423. params_string = "?#{URI.encode_www_form(url_params)}"
  424. end
  425. url = Setting.get('es_url')
  426. return "#{url}#{params_string}" if index.blank?
  427. # add type information
  428. url = "#{url}/#{index}"
  429. # add document type
  430. if with_document_type
  431. url = "#{url}/_doc"
  432. end
  433. # add action
  434. if action
  435. url = "#{url}/#{action}"
  436. end
  437. # add object id
  438. if object_id.present?
  439. url = "#{url}/#{object_id}"
  440. end
  441. "#{url}#{params_string}"
  442. end
  443. def self.humanized_error(verb:, url:, response:, payload: nil)
  444. prefix = "Unable to process #{verb} request to elasticsearch URL '#{url}'."
  445. suffix = "\n\nResponse:\n#{response.inspect}\n\n"
  446. if payload.respond_to?(:to_json)
  447. suffix += "Payload:\n#{payload.to_json}"
  448. suffix += "\n\nPayload size: #{payload.to_json.bytesize / 1024 / 1024}M"
  449. else
  450. suffix += "Payload:\n#{payload.inspect}"
  451. end
  452. message = if response&.error&.match?(__('Connection refused'))
  453. __("Elasticsearch is not reachable. It's possible that it's not running. Please check whether it is installed.")
  454. elsif url.end_with?('pipeline/zammad-attachment', 'pipeline=zammad-attachment') && response.code == 400
  455. __('The installed attachment plugin could not handle the request payload. Ensure that the correct attachment plugin is installed (ingest-attachment).')
  456. else
  457. __('Check the response and payload for detailed information:')
  458. end
  459. result = "#{prefix} #{message}#{suffix}"
  460. Rails.logger.error result.first(40_000)
  461. result
  462. end
  463. # add * on simple query like "somephrase23"
  464. def self.append_wildcard_to_simple_query(query)
  465. query = query.strip
  466. query += '*' if query.exclude?(':')
  467. query
  468. end
  469. =begin
  470. @param condition [Hash] search condition
  471. @param options [Hash] search options
  472. @option options [Integer] :from
  473. @option options [Integer] :limit
  474. @option options [Hash] :query_extension applied to ElasticSearch query
  475. @option options [Array<String>] :order_by ordering directions, desc or asc
  476. @option options [Array<String>] :sort_by fields to sort by
  477. @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.
  478. =end
  479. DEFAULT_QUERY_OPTIONS = {
  480. from: 0,
  481. limit: 10
  482. }.freeze
  483. def self.build_query(condition, options = {})
  484. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  485. data = {
  486. from: options[:from],
  487. size: options[:limit],
  488. sort: search_by_index_sort(sort_by: options[:sort_by], order_by: options[:order_by], fulltext: options[:fulltext]),
  489. query: {
  490. bool: {
  491. must: []
  492. }
  493. }
  494. }
  495. if (extension = options[:query_extension])
  496. data[:query].deep_merge! extension.deep_dup
  497. end
  498. data[:query][:bool][:must].push condition
  499. if options[:ids].present?
  500. data[:query][:bool][:must].push({ ids: { values: options[:ids] } })
  501. end
  502. data
  503. end
  504. =begin
  505. refreshes all indexes to make previous request data visible in future requests
  506. SearchIndexBackend.refresh
  507. =end
  508. def self.refresh
  509. return if !enabled?
  510. url = "#{Setting.get('es_url')}/_all/_refresh"
  511. make_request_and_validate(url, method: :post)
  512. end
  513. =begin
  514. helper method for making HTTP calls
  515. @param url [String] url
  516. @option params [Hash] :data is a payload hash
  517. @option params [Symbol] :method is a HTTP method
  518. @option params [Integer] :open_timeout is HTTP request open timeout
  519. @option params [Integer] :read_timeout is HTTP request read timeout
  520. @return UserAgent response
  521. =end
  522. def self.make_request(url, data: {}, method: :get, open_timeout: 8, read_timeout: 180)
  523. Rails.logger.debug { "# curl -X #{method} \"#{url}\" " }
  524. Rails.logger.debug { "-d '#{data.to_json}'" } if data.present?
  525. options = {
  526. json: true,
  527. open_timeout: open_timeout,
  528. read_timeout: read_timeout,
  529. total_timeout: (open_timeout + read_timeout + 60),
  530. open_socket_tries: 3,
  531. user: Setting.get('es_user'),
  532. password: Setting.get('es_password'),
  533. }
  534. response = UserAgent.send(method, url, data, options)
  535. Rails.logger.debug { "# #{response.code}" }
  536. response
  537. end
  538. =begin
  539. helper method for making HTTP calls and raising error if response was not success
  540. @param url [String] url
  541. @option args [Hash] see {make_request}
  542. @return [Boolean] always returns true. Raises error if something went wrong.
  543. =end
  544. def self.make_request_and_validate(url, **args)
  545. response = make_request(url, **args)
  546. return true if response.success?
  547. raise humanized_error(
  548. verb: args[:method],
  549. url: url,
  550. payload: args[:data],
  551. response: response
  552. )
  553. end
  554. =begin
  555. This function will return a index mapping based on the
  556. attributes of the database table of the existing object.
  557. mapping = SearchIndexBackend.get_mapping_properties_object(Ticket)
  558. Returns:
  559. mapping = {
  560. User: {
  561. properties: {
  562. firstname: {
  563. type: 'keyword',
  564. },
  565. }
  566. }
  567. }
  568. =end
  569. def self.get_mapping_properties_object(object)
  570. name = '_doc'
  571. result = {
  572. name => {
  573. properties: {}
  574. }
  575. }
  576. store_columns = %w[preferences data]
  577. # for elasticsearch 6.x and later
  578. string_type = 'text'
  579. string_raw = { type: 'keyword', ignore_above: 5012 }
  580. boolean_raw = { type: 'boolean' }
  581. object.columns_hash.each do |key, value|
  582. if value.type == :string && value.limit && value.limit <= 5000 && store_columns.exclude?(key)
  583. result[name][:properties][key] = {
  584. type: string_type,
  585. fields: {
  586. keyword: string_raw,
  587. }
  588. }
  589. elsif value.type == :integer
  590. result[name][:properties][key] = {
  591. type: 'integer',
  592. }
  593. elsif value.type == :datetime || value.type == :date
  594. result[name][:properties][key] = {
  595. type: 'date',
  596. }
  597. elsif value.type == :boolean
  598. result[name][:properties][key] = {
  599. type: 'boolean',
  600. fields: {
  601. keyword: boolean_raw,
  602. }
  603. }
  604. elsif value.type == :binary
  605. result[name][:properties][key] = {
  606. type: 'binary',
  607. }
  608. elsif value.type == :bigint
  609. result[name][:properties][key] = {
  610. type: 'long',
  611. }
  612. elsif value.type == :decimal
  613. result[name][:properties][key] = {
  614. type: 'float',
  615. }
  616. end
  617. end
  618. case object.name
  619. when 'Ticket'
  620. result[name][:properties][:article] = {
  621. type: 'nested',
  622. include_in_parent: true,
  623. }
  624. end
  625. result[name]
  626. end
  627. # get es version
  628. def self.version
  629. @version ||= SearchIndexBackend.info&.dig('version', 'number')
  630. end
  631. def self.configured?
  632. Setting.get('es_url').present?
  633. end
  634. def self.settings
  635. {
  636. 'index.mapping.total_fields.limit': 2000,
  637. }
  638. end
  639. def self.create_index(models = Models.indexable)
  640. models.each do |local_object|
  641. SearchIndexBackend.index(
  642. action: 'create',
  643. name: local_object.name,
  644. data: {
  645. mappings: SearchIndexBackend.get_mapping_properties_object(local_object),
  646. settings: SearchIndexBackend.settings,
  647. }
  648. )
  649. end
  650. end
  651. def self.drop_index(models = Models.indexable)
  652. models.each do |local_object|
  653. SearchIndexBackend.index(
  654. action: 'delete',
  655. name: local_object.name,
  656. )
  657. end
  658. end
  659. def self.create_object_index(object)
  660. models = Models.indexable.select { |c| c.to_s == object }
  661. create_index(models)
  662. end
  663. def self.drop_object_index(object)
  664. models = Models.indexable.select { |c| c.to_s == object }
  665. drop_index(models)
  666. end
  667. def self.pipeline(create: false)
  668. pipeline = Setting.get('es_pipeline')
  669. if create && pipeline.blank?
  670. pipeline = "zammad#{SecureRandom.uuid}"
  671. Setting.set('es_pipeline', pipeline)
  672. end
  673. pipeline
  674. end
  675. def self.pipeline_settings
  676. {
  677. ignore_failure: true,
  678. ignore_missing: true,
  679. }
  680. end
  681. def self.create_pipeline
  682. SearchIndexBackend.processors(
  683. "_ingest/pipeline/#{pipeline(create: true)}": [
  684. {
  685. action: 'delete',
  686. },
  687. {
  688. action: 'create',
  689. description: __('Extract zammad-attachment information from arrays'),
  690. processors: [
  691. {
  692. foreach: {
  693. field: 'article',
  694. processor: {
  695. foreach: {
  696. field: '_ingest._value.attachment',
  697. processor: {
  698. attachment: {
  699. target_field: '_ingest._value',
  700. field: '_ingest._value._content',
  701. }.merge(pipeline_settings),
  702. }
  703. }.merge(pipeline_settings),
  704. }
  705. }.merge(pipeline_settings),
  706. },
  707. {
  708. foreach: {
  709. field: 'attachment',
  710. processor: {
  711. attachment: {
  712. target_field: '_ingest._value',
  713. field: '_ingest._value._content',
  714. }.merge(pipeline_settings),
  715. }
  716. }.merge(pipeline_settings),
  717. }
  718. ]
  719. }
  720. ]
  721. )
  722. end
  723. def self.drop_pipeline
  724. return if pipeline.blank?
  725. SearchIndexBackend.processors(
  726. "_ingest/pipeline/#{pipeline}": [
  727. {
  728. action: 'delete',
  729. },
  730. ]
  731. )
  732. end
  733. end