chewy 5.1.0 → 7.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (234) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.github/workflows/ruby.yml +73 -0
  7. data/.rubocop.yml +13 -8
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +449 -347
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -7
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +423 -311
  16. data/chewy.gemspec +8 -10
  17. data/gemfiles/rails.5.2.activerecord.gemfile +9 -14
  18. data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
  19. data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
  20. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  21. data/lib/chewy/config.rb +42 -60
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +80 -20
  24. data/lib/chewy/fields/root.rb +7 -17
  25. data/lib/chewy/index/actions.rb +62 -35
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +2 -3
  28. data/lib/chewy/{type → index}/adapter/object.rb +28 -32
  29. data/lib/chewy/{type → index}/adapter/orm.rb +26 -24
  30. data/lib/chewy/index/aliases.rb +14 -5
  31. data/lib/chewy/{type → index}/crutch.rb +5 -5
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +6 -7
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +17 -16
  36. data/lib/chewy/{type → index}/import.rb +51 -33
  37. data/lib/chewy/{type → index}/mapping.rb +32 -37
  38. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  39. data/lib/chewy/index/observe/callback.rb +34 -0
  40. data/lib/chewy/index/observe.rb +17 -0
  41. data/lib/chewy/index/specification.rb +1 -0
  42. data/lib/chewy/{type → index}/syncer.rb +61 -62
  43. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  44. data/lib/chewy/{type → index}/wrapper.rb +13 -3
  45. data/lib/chewy/index.rb +46 -96
  46. data/lib/chewy/journal.rb +25 -14
  47. data/lib/chewy/minitest/helpers.rb +86 -13
  48. data/lib/chewy/minitest/search_index_receiver.rb +22 -26
  49. data/lib/chewy/multi_search.rb +62 -0
  50. data/lib/chewy/railtie.rb +6 -20
  51. data/lib/chewy/rake_helper.rb +136 -108
  52. data/lib/chewy/rspec/build_query.rb +12 -0
  53. data/lib/chewy/rspec/helpers.rb +55 -0
  54. data/lib/chewy/rspec/update_index.rb +55 -44
  55. data/lib/chewy/rspec.rb +2 -0
  56. data/lib/chewy/runtime.rb +1 -1
  57. data/lib/chewy/search/loader.rb +19 -41
  58. data/lib/chewy/search/parameters/collapse.rb +16 -0
  59. data/lib/chewy/search/parameters/concerns/query_storage.rb +2 -2
  60. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  61. data/lib/chewy/search/parameters/indices.rb +12 -57
  62. data/lib/chewy/search/parameters/none.rb +1 -3
  63. data/lib/chewy/search/parameters/order.rb +6 -19
  64. data/lib/chewy/search/parameters/source.rb +5 -1
  65. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  66. data/lib/chewy/search/parameters.rb +7 -4
  67. data/lib/chewy/search/query_proxy.rb +9 -2
  68. data/lib/chewy/search/request.rb +180 -154
  69. data/lib/chewy/search/response.rb +5 -5
  70. data/lib/chewy/search/scoping.rb +7 -8
  71. data/lib/chewy/search/scrolling.rb +16 -13
  72. data/lib/chewy/search.rb +7 -22
  73. data/lib/chewy/stash.rb +19 -30
  74. data/lib/chewy/strategy/active_job.rb +2 -2
  75. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  76. data/lib/chewy/strategy/base.rb +10 -0
  77. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  78. data/lib/chewy/strategy/sidekiq.rb +3 -2
  79. data/lib/chewy/strategy.rb +5 -19
  80. data/lib/chewy/version.rb +1 -1
  81. data/lib/chewy.rb +36 -80
  82. data/lib/generators/chewy/install_generator.rb +1 -1
  83. data/lib/tasks/chewy.rake +26 -32
  84. data/migration_guide.md +56 -0
  85. data/spec/chewy/config_spec.rb +15 -61
  86. data/spec/chewy/fields/base_spec.rb +432 -145
  87. data/spec/chewy/fields/root_spec.rb +20 -28
  88. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  89. data/spec/chewy/index/actions_spec.rb +388 -55
  90. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  91. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  92. data/spec/chewy/index/aliases_spec.rb +3 -3
  93. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  94. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  95. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +14 -22
  96. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  97. data/spec/chewy/{type → index}/import_spec.rb +149 -96
  98. data/spec/chewy/index/mapping_spec.rb +135 -0
  99. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  100. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  101. data/spec/chewy/index/observe_spec.rb +143 -0
  102. data/spec/chewy/index/settings_spec.rb +3 -1
  103. data/spec/chewy/index/specification_spec.rb +20 -30
  104. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  105. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  106. data/spec/chewy/index/wrapper_spec.rb +100 -0
  107. data/spec/chewy/index_spec.rb +69 -137
  108. data/spec/chewy/journal_spec.rb +46 -91
  109. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  110. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  111. data/spec/chewy/multi_search_spec.rb +84 -0
  112. data/spec/chewy/rake_helper_spec.rb +293 -101
  113. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  114. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  115. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  116. data/spec/chewy/runtime_spec.rb +2 -2
  117. data/spec/chewy/search/loader_spec.rb +19 -53
  118. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  119. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  120. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  121. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  122. data/spec/chewy/search/parameters/indices_spec.rb +26 -118
  123. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  124. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  125. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  126. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  127. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  128. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  129. data/spec/chewy/search/parameters_spec.rb +23 -7
  130. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  131. data/spec/chewy/search/request_spec.rb +344 -149
  132. data/spec/chewy/search/response_spec.rb +35 -25
  133. data/spec/chewy/search/scrolling_spec.rb +28 -26
  134. data/spec/chewy/search_spec.rb +69 -59
  135. data/spec/chewy/stash_spec.rb +16 -26
  136. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  137. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  138. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  139. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  140. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  141. data/spec/chewy/strategy_spec.rb +19 -15
  142. data/spec/chewy_spec.rb +17 -110
  143. data/spec/spec_helper.rb +6 -29
  144. data/spec/support/active_record.rb +43 -5
  145. metadata +102 -198
  146. data/.travis.yml +0 -45
  147. data/Appraisals +0 -81
  148. data/LEGACY_DSL.md +0 -497
  149. data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
  150. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  151. data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
  152. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +0 -16
  153. data/gemfiles/rails.5.0.activerecord.gemfile +0 -16
  154. data/gemfiles/rails.5.0.mongoid.6.1.gemfile +0 -16
  155. data/gemfiles/rails.5.1.activerecord.gemfile +0 -16
  156. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +0 -16
  157. data/gemfiles/sequel.4.45.gemfile +0 -11
  158. data/lib/chewy/backports/deep_dup.rb +0 -46
  159. data/lib/chewy/backports/duplicable.rb +0 -91
  160. data/lib/chewy/query/compose.rb +0 -68
  161. data/lib/chewy/query/criteria.rb +0 -191
  162. data/lib/chewy/query/filters.rb +0 -244
  163. data/lib/chewy/query/loading.rb +0 -110
  164. data/lib/chewy/query/nodes/and.rb +0 -25
  165. data/lib/chewy/query/nodes/base.rb +0 -17
  166. data/lib/chewy/query/nodes/bool.rb +0 -34
  167. data/lib/chewy/query/nodes/equal.rb +0 -34
  168. data/lib/chewy/query/nodes/exists.rb +0 -20
  169. data/lib/chewy/query/nodes/expr.rb +0 -28
  170. data/lib/chewy/query/nodes/field.rb +0 -110
  171. data/lib/chewy/query/nodes/has_child.rb +0 -15
  172. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  173. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  174. data/lib/chewy/query/nodes/match_all.rb +0 -11
  175. data/lib/chewy/query/nodes/missing.rb +0 -20
  176. data/lib/chewy/query/nodes/not.rb +0 -25
  177. data/lib/chewy/query/nodes/or.rb +0 -25
  178. data/lib/chewy/query/nodes/prefix.rb +0 -19
  179. data/lib/chewy/query/nodes/query.rb +0 -20
  180. data/lib/chewy/query/nodes/range.rb +0 -63
  181. data/lib/chewy/query/nodes/raw.rb +0 -15
  182. data/lib/chewy/query/nodes/regexp.rb +0 -35
  183. data/lib/chewy/query/nodes/script.rb +0 -20
  184. data/lib/chewy/query/pagination.rb +0 -25
  185. data/lib/chewy/query.rb +0 -1142
  186. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  187. data/lib/chewy/search/parameters/types.rb +0 -20
  188. data/lib/chewy/strategy/resque.rb +0 -27
  189. data/lib/chewy/strategy/shoryuken.rb +0 -40
  190. data/lib/chewy/type/actions.rb +0 -43
  191. data/lib/chewy/type/adapter/mongoid.rb +0 -67
  192. data/lib/chewy/type/adapter/sequel.rb +0 -93
  193. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  194. data/lib/chewy/type/observe.rb +0 -82
  195. data/lib/chewy/type.rb +0 -117
  196. data/lib/sequel/plugins/chewy_observe.rb +0 -63
  197. data/spec/chewy/query/criteria_spec.rb +0 -700
  198. data/spec/chewy/query/filters_spec.rb +0 -201
  199. data/spec/chewy/query/loading_spec.rb +0 -124
  200. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  201. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  202. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  203. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  204. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  205. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  206. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  207. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  208. data/spec/chewy/query/nodes/not_spec.rb +0 -14
  209. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  210. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  211. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  212. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  213. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  214. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  215. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  216. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  217. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  218. data/spec/chewy/query/pagination_spec.rb +0 -39
  219. data/spec/chewy/query_spec.rb +0 -637
  220. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  221. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  222. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  223. data/spec/chewy/strategy/resque_spec.rb +0 -46
  224. data/spec/chewy/strategy/shoryuken_spec.rb +0 -66
  225. data/spec/chewy/type/actions_spec.rb +0 -50
  226. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  227. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  228. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  229. data/spec/chewy/type/mapping_spec.rb +0 -173
  230. data/spec/chewy/type/observe_spec.rb +0 -137
  231. data/spec/chewy/type/wrapper_spec.rb +0 -98
  232. data/spec/chewy/type_spec.rb +0 -55
  233. data/spec/support/mongoid.rb +0 -93
  234. data/spec/support/sequel.rb +0 -80
