search_index_backend.rb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class SearchIndexBackend
  3. =begin
  4. info about used search index machine
  5. SearchIndexBackend.info
  6. =end
  7. def self.info
  8. url = Setting.get('es_url').to_s
  9. return if url.blank?
  10. response = make_request(url)
  11. if response.success?
  12. installed_version = response.data.dig('version', 'number')
  13. raise "Unable to get elasticsearch version from response: #{response.inspect}" if installed_version.blank?
  14. version_supported = Gem::Version.new(installed_version) < Gem::Version.new('8')
  15. raise "Version #{installed_version} of configured elasticsearch is not supported." if !version_supported
  16. version_supported = Gem::Version.new(installed_version) > Gem::Version.new('2.3')
  17. raise "Version #{installed_version} of configured elasticsearch is not supported." if !version_supported
  18. return response.data
  19. end
  20. raise humanized_error(
  21. verb: 'GET',
  22. url: url,
  23. response: response,
  24. )
  25. end
  26. =begin
  27. update processors
  28. SearchIndexBackend.processors(
  29. _ingest/pipeline/attachment: {
  30. description: 'Extract attachment information from arrays',
  31. processors: [
  32. {
  33. foreach: {
  34. field: 'ticket.articles.attachments',
  35. processor: {
  36. attachment: {
  37. target_field: '_ingest._value.attachment',
  38. field: '_ingest._value.data'
  39. }
  40. }
  41. }
  42. }
  43. ]
  44. }
  45. )
  46. =end
  47. def self.processors(data)
  48. data.each do |key, items|
  49. url = "#{Setting.get('es_url')}/#{key}"
  50. items.each do |item|
  51. if item[:action] == 'delete'
  52. response = make_request(url, method: :delete)
  53. next if response.success?
  54. next if response.code.to_s == '404'
  55. raise humanized_error(
  56. verb: 'DELETE',
  57. url: url,
  58. response: response,
  59. )
  60. end
  61. item.delete(:action)
  62. make_request_and_validate(url, data: item, method: :put)
  63. end
  64. end
  65. true
  66. end
  67. =begin
  68. create/update/delete index
  69. SearchIndexBackend.index(
  70. :action => 'create', # create/update/delete
  71. :name => 'Ticket',
  72. :data => {
  73. :mappings => {
  74. :Ticket => {
  75. :properties => {
  76. :articles => {
  77. :type => 'nested',
  78. :properties => {
  79. 'attachment' => { :type => 'attachment' }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. )
  87. SearchIndexBackend.index(
  88. :action => 'delete', # create/update/delete
  89. :name => 'Ticket',
  90. )
  91. =end
  92. def self.index(data)
  93. url = build_url(type: data[:name], with_pipeline: false, with_document_type: false)
  94. return if url.blank?
  95. if data[:action] && data[:action] == 'delete'
  96. return SearchIndexBackend.remove(data[:name])
  97. end
  98. make_request_and_validate(url, data: data[:data], method: :put)
  99. end
  100. =begin
  101. add new object to search index
  102. SearchIndexBackend.add('Ticket', some_data_object)
  103. =end
  104. def self.add(type, data)
  105. url = build_url(type: type, object_id: data['id'])
  106. return if url.blank?
  107. make_request_and_validate(url, data: data, method: :post)
  108. end
  109. =begin
  110. This function updates specifc attributes of an index based on a query.
  111. data = {
  112. organization: {
  113. name: "Zammad Foundation"
  114. }
  115. }
  116. where = {
  117. organization_id: 1
  118. }
  119. SearchIndexBackend.update_by_query('Ticket', data, where)
  120. =end
  121. def self.update_by_query(type, data, where)
  122. return if data.blank?
  123. return if where.blank?
  124. url = build_url(type: type, action: '_update_by_query', with_pipeline: false, with_document_type: false, url_params: { conflicts: 'proceed' })
  125. return if url.blank?
  126. script_list = []
  127. data.each do |key, _value|
  128. script_list.push("ctx._source.#{key}=params.#{key}")
  129. end
  130. data = {
  131. script: {
  132. lang: 'painless',
  133. source: script_list.join(';'),
  134. params: data,
  135. },
  136. query: {
  137. term: where,
  138. },
  139. }
  140. make_request_and_validate(url, data: data, method: :post, read_timeout: 10.minutes)
  141. end
  142. =begin
  143. remove whole data from index
  144. SearchIndexBackend.remove('Ticket', 123)
  145. SearchIndexBackend.remove('Ticket')
  146. =end
  147. def self.remove(type, o_id = nil)
  148. url = if o_id
  149. build_url(type: type, object_id: o_id, with_pipeline: false, with_document_type: true)
  150. else
  151. build_url(type: type, object_id: o_id, with_pipeline: false, with_document_type: false)
  152. end
  153. return if url.blank?
  154. response = make_request(url, method: :delete)
  155. return true if response.success?
  156. return true if response.code.to_s == '400'
  157. humanized_error = humanized_error(
  158. verb: 'DELETE',
  159. url: url,
  160. response: response,
  161. )
  162. Rails.logger.warn "Can't delete index: #{humanized_error}"
  163. false
  164. end
  165. =begin
  166. @param query [String] search query
  167. @param index [String, Array<String>] indexes to search in (see search_by_index)
  168. @param options [Hash] search options (see build_query)
  169. @return search result
  170. @example Sample queries
  171. result = SearchIndexBackend.search('search query', ['User', 'Organization'], limit: limit)
  172. - result = SearchIndexBackend.search('search query', 'User', limit: limit)
  173. result = SearchIndexBackend.search('search query', 'User', limit: limit, sort_by: ['updated_at'], order_by: ['desc'])
  174. result = SearchIndexBackend.search('search query', 'User', limit: limit, sort_by: ['active', updated_at'], order_by: ['desc', 'desc'])
  175. result = [
  176. {
  177. :id => 123,
  178. :type => 'User',
  179. },
  180. {
  181. :id => 125,
  182. :type => 'User',
  183. },
  184. {
  185. :id => 15,
  186. :type => 'Organization',
  187. }
  188. ]
  189. =end
  190. def self.search(query, index, options = {})
  191. if !index.is_a? Array
  192. return search_by_index(query, index, options)
  193. end
  194. index
  195. .map { |local_index| search_by_index(query, local_index, options) }
  196. .compact
  197. .flatten(1)
  198. end
  199. =begin
  200. @param query [String] search query
  201. @param index [String] index name
  202. @param options [Hash] search options (see build_query)
  203. @return search result
  204. =end
  205. def self.search_by_index(query, index, options = {})
  206. return [] if query.blank?
  207. url = build_url(type: index, action: '_search', with_pipeline: false, with_document_type: true)
  208. return [] if url.blank?
  209. # real search condition
  210. condition = {
  211. 'query_string' => {
  212. 'query' => append_wildcard_to_simple_query(query),
  213. 'default_operator' => 'AND',
  214. 'analyze_wildcard' => true,
  215. }
  216. }
  217. if (fields = options.dig(:highlight_fields_by_indexes, index.to_sym))
  218. condition['query_string']['fields'] = fields
  219. end
  220. query_data = build_query(condition, options)
  221. if (fields = options.dig(:highlight_fields_by_indexes, index.to_sym))
  222. fields_for_highlight = fields.each_with_object({}) { |elem, memo| memo[elem] = {} }
  223. query_data[:highlight] = { fields: fields_for_highlight }
  224. end
  225. response = make_request(url, data: query_data)
  226. if !response.success?
  227. Rails.logger.error humanized_error(
  228. verb: 'GET',
  229. url: url,
  230. payload: query_data,
  231. response: response,
  232. )
  233. return []
  234. end
  235. data = response.data&.dig('hits', 'hits')
  236. return [] if !data
  237. data.map do |item|
  238. Rails.logger.info "... #{item['_type']} #{item['_id']}"
  239. output = {
  240. id: item['_id'],
  241. type: index,
  242. }
  243. if options.dig(:highlight_fields_by_indexes, index.to_sym)
  244. output[:highlight] = item['highlight']
  245. end
  246. output
  247. end
  248. end
  249. def self.search_by_index_sort(sort_by = nil, order_by = nil)
  250. result = []
  251. sort_by&.each_with_index do |value, index|
  252. next if value.blank?
  253. next if order_by&.at(index).blank?
  254. # for sorting values use .keyword values (no analyzer is used - plain values)
  255. if value !~ /\./ && value !~ /_(time|date|till|id|ids|at)$/
  256. value += '.keyword'
  257. end
  258. result.push(
  259. value => {
  260. order: order_by[index],
  261. },
  262. )
  263. end
  264. if result.blank?
  265. result.push(
  266. updated_at: {
  267. order: 'desc',
  268. },
  269. )
  270. end
  271. result.push('_score')
  272. result
  273. end
  274. =begin
  275. get count of tickets and tickets which match on selector
  276. result = SearchIndexBackend.selectors(index, selector)
  277. example with a simple search:
  278. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } })
  279. result = [
  280. { id: 1, type: 'Ticket' },
  281. { id: 2, type: 'Ticket' },
  282. { id: 3, type: 'Ticket' },
  283. ]
  284. you also can get aggregations
  285. result = SearchIndexBackend.selectors(index, selector, options, aggs_interval)
  286. example for aggregations within one year
  287. aggs_interval = {
  288. from: '2015-01-01',
  289. to: '2015-12-31',
  290. interval: 'month', # year, quarter, month, week, day, hour, minute, second
  291. field: 'created_at',
  292. }
  293. options = {
  294. limit: 123,
  295. current_user: User.find(123),
  296. }
  297. result = SearchIndexBackend.selectors('Ticket', { 'category' => { 'operator' => 'is', 'value' => 'aa::ab' } }, options, aggs_interval)
  298. result = {
  299. hits:{
  300. total:4819,
  301. },
  302. aggregations:{
  303. time_buckets:{
  304. buckets:[
  305. {
  306. key_as_string:"2014-10-01T00:00:00.000Z",
  307. key:1412121600000,
  308. doc_count:420
  309. },
  310. {
  311. key_as_string:"2014-11-01T00:00:00.000Z",
  312. key:1414800000000,
  313. doc_count:561
  314. },
  315. ...
  316. ]
  317. }
  318. }
  319. }
  320. =end
  321. def self.selectors(index, selectors = nil, options = {}, aggs_interval = nil)
  322. raise 'no selectors given' if !selectors
  323. url = build_url(type: index, action: '_search', with_pipeline: false, with_document_type: true)
  324. return if url.blank?
  325. data = selector2query(selectors, options, aggs_interval)
  326. response = make_request(url, data: data)
  327. if !response.success?
  328. raise humanized_error(
  329. verb: 'GET',
  330. url: url,
  331. payload: data,
  332. response: response,
  333. )
  334. end
  335. Rails.logger.debug { response.data.to_json }
  336. if aggs_interval.blank? || aggs_interval[:interval].blank?
  337. ticket_ids = []
  338. response.data['hits']['hits'].each do |item|
  339. ticket_ids.push item['_id']
  340. end
  341. # in lower ES 6 versions, we get total count directly, in higher
  342. # versions we need to pick it from total has
  343. count = response.data['hits']['total']
  344. if response.data['hits']['total'].class != Integer
  345. count = response.data['hits']['total']['value']
  346. end
  347. return {
  348. count: count,
  349. ticket_ids: ticket_ids,
  350. }
  351. end
  352. response.data
  353. end
  354. DEFAULT_SELECTOR_OPTIONS = {
  355. limit: 10
  356. }.freeze
  357. def self.selector2query(selector, options, aggs_interval)
  358. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  359. query_must = []
  360. query_must_not = []
  361. relative_map = {
  362. day: 'd',
  363. year: 'y',
  364. month: 'M',
  365. hour: 'h',
  366. minute: 'm',
  367. }
  368. if selector.present?
  369. selector.each do |key, data|
  370. key_tmp = key.sub(/^.+?\./, '')
  371. wildcard_or_term = 'term'
  372. if data['value'].is_a?(Array)
  373. wildcard_or_term = 'terms'
  374. end
  375. t = {}
  376. # use .keyword in case of compare exact values
  377. if data['operator'] == 'is' || data['operator'] == 'is not'
  378. if data['value'].is_a?(Array)
  379. data['value'].each do |value|
  380. next if !value.is_a?(String) || value !~ /[A-z]/
  381. key_tmp += '.keyword'
  382. break
  383. end
  384. elsif data['value'].is_a?(String) && /[A-z]/.match?(data['value'])
  385. key_tmp += '.keyword'
  386. end
  387. end
  388. # use .keyword and wildcard search in cases where query contains non A-z chars
  389. if data['operator'] == 'contains' || data['operator'] == 'contains not'
  390. if data['value'].is_a?(Array)
  391. data['value'].each_with_index do |value, index|
  392. next if !value.is_a?(String) || value !~ /[A-z]/ || value !~ /\W/
  393. data['value'][index] = "*#{value}*"
  394. key_tmp += '.keyword'
  395. wildcard_or_term = 'wildcards'
  396. break
  397. end
  398. elsif data['value'].is_a?(String) && /[A-z]/.match?(data['value']) && data['value'] =~ /\W/
  399. data['value'] = "*#{data['value']}*"
  400. key_tmp += '.keyword'
  401. wildcard_or_term = 'wildcard'
  402. end
  403. end
  404. # is/is not/contains/contains not
  405. if data['operator'] == 'is' || data['operator'] == 'is not' || data['operator'] == 'contains' || data['operator'] == 'contains not'
  406. t[wildcard_or_term] = {}
  407. t[wildcard_or_term][key_tmp] = data['value']
  408. if data['operator'] == 'is' || data['operator'] == 'contains'
  409. query_must.push t
  410. elsif data['operator'] == 'is not' || data['operator'] == 'contains not'
  411. query_must_not.push t
  412. end
  413. elsif data['operator'] == 'contains all' || data['operator'] == 'contains one' || data['operator'] == 'contains all not' || data['operator'] == 'contains one not'
  414. values = data['value'].split(',').map(&:strip)
  415. t[:query_string] = {}
  416. if data['operator'] == 'contains all'
  417. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" AND "')}\""
  418. query_must.push t
  419. elsif data['operator'] == 'contains one not'
  420. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" OR "')}\""
  421. query_must_not.push t
  422. elsif data['operator'] == 'contains one'
  423. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" OR "')}\""
  424. query_must.push t
  425. elsif data['operator'] == 'contains all not'
  426. t[:query_string][:query] = "#{key_tmp}:\"#{values.join('" AND "')}\""
  427. query_must_not.push t
  428. end
  429. # within last/within next (relative)
  430. elsif data['operator'] == 'within last (relative)' || data['operator'] == 'within next (relative)'
  431. range = relative_map[data['range'].to_sym]
  432. if range.blank?
  433. raise "Invalid relative_map for range '#{data['range']}'."
  434. end
  435. t[:range] = {}
  436. t[:range][key_tmp] = {}
  437. if data['operator'] == 'within last (relative)'
  438. t[:range][key_tmp][:gte] = "now-#{data['value']}#{range}"
  439. else
  440. t[:range][key_tmp][:lt] = "now+#{data['value']}#{range}"
  441. end
  442. query_must.push t
  443. # before/after (relative)
  444. elsif data['operator'] == 'before (relative)' || data['operator'] == 'after (relative)'
  445. range = relative_map[data['range'].to_sym]
  446. if range.blank?
  447. raise "Invalid relative_map for range '#{data['range']}'."
  448. end
  449. t[:range] = {}
  450. t[:range][key_tmp] = {}
  451. if data['operator'] == 'before (relative)'
  452. t[:range][key_tmp][:lt] = "now-#{data['value']}#{range}"
  453. else
  454. t[:range][key_tmp][:gt] = "now+#{data['value']}#{range}"
  455. end
  456. query_must.push t
  457. # before/after (absolute)
  458. elsif data['operator'] == 'before (absolute)' || data['operator'] == 'after (absolute)'
  459. t[:range] = {}
  460. t[:range][key_tmp] = {}
  461. if data['operator'] == 'before (absolute)'
  462. t[:range][key_tmp][:lt] = (data['value'])
  463. else
  464. t[:range][key_tmp][:gt] = (data['value'])
  465. end
  466. query_must.push t
  467. else
  468. raise "unknown operator '#{data['operator']}' for #{key}"
  469. end
  470. end
  471. end
  472. data = {
  473. query: {},
  474. size: options[:limit],
  475. }
  476. # add aggs to filter
  477. if aggs_interval.present?
  478. if aggs_interval[:interval].present?
  479. data[:size] = 0
  480. data[:aggs] = {
  481. time_buckets: {
  482. date_histogram: {
  483. field: aggs_interval[:field],
  484. interval: aggs_interval[:interval],
  485. }
  486. }
  487. }
  488. if aggs_interval[:timezone].present?
  489. data[:aggs][:time_buckets][:date_histogram][:time_zone] = aggs_interval[:timezone]
  490. end
  491. end
  492. r = {}
  493. r[:range] = {}
  494. r[:range][aggs_interval[:field]] = {
  495. from: aggs_interval[:from],
  496. to: aggs_interval[:to],
  497. }
  498. query_must.push r
  499. end
  500. data[:query][:bool] ||= {}
  501. if query_must.present?
  502. data[:query][:bool][:must] = query_must
  503. end
  504. if query_must_not.present?
  505. data[:query][:bool][:must_not] = query_must_not
  506. end
  507. # add sort
  508. if aggs_interval.present? && aggs_interval[:field].present? && aggs_interval[:interval].blank?
  509. sort = []
  510. sort[0] = {}
  511. sort[0][aggs_interval[:field]] = {
  512. order: 'desc'
  513. }
  514. sort[1] = '_score'
  515. data['sort'] = sort
  516. else
  517. data['sort'] = search_by_index_sort(options[:sort_by], options[:order_by])
  518. end
  519. data
  520. end
  521. =begin
  522. return true if backend is configured
  523. result = SearchIndexBackend.enabled?
  524. =end
  525. def self.enabled?
  526. return false if Setting.get('es_url').blank?
  527. true
  528. end
  529. def self.build_index_name(index = nil)
  530. local_index = "#{Setting.get('es_index')}_#{Rails.env}"
  531. return local_index if index.blank?
  532. return "#{local_index}/#{index}" if lower_equal_es56?
  533. "#{local_index}_#{index.underscore.tr('/', '_')}"
  534. end
  535. =begin
  536. return true if the elastic search version is lower equal 5.6
  537. result = SearchIndexBackend.lower_equal_es56?
  538. returns
  539. result = true
  540. =end
  541. def self.lower_equal_es56?
  542. Setting.get('es_multi_index') == false
  543. end
  544. =begin
  545. generate url for index or document access (only for internal use)
  546. # url to access single document in index (in case with_pipeline or not)
  547. url = SearchIndexBackend.build_url(type: 'User', object_id: 123, with_pipeline: true)
  548. # url to access whole index
  549. url = SearchIndexBackend.build_url(type: 'User')
  550. # url to access document definition in index (only es6 and higher)
  551. url = SearchIndexBackend.build_url(type: 'User', with_pipeline: false, with_document_type: true)
  552. # base url
  553. url = SearchIndexBackend.build_url
  554. =end
  555. # rubocop:disable Metrics/ParameterLists
  556. def self.build_url(type: nil, action: nil, object_id: nil, with_pipeline: true, with_document_type: true, url_params: {})
  557. # rubocop:enable Metrics/ParameterLists
  558. return if !SearchIndexBackend.enabled?
  559. # set index
  560. index = build_index_name(type)
  561. # add pipeline if needed
  562. if index && with_pipeline == true
  563. url_pipline = Setting.get('es_pipeline')
  564. if url_pipline.present?
  565. url_params['pipeline'] = url_pipline
  566. end
  567. end
  568. # prepare url params
  569. params_string = ''
  570. if url_params.present?
  571. params_string = '?' + url_params.map { |key, value| "#{key}=#{value}" }.join('&')
  572. end
  573. url = Setting.get('es_url')
  574. return "#{url}#{params_string}" if index.blank?
  575. # add type information
  576. url = "#{url}/#{index}"
  577. # add document type
  578. if with_document_type && !lower_equal_es56?
  579. url = "#{url}/_doc"
  580. end
  581. # add action
  582. if action
  583. url = "#{url}/#{action}"
  584. end
  585. # add object id
  586. if object_id.present?
  587. url = "#{url}/#{object_id}"
  588. end
  589. "#{url}#{params_string}"
  590. end
  591. def self.humanized_error(verb:, url:, payload: nil, response:)
  592. prefix = "Unable to process #{verb} request to elasticsearch URL '#{url}'."
  593. suffix = "\n\nResponse:\n#{response.inspect}\n\n"
  594. if payload.respond_to?(:to_json)
  595. suffix += "Payload:\n#{payload.to_json}"
  596. suffix += "\n\nPayload size: #{payload.to_json.bytesize / 1024 / 1024}M"
  597. else
  598. suffix += "Payload:\n#{payload.inspect}"
  599. end
  600. message = if response&.error&.match?('Connection refused')
  601. "Elasticsearch is not reachable, probably because it's not running or even installed."
  602. elsif url.end_with?('pipeline/zammad-attachment', 'pipeline=zammad-attachment') && response.code == 400
  603. 'The installed attachment plugin could not handle the request payload. Ensure that the correct attachment plugin is installed (5.6 => ingest-attachment, 2.4 - 5.5 => mapper-attachments).'
  604. else
  605. 'Check the response and payload for detailed information: '
  606. end
  607. result = "#{prefix} #{message}#{suffix}"
  608. Rails.logger.error result.first(40_000)
  609. result
  610. end
  611. # add * on simple query like "somephrase23"
  612. def self.append_wildcard_to_simple_query(query)
  613. query.strip!
  614. query += '*' if !query.match?(/:/)
  615. query
  616. end
  617. =begin
  618. @param condition [Hash] search condition
  619. @param options [Hash] search options
  620. @option options [Integer] :from
  621. @option options [Integer] :limit
  622. @option options [Hash] :query_extension applied to ElasticSearch query
  623. @option options [Array<String>] :order_by ordering directions, desc or asc
  624. @option options [Array<String>] :sort_by fields to sort by
  625. =end
  626. DEFAULT_QUERY_OPTIONS = {
  627. from: 0,
  628. limit: 10
  629. }.freeze
  630. def self.build_query(condition, options = {})
  631. options = DEFAULT_QUERY_OPTIONS.merge(options.deep_symbolize_keys)
  632. data = {
  633. from: options[:from],
  634. size: options[:limit],
  635. sort: search_by_index_sort(options[:sort_by], options[:order_by]),
  636. query: {
  637. bool: {
  638. must: []
  639. }
  640. }
  641. }
  642. if (extension = options.dig(:query_extension))
  643. data[:query].deep_merge! extension.deep_dup
  644. end
  645. data[:query][:bool][:must].push condition
  646. data
  647. end
  648. =begin
  649. refreshes all indexes to make previous request data visible in future requests
  650. SearchIndexBackend.refresh
  651. =end
  652. def self.refresh
  653. return if !enabled?
  654. url = "#{Setting.get('es_url')}/_all/_refresh"
  655. make_request_and_validate(url, method: :post)
  656. end
  657. =begin
  658. helper method for making HTTP calls
  659. @param url [String] url
  660. @option params [Hash] :data is a payload hash
  661. @option params [Symbol] :method is a HTTP method
  662. @option params [Integer] :open_timeout is HTTP request open timeout
  663. @option params [Integer] :read_timeout is HTTP request read timeout
  664. @return UserAgent response
  665. =end
  666. def self.make_request(url, data: {}, method: :get, open_timeout: 8, read_timeout: 180)
  667. Rails.logger.info "# curl -X #{method} \"#{url}\" "
  668. Rails.logger.debug { "-d '#{data.to_json}'" } if data.present?
  669. options = {
  670. json: true,
  671. open_timeout: open_timeout,
  672. read_timeout: read_timeout,
  673. total_timeout: (open_timeout + read_timeout + 60),
  674. open_socket_tries: 3,
  675. user: Setting.get('es_user'),
  676. password: Setting.get('es_password'),
  677. }
  678. response = UserAgent.send(method, url, data, options)
  679. Rails.logger.info "# #{response.code}"
  680. response
  681. end
  682. =begin
  683. helper method for making HTTP calls and raising error if response was not success
  684. @param url [String] url
  685. @option args [Hash] see {make_request}
  686. @return [Boolean] always returns true. Raises error if something went wrong.
  687. =end
  688. def self.make_request_and_validate(url, **args)
  689. response = make_request(url, args)
  690. return true if response.success?
  691. raise humanized_error(
  692. verb: args[:method],
  693. url: url,
  694. payload: args[:data],
  695. response: response
  696. )
  697. end
  698. end