@@ -3,14 +3,12 @@ module Chewy
3
3
  IMPORT_CALLBACK = lambda do |output, _name, start, finish, _id, payload|
4
4
  duration = (finish - start).ceil
5
5
  stats = payload.fetch(:import, {}).map { |key, count| "#{key} #{count}" }.join(', ')
6
- output.puts " Imported #{payload[:type]} in #{human_duration(duration)}, stats: #{stats}"
7
- if payload[:errors]
8
- payload[:errors].each do |action, errors|
9
- output.puts " #{action.to_s.humanize} errors:"
10
- errors.each do |error, documents|
11
- output.puts " `#{error}`"
12
- output.puts " on #{documents.count} documents: #{documents}"
13
- end
6
+ output.puts " Imported #{payload[:index]} in #{human_duration(duration)}, stats: #{stats}"
7
+ payload[:errors]&.each do |action, errors|
8
+ output.puts " #{action.to_s.humanize} errors:"
9
+ errors.each do |error, documents|
10
+ output.puts " `#{error}`"
11
+ output.puts " on #{documents.count} documents: #{documents}"
14
12
  end
15
13
  end
16
14
  end
@@ -21,6 +19,9 @@ module Chewy
21
19
  output.puts " Applying journal to #{targets}, #{count} entries, stage #{payload[:stage]}"
22
20
  end
23
21
 
22
+ DELETE_BY_QUERY_OPTIONS = %w[WAIT_FOR_COMPLETION REQUESTS_PER_SECOND SCROLL_SIZE].freeze
23
+ FALSE_VALUES = %w[0 f false off].freeze
24
+
24
25
  class << self
25
26
  # Performs zero-downtime reindexing of all documents for the specified indexes
26
27
  #
@@ -36,7 +37,9 @@ module Chewy
36
37
  # @param parallel [true, Integer, Hash] any acceptable parallel options for import
37
38
  # @param output [IO] output io for logging
38
39
  # @return [Array<Chewy::Index>] indexes that were reset
39
- def reset(only: nil, except: nil, parallel: nil, output: STDOUT)
40
+ def reset(only: nil, except: nil, parallel: nil, output: $stdout)
41
+ warn_missing_index(output)
42
+
40
43
  subscribed_task_stats(output) do
41
44
  indexes_from(only: only, except: except).each do |index|
42
45
  reset_one(index, output, parallel: parallel)
@@ -59,7 +62,9 @@ module Chewy
59
62
  # @param parallel [true, Integer, Hash] any acceptable parallel options for import
60
63
  # @param output [IO] output io for logging
61
64
  # @return [Array<Chewy::Index>] indexes that were actually reset
62
- def upgrade(only: nil, except: nil, parallel: nil, output: STDOUT)
65
+ def upgrade(only: nil, except: nil, parallel: nil, output: $stdout)
66
+ warn_missing_index(output)
67
+
63
68
  subscribed_task_stats(output) do
64
69
  indexes = indexes_from(only: only, except: except)
65
70
 
@@ -87,23 +92,21 @@ module Chewy
87
92
  #
88
93
  # @example
89
94
  # Chewy::RakeHelper.update # updates everything
90
- # Chewy::RakeHelper.update(only: 'places') # updates only PlacesIndex::City and PlacesIndex::Country
91
- # Chewy::RakeHelper.update(only: 'places#city') # updates PlacesIndex::City only
92
- # Chewy::RakeHelper.update(except: PlacesIndex::Country) # updates everything, but PlacesIndex::Country
93
- # Chewy::RakeHelper.update(only: 'places', except: 'places#country') # updates PlacesIndex::City only
95
+ # Chewy::RakeHelper.update(only: 'places') # updates only PlacesIndex
96
+ # Chewy::RakeHelper.update(except: PlacesIndex) # updates everything, but PlacesIndex
94
97
  #
95
- # @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to update; if nothing is passed - uses all the types defined in the app
96
- # @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
98
+ # @param only [Array<Chewy::Index, String>, Chewy::Index, String] indexes to update; if nothing is passed - uses all the indexes defined in the app
99
+ # @param except [Array<Chewy::Index, String>, Chewy::Index, String] indexes to exclude from processing
97
100
  # @param parallel [true, Integer, Hash] any acceptable parallel options for import
98
101
  # @param output [IO] output io for logging
99
- # @return [Array<Chewy::Type>] types that were actually updated
100
- def update(only: nil, except: nil, parallel: nil, output: STDOUT)
102
+ # @return [Array<Chewy::Index>] indexes that were actually updated
103
+ def update(only: nil, except: nil, parallel: nil, output: $stdout)
101
104
  subscribed_task_stats(output) do
102
- types_from(only: only, except: except).group_by(&:index).each_with_object([]) do |(index, types), update_types|
105
+ indexes_from(only: only, except: except).each_with_object([]) do |index, updated_indexes|
103
106
  if index.exists?
104
107
  output.puts "Updating #{index}"
105
- types.each { |type| type.import(parallel: parallel) }
106
- update_types.concat(types)
108
+ index.import(parallel: parallel)
109
+ updated_indexes.push(index)
107
110
  else
108
111
  output.puts "Skipping #{index}, it does not exists (use rake chewy:reset[#{index.derivable_name}] to create and update it)"
109
112
  end
@@ -115,31 +118,29 @@ module Chewy
115
118
  #
116
119
  # @example
117
120
  # Chewy::RakeHelper.sync # synchronizes everything
118
- # Chewy::RakeHelper.sync(only: 'places') # synchronizes only PlacesIndex::City and PlacesIndex::Country
119
- # Chewy::RakeHelper.sync(only: 'places#city') # synchronizes PlacesIndex::City only
120
- # Chewy::RakeHelper.sync(except: PlacesIndex::Country) # synchronizes everything, but PlacesIndex::Country
121
- # Chewy::RakeHelper.sync(only: 'places', except: 'places#country') # synchronizes PlacesIndex::City only
121
+ # Chewy::RakeHelper.sync(only: 'places') # synchronizes only PlacesIndex
122
+ # Chewy::RakeHelper.sync(except: PlacesIndex) # synchronizes everything, but PlacesIndex
122
123
  #
123
- # @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to synchronize; if nothing is passed - uses all the types defined in the app
124
- # @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
124
+ # @param only [Array<Chewy::Index, String>, Chewy::Index, String] indexes to synchronize; if nothing is passed - uses all the indexes defined in the app
125
+ # @param except [Array<Chewy::Index, String>, Chewy::Index, String] indexes to exclude from processing
125
126
  # @param parallel [true, Integer, Hash] any acceptable parallel options for sync
126
127
  # @param output [IO] output io for logging
127
- # @return [Array<Chewy::Type>] types that were actually updated
128
- def sync(only: nil, except: nil, parallel: nil, output: STDOUT)
128
+ # @return [Array<Chewy::Index>] indexes that were actually updated
129
+ def sync(only: nil, except: nil, parallel: nil, output: $stdout)
129
130
  subscribed_task_stats(output) do
130
- types_from(only: only, except: except).each_with_object([]) do |type, synced_types|
131
- output.puts "Synchronizing #{type}"
132
- output.puts " #{type} doesn't support outdated synchronization" unless type.supports_outdated_sync?
131
+ indexes_from(only: only, except: except).each_with_object([]) do |index, synced_indexes|
132
+ output.puts "Synchronizing #{index}"
133
+ output.puts " #{index} doesn't support outdated synchronization" unless index.supports_outdated_sync?
133
134
  time = Time.now
134
- sync_result = type.sync(parallel: parallel)
135
+ sync_result = index.sync(parallel: parallel)
135
136
  if !sync_result
136
- output.puts " Something went wrong with the #{type} synchronization"
137
- elsif sync_result[:count] > 0
137
+ output.puts " Something went wrong with the #{index} synchronization"
138
+ elsif (sync_result[:count]).positive?
138
139
  output.puts " Missing documents: #{sync_result[:missing]}" if sync_result[:missing].present?
139
140
  output.puts " Outdated documents: #{sync_result[:outdated]}" if sync_result[:outdated].present?
140
- synced_types.push(type)
141
+ synced_indexes.push(index)
141
142
  else
142
- output.puts " Skipping #{type}, up to date"
143
+ output.puts " Skipping #{index}, up to date"
143
144
  end
144
145
  output.puts " Took #{human_duration(Time.now - time)}"
145
146
  end
@@ -147,51 +148,52 @@ module Chewy
147
148
  end
148
149
 
149
150
  # Applies changes that were done after the specified time for the
150
- # specified indexes/types or all of them.
151
+ # specified indexes or all of them.
151
152
  #
152
153
  # @example
153
154
  # Chewy::RakeHelper.journal_apply(time: 1.minute.ago) # applies entries created for the last minute
154
- # Chewy::RakeHelper.journal_apply(time: 1.minute.ago, only: 'places') # applies only PlacesIndex::City and PlacesIndex::Country entries reated for the last minute
155
- # Chewy::RakeHelper.journal_apply(time: 1.minute.ago, only: 'places#city') # applies PlacesIndex::City entries reated for the last minute only
156
- # Chewy::RakeHelper.journal_apply(time: 1.minute.ago, except: PlacesIndex::Country) # applies everything, but PlacesIndex::Country entries reated for the last minute
157
- # Chewy::RakeHelper.journal_apply(time: 1.minute.ago, only: 'places', except: 'places#country') # applies PlacesIndex::City entries reated for the last minute only
155
+ # Chewy::RakeHelper.journal_apply(time: 1.minute.ago, only: 'places') # applies only PlacesIndex entries created for the last minute
156
+ # Chewy::RakeHelper.journal_apply(time: 1.minute.ago, except: PlacesIndex) # applies everything, but PlacesIndex, entries created for the last minute
158
157
  #
159
158
  # @param time [Time, DateTime] use only journal entries created after this time
160
- # @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to synchronize; if nothing is passed - uses all the types defined in the app
161
- # @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
159
+ # @param only [Array<Chewy::Index, String>, Chewy::Index, String] indexes to synchronize; if nothing is passed - uses all the indexes defined in the app
160
+ # @param except [Array<Chewy::Index, String>, Chewy::Index, String] indexes to exclude from processing
162
161
  # @param output [IO] output io for logging
163
- # @return [Array<Chewy::Type>] types that were actually updated
164
- def journal_apply(time: nil, only: nil, except: nil, output: STDOUT)
162
+ # @return [Array<Chewy::Index>] indexes that were actually updated
163
+ def journal_apply(time: nil, only: nil, except: nil, output: $stdout)
165
164
  raise ArgumentError, 'Please specify the time to start with' unless time
165
+
166
166
  subscribed_task_stats(output) do
167
167
  output.puts "Applying journal entries created after #{time}"
168
- count = Chewy::Journal.new(types_from(only: only, except: except)).apply(time)
168
+ count = Chewy::Journal.new(journal_indexes_from(only: only, except: except)).apply(time)
169
169
  output.puts 'No journal entries were created after the specified time' if count.zero?
170
170
  end
171
171
  end
172
172
 
173
173
  # Removes journal records created before the specified timestamp for
174
- # the specified indexes/types or all of them.
174
+ # the specified indexes or all of them.
175
175
  #
176
176
  # @example
177
177
  # Chewy::RakeHelper.journal_clean # cleans everything
178
178
  # Chewy::RakeHelper.journal_clean(time: 1.minute.ago) # leaves only entries created for the last minute
179
- # Chewy::RakeHelper.journal_clean(only: 'places') # cleans only PlacesIndex::City and PlacesIndex::Country entries
180
- # Chewy::RakeHelper.journal_clean(only: 'places#city') # cleans PlacesIndex::City entries only
181
- # Chewy::RakeHelper.journal_clean(except: PlacesIndex::Country) # cleans everything, but PlacesIndex::Country entries
182
- # Chewy::RakeHelper.journal_clean(only: 'places', except: 'places#country') # cleans PlacesIndex::City entries only
179
+ # Chewy::RakeHelper.journal_clean(only: 'places') # cleans only PlacesIndex entries
180
+ # Chewy::RakeHelper.journal_clean(except: PlacesIndex) # cleans everything, but PlacesIndex entries
183
181
  #
184
182
  # @param time [Time, DateTime] clean all the journal entries created before this time
185
- # @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to synchronize; if nothing is passed - uses all the types defined in the app
186
- # @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
183
+ # @param only [Array<Chewy::Index, String>, Chewy::Index, String] indexes to synchronize; if nothing is passed - uses all the indexes defined in the app
184
+ # @param except [Array<Chewy::Index, String>, Chewy::Index, String] indexes to exclude from processing
187
185
  # @param output [IO] output io for logging
188
- # @return [Array<Chewy::Type>] types that were actually updated
189
- def journal_clean(time: nil, only: nil, except: nil, output: STDOUT)
186
+ # @return [Array<Chewy::Index>] indexes that were actually updated
187
+ def journal_clean(time: nil, only: nil, except: nil, delete_by_query_options: {}, output: $stdout)
190
188
  subscribed_task_stats(output) do
191
189
  output.puts "Cleaning journal entries created before #{time}" if time
192
- response = Chewy::Journal.new(types_from(only: only, except: except)).clean(time)
193
- count = response['deleted'] || response['_indices']['_all']['deleted']
194
- output.puts "Cleaned up #{count} journal entries"
190
+ response = Chewy::Journal.new(journal_indexes_from(only: only, except: except)).clean(time, delete_by_query_options: delete_by_query_options)
191
+ if response.key?('task')
192
+ output.puts "Task to cleanup the journal has been created, #{response['task']}"
193
+ else
194
+ count = response['deleted'] || response['_indices']['_all']['deleted']
195
+ output.puts "Cleaned up #{count} journal entries"
196
+ end
195
197
  end
196
198
  end
197
199
 
@@ -204,47 +206,81 @@ module Chewy
204
206
  Chewy::Index.descendants - [Chewy::Stash::Journal, Chewy::Stash::Specification]
205
207
  end
206
208
 
209
+ # Reindex data from source index to destination index
210
+ #
211
+ # @example
212
+ # Chewy::RakeHelper.reindex(source: 'users_index', dest: 'cities_index') reindex data from 'users_index' index to 'cities_index'
213
+ #
214
+ # @param source [String], dest [String] indexes to reindex
215
+ def reindex(source:, dest:, output: $stdout)
216
+ subscribed_task_stats(output) do
217
+ output.puts "Source index is #{source}\nDestination index is #{dest}"
218
+ Chewy::Index.reindex(source: source, dest: dest)
219
+ output.puts "#{source} index successfully reindexed with #{dest} index data"
220
+ end
221
+ end
222
+
223
+ # Adds new fields to an existing data stream or index.
224
+ # Change the search settings of existing fields.
225
+ #
226
+ # @example
227
+ # Chewy::RakeHelper.update_mapping('cities', {properties: {new_field: {type: :text}}}) update 'cities' index with new_field of text type
228
+ #
229
+ # @param name [String], body_hash [Hash] index name and body hash to update
230
+ def update_mapping(name:, output: $stdout)
231
+ subscribed_task_stats(output) do
232
+ output.puts "Index name is #{name}"
233
+ normalize_index(name).update_mapping
234
+ output.puts "#{name} index successfully updated"
235
+ end
236
+ end
237
+
238
+ # Reads options that are required to run journal cleanup asynchronously from ENV hash
239
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
240
+ #
241
+ # @example
242
+ # Chewy::RakeHelper.delete_by_query_options_from_env({'WAIT_FOR_COMPLETION' => 'false','REQUESTS_PER_SECOND' => '10','SCROLL_SIZE' => '5000'})
243
+ # # => { wait_for_completion: false, requests_per_second: 10.0, scroll_size: 5000 }
244
+ #
245
+ def delete_by_query_options_from_env(env)
246
+ env
247
+ .slice(*DELETE_BY_QUERY_OPTIONS)
248
+ .transform_keys { |k| k.downcase.to_sym }
249
+ .to_h do |key, value|
250
+ case key
251
+ when :wait_for_completion then [key, !FALSE_VALUES.include?(value.downcase)]
252
+ when :requests_per_second then [key, value.to_f]
253
+ when :scroll_size then [key, value.to_i]
254
+ end
255
+ end
256
+ end
257
+
207
258
  def normalize_indexes(*identifiers)
208
259
  identifiers.flatten(1).map { |identifier| normalize_index(identifier) }
209
260
  end
210
261
 
211
262
  def normalize_index(identifier)
212
263
  return identifier if identifier.is_a?(Class) && identifier < Chewy::Index
213
- "#{identifier.to_s.gsub(/identifier\z/i, '').camelize}Index".constantize
264
+
265
+ "#{identifier.to_s.camelize}Index".constantize
214
266
  end
215
267
 
216
- def subscribed_task_stats(output = STDOUT)
268
+ def subscribed_task_stats(output = $stdout, &block)
217
269
  start = Time.now
218
270
  ActiveSupport::Notifications.subscribed(JOURNAL_CALLBACK.curry[output], 'apply_journal.chewy') do
219
- ActiveSupport::Notifications.subscribed(IMPORT_CALLBACK.curry[output], 'import_objects.chewy') do
220
- yield
221
- end
271
+ ActiveSupport::Notifications.subscribed(IMPORT_CALLBACK.curry[output], 'import_objects.chewy', &block)
222
272
  end
223
273
  output.puts "Total: #{human_duration(Time.now - start)}"
224
274
  end
225
275
 
226
- def reset_index(*indexes)
227
- ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.reset_index` is deprecated and will be removed soon, use `Chewy::RakeHelper.reset` instead'
228
- reset(only: indexes)
229
- end
230
-
231
- def reset_all(*except)
232
- ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.reset_all` is deprecated and will be removed soon, use `Chewy::RakeHelper.reset` instead'
233
- reset(except: except)
234
- end
276
+ private
235
277
 
236
- def update_index(*indexes)
237
- ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.update_index` is deprecated and will be removed soon, use `Chewy::RakeHelper.update` instead'
238
- update(only: indexes)
239
- end
278
+ def journal_indexes_from(only: nil, except: nil)
279
+ return if Array.wrap(only).empty? && Array.wrap(except).empty?
240
280
 
241
- def update_all(*except)
242
- ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.update_all` is deprecated and will be removed soon, use `Chewy::RakeHelper.update` instead'
243
- update(except: except)
281
+ indexes_from(only: only, except: except)
244
282
  end
245
283
 
246
- private
247
-
248
284
  def indexes_from(only: nil, except: nil)
249
285
  indexes = if only.present?
250
286
  normalize_indexes(Array.wrap(only))
@@ -261,33 +297,9 @@ module Chewy
261
297
  indexes.sort_by(&:derivable_name)
262
298
  end
263
299
 
264
- def types_from(only: nil, except: nil)
265
- types = if only.present?
266
- normalize_types(Array.wrap(only))
267
- else
268
- all_indexes.flat_map(&:types)
269
- end
270
-
271
- types = if except.present?
272
- types - normalize_types(Array.wrap(except))
273
- else
274
- types
275
- end
276
-
277
- types.sort_by(&:derivable_name)
278
- end
279
-
280
- def normalize_types(*identifiers)
281
- identifiers.flatten(1).flat_map { |identifier| normalize_type(identifier) }
282
- end
283
-
284
- def normalize_type(identifier)
285
- Chewy.derive_types(identifier)
286
- end
287
-
288
300
  def human_duration(seconds)
289
301
  [[60, :s], [60, :m], [24, :h]].map do |amount, unit|
290
- if seconds > 0
302
+ if seconds.positive?
291
303
  seconds, n = seconds.divmod(amount)
292
304
  "#{n.to_i}#{unit}"
293
305
  end
@@ -296,7 +308,23 @@ module Chewy
296
308
 
297
309
  def reset_one(index, output, parallel: false)
298
310
  output.puts "Resetting #{index}"
299
- index.reset!((Time.now.to_f * 1000).round, parallel: parallel)
311
+ index.reset!((Time.now.to_f * 1000).round, parallel: parallel, apply_journal: journal_exists?)
312
+ end
313
+
314
+ def warn_missing_index(output)
315
+ return if journal_exists?
316
+
317
+ output.puts "############################################################\n" \
318
+ "WARN: You are risking to lose some changes during the reset.\n" \
319
+ " Please consider enabling journaling.\n" \
320
+ " See https://github.com/toptal/chewy#journaling\n" \
321
+ '############################################################'
322
+ end
323
+
324
+ def journal_exists?
325
+ @journal_exists = Chewy::Stash::Journal.exists? if @journal_exists.nil?
326
+
327
+ @journal_exists
300
328
  end
301
329
  end
302
330
  end
@@ -0,0 +1,12 @@
1
+ # Rspec helper to compare request and expected query
2
+ # To use it - add `require 'chewy/rspec/build_query'` to the `spec_helper.rb`
3
+ # Simple usage - just pass expected response as argument
4
+ # and then call needed query.
5
+ #
6
+ # expect { method1.method2...methodN }.to build_query(expected_query)
7
+ #
8
+ RSpec::Matchers.define :build_query do |expected_query = {}|
9
+ match do |request|
10
+ request.render == expected_query
11
+ end
12
+ end
@@ -0,0 +1,55 @@
1
+ module Chewy
2
+ module Rspec
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+ # Rspec helper to mock elasticsearch response
6
+ # To use it - add `require 'chewy/rspec'` to the `spec_helper.rb`
7
+ #
8
+ # mock_elasticsearch_response(CitiesIndex, raw_response)
9
+ # expect(CitiesIndex.query({}).hits).to eq(hits)
10
+ #
11
+ def mock_elasticsearch_response(index, raw_response)
12
+ mocked_request = Chewy::Search::Request.new(index)
13
+ allow(Chewy::Search::Request).to receive(:new).and_return(mocked_request)
14
+ allow(mocked_request).to receive(:perform).and_return(raw_response)
15
+ end
16
+
17
+ # Rspec helper to mock Elasticsearch response source
18
+ # To use it - add `require 'chewy/rspec'` to the `spec_helper.rb`
19
+ #
20
+ # mock_elasticsearch_response_sources(CitiesIndex, sources)
21
+ # expect(CitiesIndex.query({}).hits).to eq(hits)
22
+ #
23
+ def mock_elasticsearch_response_sources(index, hits)
24
+ raw_response = {
25
+ 'took' => 4,
26
+ 'timed_out' => false,
27
+ '_shards' => {
28
+ 'total' => 1,
29
+ 'successful' => 1,
30
+ 'skipped' => 0,
31
+ 'failed' => 0
32
+ },
33
+ 'hits' => {
34
+ 'total' => {
35
+ 'value' => hits.count,
36
+ 'relation' => 'eq'
37
+ },
38
+ 'max_score' => 1.0,
39
+ 'hits' => hits.each_with_index.map do |hit, i|
40
+ {
41
+ '_index' => index.index_name,
42
+ '_type' => '_doc',
43
+ '_id' => (i + 1).to_s,
44
+ '_score' => 3.14,
45
+ '_source' => hit
46
+ }
47
+ end
48
+ }
49
+ }
50
+
51
+ mock_elasticsearch_response(index, raw_response)
52
+ end
53
+ end
54
+ end
55
+ end