chewy 0.8.4 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +56 -0
  4. data/.rubocop_todo.yml +44 -0
  5. data/.travis.yml +36 -67
  6. data/.yardopts +5 -0
  7. data/Appraisals +63 -58
  8. data/CHANGELOG.md +168 -11
  9. data/Gemfile +16 -9
  10. data/Guardfile +5 -5
  11. data/LEGACY_DSL.md +497 -0
  12. data/README.md +403 -470
  13. data/Rakefile +11 -1
  14. data/chewy.gemspec +12 -15
  15. data/gemfiles/rails.4.0.activerecord.gemfile +9 -9
  16. data/gemfiles/rails.4.1.activerecord.gemfile +9 -9
  17. data/gemfiles/rails.4.2.activerecord.gemfile +8 -8
  18. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +16 -0
  19. data/gemfiles/rails.5.0.activerecord.gemfile +16 -0
  20. data/gemfiles/rails.5.0.mongoid.6.1.gemfile +16 -0
  21. data/gemfiles/rails.5.1.activerecord.gemfile +16 -0
  22. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +16 -0
  23. data/gemfiles/rails.5.2.activerecord.gemfile +16 -0
  24. data/gemfiles/sequel.4.45.gemfile +11 -0
  25. data/lib/chewy/backports/deep_dup.rb +1 -1
  26. data/lib/chewy/backports/duplicable.rb +1 -0
  27. data/lib/chewy/config.rb +53 -21
  28. data/lib/chewy/errors.rb +6 -6
  29. data/lib/chewy/fields/base.rb +59 -29
  30. data/lib/chewy/fields/root.rb +49 -14
  31. data/lib/chewy/index/actions.rb +95 -36
  32. data/lib/chewy/index/aliases.rb +2 -1
  33. data/lib/chewy/index/settings.rb +10 -5
  34. data/lib/chewy/index/specification.rb +60 -0
  35. data/lib/chewy/index.rb +239 -138
  36. data/lib/chewy/journal.rb +55 -0
  37. data/lib/chewy/log_subscriber.rb +8 -8
  38. data/lib/chewy/minitest/helpers.rb +77 -0
  39. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  40. data/lib/chewy/minitest.rb +1 -0
  41. data/lib/chewy/query/compose.rb +18 -19
  42. data/lib/chewy/query/criteria.rb +34 -24
  43. data/lib/chewy/query/filters.rb +28 -11
  44. data/lib/chewy/query/loading.rb +3 -4
  45. data/lib/chewy/query/nodes/and.rb +1 -1
  46. data/lib/chewy/query/nodes/base.rb +1 -1
  47. data/lib/chewy/query/nodes/bool.rb +6 -4
  48. data/lib/chewy/query/nodes/equal.rb +4 -4
  49. data/lib/chewy/query/nodes/exists.rb +1 -1
  50. data/lib/chewy/query/nodes/expr.rb +2 -2
  51. data/lib/chewy/query/nodes/field.rb +35 -31
  52. data/lib/chewy/query/nodes/has_child.rb +1 -0
  53. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  54. data/lib/chewy/query/nodes/has_relation.rb +10 -12
  55. data/lib/chewy/query/nodes/missing.rb +1 -1
  56. data/lib/chewy/query/nodes/not.rb +1 -1
  57. data/lib/chewy/query/nodes/or.rb +1 -1
  58. data/lib/chewy/query/nodes/prefix.rb +3 -2
  59. data/lib/chewy/query/nodes/query.rb +1 -1
  60. data/lib/chewy/query/nodes/range.rb +9 -9
  61. data/lib/chewy/query/nodes/raw.rb +1 -1
  62. data/lib/chewy/query/nodes/regexp.rb +13 -9
  63. data/lib/chewy/query/nodes/script.rb +4 -4
  64. data/lib/chewy/query/pagination.rb +10 -1
  65. data/lib/chewy/query.rb +286 -170
  66. data/lib/chewy/railtie.rb +7 -6
  67. data/lib/chewy/rake_helper.rb +275 -37
  68. data/lib/chewy/repository.rb +2 -2
  69. data/lib/chewy/rspec/update_index.rb +70 -65
  70. data/lib/chewy/rspec.rb +1 -1
  71. data/lib/chewy/runtime/version.rb +4 -4
  72. data/lib/chewy/search/loader.rb +83 -0
  73. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  74. data/lib/chewy/search/pagination/will_paginate.rb +43 -0
  75. data/lib/chewy/search/parameters/aggs.rb +16 -0
  76. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  77. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  78. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  79. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  80. data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
  81. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  82. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  83. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  84. data/lib/chewy/search/parameters/explain.rb +16 -0
  85. data/lib/chewy/search/parameters/filter.rb +47 -0
  86. data/lib/chewy/search/parameters/highlight.rb +16 -0
  87. data/lib/chewy/search/parameters/indices.rb +123 -0
  88. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  89. data/lib/chewy/search/parameters/limit.rb +17 -0
  90. data/lib/chewy/search/parameters/load.rb +32 -0
  91. data/lib/chewy/search/parameters/min_score.rb +16 -0
  92. data/lib/chewy/search/parameters/none.rb +27 -0
  93. data/lib/chewy/search/parameters/offset.rb +17 -0
  94. data/lib/chewy/search/parameters/order.rb +64 -0
  95. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  96. data/lib/chewy/search/parameters/preference.rb +16 -0
  97. data/lib/chewy/search/parameters/profile.rb +16 -0
  98. data/lib/chewy/search/parameters/query.rb +19 -0
  99. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  100. data/lib/chewy/search/parameters/rescore.rb +29 -0
  101. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  102. data/lib/chewy/search/parameters/search_after.rb +20 -0
  103. data/lib/chewy/search/parameters/search_type.rb +16 -0
  104. data/lib/chewy/search/parameters/source.rb +73 -0
  105. data/lib/chewy/search/parameters/storage.rb +95 -0
  106. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  107. data/lib/chewy/search/parameters/suggest.rb +16 -0
  108. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  109. data/lib/chewy/search/parameters/timeout.rb +16 -0
  110. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  111. data/lib/chewy/search/parameters/types.rb +20 -0
  112. data/lib/chewy/search/parameters/version.rb +16 -0
  113. data/lib/chewy/search/parameters.rb +167 -0
  114. data/lib/chewy/search/query_proxy.rb +257 -0
  115. data/lib/chewy/search/request.rb +1045 -0
  116. data/lib/chewy/search/response.rb +119 -0
  117. data/lib/chewy/search/scoping.rb +50 -0
  118. data/lib/chewy/search/scrolling.rb +134 -0
  119. data/lib/chewy/search.rb +81 -26
  120. data/lib/chewy/stash.rb +79 -0
  121. data/lib/chewy/strategy/active_job.rb +1 -0
  122. data/lib/chewy/strategy/atomic.rb +2 -4
  123. data/lib/chewy/strategy/base.rb +4 -4
  124. data/lib/chewy/strategy/bypass.rb +1 -2
  125. data/lib/chewy/strategy/resque.rb +1 -0
  126. data/lib/chewy/strategy/shoryuken.rb +40 -0
  127. data/lib/chewy/strategy/sidekiq.rb +13 -1
  128. data/lib/chewy/strategy/urgent.rb +1 -1
  129. data/lib/chewy/strategy.rb +19 -10
  130. data/lib/chewy/type/actions.rb +26 -2
  131. data/lib/chewy/type/adapter/active_record.rb +50 -24
  132. data/lib/chewy/type/adapter/base.rb +29 -9
  133. data/lib/chewy/type/adapter/mongoid.rb +19 -10
  134. data/lib/chewy/type/adapter/object.rb +195 -31
  135. data/lib/chewy/type/adapter/orm.rb +69 -33
  136. data/lib/chewy/type/adapter/sequel.rb +37 -19
  137. data/lib/chewy/type/crutch.rb +5 -4
  138. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  139. data/lib/chewy/type/import/bulk_request.rb +78 -0
  140. data/lib/chewy/type/import/journal_builder.rb +45 -0
  141. data/lib/chewy/type/import/routine.rb +138 -0
  142. data/lib/chewy/type/import.rb +150 -176
  143. data/lib/chewy/type/mapping.rb +58 -42
  144. data/lib/chewy/type/observe.rb +21 -15
  145. data/lib/chewy/type/syncer.rb +222 -0
  146. data/lib/chewy/type/witchcraft.rb +89 -34
  147. data/lib/chewy/type/wrapper.rb +48 -16
  148. data/lib/chewy/type.rb +77 -49
  149. data/lib/chewy/version.rb +1 -1
  150. data/lib/chewy.rb +95 -52
  151. data/lib/generators/chewy/install_generator.rb +3 -3
  152. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  153. data/lib/tasks/chewy.rake +91 -28
  154. data/spec/chewy/config_spec.rb +130 -12
  155. data/spec/chewy/fields/base_spec.rb +194 -172
  156. data/spec/chewy/fields/root_spec.rb +123 -17
  157. data/spec/chewy/fields/time_fields_spec.rb +10 -9
  158. data/spec/chewy/index/actions_spec.rb +228 -43
  159. data/spec/chewy/index/aliases_spec.rb +2 -2
  160. data/spec/chewy/index/settings_spec.rb +100 -49
  161. data/spec/chewy/index/specification_spec.rb +169 -0
  162. data/spec/chewy/index_spec.rb +159 -63
  163. data/spec/chewy/journal_spec.rb +268 -0
  164. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  165. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  166. data/spec/chewy/query/criteria_spec.rb +503 -236
  167. data/spec/chewy/query/filters_spec.rb +96 -68
  168. data/spec/chewy/query/loading_spec.rb +80 -42
  169. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  170. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  171. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  172. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  173. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  174. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  175. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  176. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  177. data/spec/chewy/query/nodes/not_spec.rb +5 -7
  178. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  179. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  180. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  181. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  182. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  183. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  184. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  185. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  186. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  187. data/spec/chewy/query/pagination_spec.rb +25 -22
  188. data/spec/chewy/query_spec.rb +510 -505
  189. data/spec/chewy/rake_helper_spec.rb +381 -0
  190. data/spec/chewy/repository_spec.rb +8 -8
  191. data/spec/chewy/rspec/update_index_spec.rb +215 -113
  192. data/spec/chewy/runtime_spec.rb +2 -2
  193. data/spec/chewy/search/loader_spec.rb +117 -0
  194. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  195. data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
  196. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  197. data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
  198. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  199. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  200. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  201. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  202. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  203. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  204. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  205. data/spec/chewy/search/parameters/indices_spec.rb +191 -0
  206. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  207. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  208. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  209. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  210. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  211. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  212. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  213. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  214. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  215. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  216. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  217. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  218. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  219. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  220. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  221. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  222. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  223. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  224. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  225. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  226. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  227. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  228. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  229. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  230. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  231. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  232. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  233. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  234. data/spec/chewy/search/parameters_spec.rb +145 -0
  235. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  236. data/spec/chewy/search/request_spec.rb +685 -0
  237. data/spec/chewy/search/response_spec.rb +192 -0
  238. data/spec/chewy/search/scrolling_spec.rb +169 -0
  239. data/spec/chewy/search_spec.rb +37 -20
  240. data/spec/chewy/stash_spec.rb +95 -0
  241. data/spec/chewy/strategy/active_job_spec.rb +8 -2
  242. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  243. data/spec/chewy/strategy/resque_spec.rb +8 -2
  244. data/spec/chewy/strategy/shoryuken_spec.rb +66 -0
  245. data/spec/chewy/strategy/sidekiq_spec.rb +10 -2
  246. data/spec/chewy/strategy_spec.rb +6 -6
  247. data/spec/chewy/type/actions_spec.rb +29 -10
  248. data/spec/chewy/type/adapter/active_record_spec.rb +357 -139
  249. data/spec/chewy/type/adapter/mongoid_spec.rb +220 -101
  250. data/spec/chewy/type/adapter/object_spec.rb +129 -40
  251. data/spec/chewy/type/adapter/sequel_spec.rb +304 -152
  252. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  253. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  254. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  255. data/spec/chewy/type/import/routine_spec.rb +110 -0
  256. data/spec/chewy/type/import_spec.rb +360 -244
  257. data/spec/chewy/type/mapping_spec.rb +96 -29
  258. data/spec/chewy/type/observe_spec.rb +25 -15
  259. data/spec/chewy/type/syncer_spec.rb +123 -0
  260. data/spec/chewy/type/witchcraft_spec.rb +122 -44
  261. data/spec/chewy/type/wrapper_spec.rb +63 -23
  262. data/spec/chewy/type_spec.rb +32 -10
  263. data/spec/chewy_spec.rb +82 -12
  264. data/spec/spec_helper.rb +16 -2
  265. data/spec/support/active_record.rb +6 -2
  266. data/spec/support/class_helpers.rb +4 -19
  267. data/spec/support/mongoid.rb +17 -5
  268. data/spec/support/sequel.rb +6 -1
  269. metadata +250 -57
  270. data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
  271. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
  272. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
  273. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  274. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  275. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  276. data/gemfiles/rails.4.0.mongoid.4.0.0.kaminari.gemfile +0 -14
  277. data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
  278. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  279. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  280. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  281. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  282. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  283. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  284. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  285. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  286. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  287. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  288. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  289. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -15
  290. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
  291. data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
  292. data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
  293. data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
  294. data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
  295. data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
  296. data/gemfiles/rails.4.2.mongoid.5.1.0.will_paginate.gemfile +0 -14
  297. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  298. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  299. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
  300. data/gemfiles/sequel.4.31.gemfile +0 -13
  301. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  302. data/lib/chewy/query/scoping.rb +0 -20
  303. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -60
data/lib/chewy/railtie.rb CHANGED
@@ -14,6 +14,8 @@ module Chewy
14
14
  if Rails.application.config.respond_to?(:assets) && env['PATH_INFO'].start_with?(Rails.application.config.assets.prefix)
15
15
  @app.call(env)
16
16
  else
17
+ Chewy.logger.info("Chewy request strategy is `#{Chewy.request_strategy}`") if Chewy.logger && @request_strategy != Chewy.request_strategy
18
+ @request_strategy = Chewy.request_strategy
17
19
  Chewy.strategy(Chewy.request_strategy) { @app.call(env) }
18
20
  end
19
21
  end
@@ -41,17 +43,16 @@ module Chewy
41
43
  end
42
44
 
43
45
  console do |app|
44
- Chewy.logger = ActiveRecord::Base.logger if defined?(ActiveRecord)
45
-
46
46
  if app.sandbox?
47
47
  Chewy.strategy(:bypass)
48
48
  else
49
49
  Chewy.strategy(:urgent)
50
50
  end
51
+ puts "Chewy console strategy is `#{Chewy.strategy.current.name}`"
51
52
  end
52
53
 
53
54
  initializer 'chewy.logger', after: 'active_record.logger' do
54
- ActiveSupport.on_load(:active_record) { Chewy.logger ||= ActiveRecord::Base.logger }
55
+ ActiveSupport.on_load(:active_record) { Chewy.logger ||= ActiveRecord::Base.logger }
55
56
  end
56
57
 
57
58
  initializer 'chewy.migration_strategy' do
@@ -67,12 +68,12 @@ module Chewy
67
68
  end
68
69
 
69
70
  initializer 'chewy.request_strategy' do |app|
70
- app.config.middleware.insert_after(Rails::Rack::Logger, RequestStrategy)
71
+ app.config.middleware.insert_before(ActionDispatch::ShowExceptions, RequestStrategy)
71
72
  end
72
73
 
73
- initializer 'chewy.add_app_chewy_path' do |app|
74
+ initializer 'chewy.add_indices_path' do |_app|
74
75
  Chewy::Railtie.all_engines.each do |engine|
75
- engine.paths.add 'app/chewy'
76
+ engine.paths.add Chewy.configuration[:indices_path]
76
77
  end
77
78
  end
78
79
  end
@@ -1,64 +1,302 @@
1
1
  module Chewy
2
2
  module RakeHelper
3
+ IMPORT_CALLBACK = lambda do |output, _name, start, finish, _id, payload|
4
+ duration = (finish - start).ceil
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
14
+ end
15
+ end
16
+ end
17
+
18
+ JOURNAL_CALLBACK = lambda do |output, _, _, _, _, payload|
19
+ count = payload[:groups].values.map(&:size).sum
20
+ targets = payload[:groups].keys.sort_by(&:derivable_name)
21
+ output.puts " Applying journal to #{targets}, #{count} entries, stage #{payload[:stage]}"
22
+ end
23
+
3
24
  class << self
25
+ # Performs zero-downtime reindexing of all documents for the specified indexes
26
+ #
27
+ # @example
28
+ # Chewy::RakeHelper.reset # resets everything
29
+ # Chewy::RakeHelper.reset(only: 'cities') # resets only CitiesIndex
30
+ # Chewy::RakeHelper.reset(only: ['cities', CountriesIndex]) # resets CitiesIndex and CountriesIndex
31
+ # Chewy::RakeHelper.reset(except: CitiesIndex) # resets everything, but CitiesIndex
32
+ # Chewy::RakeHelper.reset(only: ['cities', 'countries'], except: CitiesIndex) # resets only CountriesIndex
33
+ #
34
+ # @param only [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to reset; if nothing is passed - uses all the indexes defined in the app
35
+ # @param except [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to exclude from processing
36
+ # @param parallel [true, Integer, Hash] any acceptable parallel options for import
37
+ # @param output [IO] output io for logging
38
+ # @return [Array<Chewy::Index>] indexes that were reset
39
+ def reset(only: nil, except: nil, parallel: nil, output: STDOUT)
40
+ subscribed_task_stats(output) do
41
+ indexes_from(only: only, except: except).each do |index|
42
+ reset_one(index, output, parallel: parallel)
43
+ end
44
+ end
45
+ end
4
46
 
5
- def subscribed_task_stats(&block)
6
- callback = ->(name, start, finish, id, payload) do
7
- duration = (finish - start).round(2)
8
- puts " Imported #{payload[:type]} for #{duration}s, documents total: #{payload[:import].try(:[], :index).to_i}"
9
- payload[:errors].each do |action, errors|
10
- puts " #{action.to_s.humanize} errors:"
11
- errors.each do |error, documents|
12
- puts " `#{error}`"
13
- puts " on #{documents.count} documents: #{documents}"
47
+ # Performs zero-downtime reindexing of all documents for the specified
48
+ # indexes only if a particular index specification was changed.
49
+ #
50
+ # @example
51
+ # Chewy::RakeHelper.upgrade # resets everything
52
+ # Chewy::RakeHelper.upgrade(only: 'cities') # resets only CitiesIndex
53
+ # Chewy::RakeHelper.upgrade(only: ['cities', CountriesIndex]) # resets CitiesIndex and CountriesIndex
54
+ # Chewy::RakeHelper.upgrade(except: CitiesIndex) # resets everything, but CitiesIndex
55
+ # Chewy::RakeHelper.upgrade(only: ['cities', 'countries'], except: CitiesIndex) # resets only CountriesIndex
56
+ #
57
+ # @param only [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to reset; if nothing is passed - uses all the indexes defined in the app
58
+ # @param except [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to exclude from processing
59
+ # @param parallel [true, Integer, Hash] any acceptable parallel options for import
60
+ # @param output [IO] output io for logging
61
+ # @return [Array<Chewy::Index>] indexes that were actually reset
62
+ def upgrade(only: nil, except: nil, parallel: nil, output: STDOUT)
63
+ subscribed_task_stats(output) do
64
+ indexes = indexes_from(only: only, except: except)
65
+
66
+ changed_indexes = indexes.select do |index|
67
+ index.specification.changed?
68
+ end
69
+
70
+ if changed_indexes.present?
71
+ indexes.each do |index|
72
+ if changed_indexes.include?(index)
73
+ reset_one(index, output, parallel: parallel)
74
+ else
75
+ output.puts "Skipping #{index}, the specification didn't change"
76
+ end
14
77
  end
15
- end if payload[:errors]
78
+ else
79
+ output.puts 'No index specification was changed'
80
+ end
81
+
82
+ changed_indexes
16
83
  end
17
- ActiveSupport::Notifications.subscribed(callback, 'import_objects.chewy') do
18
- yield
84
+ end
85
+
86
+ # Performs full update for each passed type if the corresponding index exists.
87
+ #
88
+ # @example
89
+ # 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
94
+ #
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
97
+ # @param parallel [true, Integer, Hash] any acceptable parallel options for import
98
+ # @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)
101
+ subscribed_task_stats(output) do
102
+ types_from(only: only, except: except).group_by(&:index).each_with_object([]) do |(index, types), update_types|
103
+ if index.exists?
104
+ output.puts "Updating #{index}"
105
+ types.each { |type| type.import(parallel: parallel) }
106
+ update_types.concat(types)
107
+ else
108
+ output.puts "Skipping #{index}, it does not exists (use rake chewy:reset[#{index.derivable_name}] to create and update it)"
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ # Performs synchronization for each passed index if it exists.
115
+ #
116
+ # @example
117
+ # 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
122
+ #
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
125
+ # @param parallel [true, Integer, Hash] any acceptable parallel options for sync
126
+ # @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)
129
+ 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?
133
+ time = Time.now
134
+ sync_result = type.sync(parallel: parallel)
135
+ if !sync_result
136
+ output.puts " Something went wrong with the #{type} synchronization"
137
+ elsif sync_result[:count] > 0
138
+ output.puts " Missing documents: #{sync_result[:missing]}" if sync_result[:missing].present?
139
+ output.puts " Outdated documents: #{sync_result[:outdated]}" if sync_result[:outdated].present?
140
+ synced_types.push(type)
141
+ else
142
+ output.puts " Skipping #{type}, up to date"
143
+ end
144
+ output.puts " Took #{human_duration(Time.now - time)}"
145
+ end
19
146
  end
20
147
  end
21
148
 
22
- def eager_load_chewy!
23
- dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths['app/chewy'] }.compact.map(&:existent).flatten.uniq
149
+ # Applies changes that were done after the specified time for the
150
+ # specified indexes/types or all of them.
151
+ #
152
+ # @example
153
+ # 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
158
+ #
159
+ # @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
162
+ # @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)
165
+ raise ArgumentError, 'Please specify the time to start with' unless time
166
+ subscribed_task_stats(output) do
167
+ output.puts "Applying journal entries created after #{time}"
168
+ count = Chewy::Journal.new(types_from(only: only, except: except)).apply(time)
169
+ output.puts 'No journal entries were created after the specified time' if count.zero?
170
+ end
171
+ end
172
+
173
+ # Removes journal records created before the specified timestamp for
174
+ # the specified indexes/types or all of them.
175
+ #
176
+ # @example
177
+ # Chewy::RakeHelper.journal_clean # cleans everything
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
183
+ #
184
+ # @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
187
+ # @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)
190
+ subscribed_task_stats(output) do
191
+ 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"
195
+ end
196
+ end
24
197
 
25
- dirs.each do |dir|
26
- Dir.glob(File.join(dir, '**/*.rb')).each do |file|
27
- require_dependency file
198
+ # Eager loads and returns all the indexes defined in the application
199
+ # except Chewy::Stash::Specification and Chewy::Stash::Journal.
200
+ #
201
+ # @return [Array<Chewy::Index>] indexes found
202
+ def all_indexes
203
+ Chewy.eager_load!
204
+ Chewy::Index.descendants - [Chewy::Stash::Journal, Chewy::Stash::Specification]
205
+ end
206
+
207
+ def normalize_indexes(*identifiers)
208
+ identifiers.flatten(1).map { |identifier| normalize_index(identifier) }
209
+ end
210
+
211
+ def normalize_index(identifier)
212
+ return identifier if identifier.is_a?(Class) && identifier < Chewy::Index
213
+ "#{identifier.to_s.gsub(/identifier\z/i, '').camelize}Index".constantize
214
+ end
215
+
216
+ def subscribed_task_stats(output = STDOUT)
217
+ start = Time.now
218
+ 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
28
221
  end
29
222
  end
223
+ output.puts "Total: #{human_duration(Time.now - start)}"
224
+ end
225
+
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)
30
234
  end
31
235
 
32
- def normalize_index index
33
- "#{index.to_s.gsub(/index\z/i, '').camelize}Index".constantize
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)
34
239
  end
35
240
 
36
- # Performs zero downtime reindexing of all documents in the specified index.
37
- def reset_index index
38
- index = normalize_index(index)
39
- puts "Resetting #{index}"
40
- index.reset! (Time.now.to_f * 1000).round
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)
41
244
  end
42
245
 
43
- # Performs zero downtime reindexing of all documents across all indices.
44
- def reset_all
45
- eager_load_chewy!
46
- Chewy::Index.descendants.each { |index| reset_index index }
246
+ private
247
+
248
+ def indexes_from(only: nil, except: nil)
249
+ indexes = if only.present?
250
+ normalize_indexes(Array.wrap(only))
251
+ else
252
+ all_indexes
253
+ end
254
+
255
+ indexes = if except.present?
256
+ indexes - normalize_indexes(Array.wrap(except))
257
+ else
258
+ indexes
259
+ end
260
+
261
+ indexes.sort_by(&:derivable_name)
47
262
  end
48
263
 
49
- def update_index index
50
- index = normalize_index(index)
51
- puts "Updating #{index}"
52
- if index.exists?
53
- index.import
264
+ def types_from(only: nil, except: nil)
265
+ types = if only.present?
266
+ normalize_types(Array.wrap(only))
54
267
  else
55
- puts "Index `#{index.index_name}` does not exists. Use rake chewy:reset[#{index.index_name}] to create and update it."
268
+ all_indexes.flat_map(&:types)
56
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
+ def human_duration(seconds)
289
+ [[60, :s], [60, :m], [24, :h]].map do |amount, unit|
290
+ if seconds > 0
291
+ seconds, n = seconds.divmod(amount)
292
+ "#{n.to_i}#{unit}"
293
+ end
294
+ end.compact.reverse.join(' ')
57
295
  end
58
296
 
59
- def update_all
60
- eager_load_chewy!
61
- Chewy::Index.descendants.each { |index| update_index index }
297
+ def reset_one(index, output, parallel: false)
298
+ output.puts "Resetting #{index}"
299
+ index.reset!((Time.now.to_f * 1000).round, parallel: parallel)
62
300
  end
63
301
  end
64
302
  end
@@ -5,10 +5,10 @@ module Chewy
5
5
  attr_reader :analyzers, :tokenizers, :filters, :char_filters
6
6
 
7
7
  def self.delegated
8
- public_instance_methods - self.superclass.public_instance_methods - Singleton.public_instance_methods
8
+ public_instance_methods - superclass.public_instance_methods - Singleton.public_instance_methods
9
9
  end
10
10
 
11
- def self.repository name
11
+ def self.repository(name)
12
12
  plural_name = name.to_s.pluralize
13
13
 
14
14
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
@@ -20,11 +20,10 @@ require 'i18n/core_ext/hash'
20
20
  # specify { expect { user1.destroy!; user2.save! } }
21
21
  # .to update_index(UsersIndex::User).and_reindex(user2).and_delete(user1) }
22
22
  #
23
- RSpec::Matchers.define :update_index do |type_name, options = {}|
24
-
23
+ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disable BlockLength
25
24
  if !respond_to?(:failure_message) && respond_to?(:failure_message_for_should)
26
- alias :failure_message :failure_message_for_should
27
- alias :failure_message_when_negated :failure_message_for_should_not
25
+ alias_method :failure_message, :failure_message_for_should
26
+ alias_method :failure_message_when_negated, :failure_message_for_should_not
28
27
  end
29
28
 
30
29
  # Specify indexed records by passing record itself or id.
@@ -83,7 +82,9 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
83
82
  # specify { expect { [user1, user2].map(&:save!) }
84
83
  # .to update_index(UsersIndex.user).and_reindex(user1).only }
85
84
  #
86
- chain(:only) do |*args|
85
+ chain(:only) do |*_args|
86
+ raise 'Use `only` in conjunction with `and_reindex` or `and_delete`' if @reindex.blank? && @delete.blank?
87
+
87
88
  @only = true
88
89
  end
89
90
 
@@ -91,64 +92,60 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
91
92
  true
92
93
  end
93
94
 
94
- match do |block|
95
+ match do |block| # rubocop:disable BlockLength
95
96
  @reindex ||= {}
96
97
  @delete ||= {}
97
98
  @missed_reindex = []
98
99
  @missed_delete = []
99
- @updated = []
100
100
 
101
101
  type = Chewy.derive_type(type_name)
102
+ if defined?(Mocha) && RSpec.configuration.mock_framework.to_s == 'RSpec::Core::MockingAdapters::Mocha'
103
+ Chewy::Type::Import::BulkRequest.stubs(:new).with(type, any_parameters).returns(mock_bulk_request)
104
+ else
105
+ mocked_already = ::RSpec::Mocks.space.proxy_for(Chewy::Type::Import::BulkRequest).method_double_if_exists_for_message(:new)
106
+ allow(Chewy::Type::Import::BulkRequest).to receive(:new).and_call_original unless mocked_already
107
+ allow(Chewy::Type::Import::BulkRequest).to receive(:new).with(type, any_args).and_return(mock_bulk_request)
108
+ end
102
109
 
103
- instance_eval <<-RUBY, __FILE__, __LINE__ + 1
104
- #{agnostic_stub} do |bulk_options|
105
- @updated += bulk_options[:body].map do |updated_document|
106
- updated_document.deep_symbolize_keys
107
- end
108
- {}
109
- end
110
- RUBY
111
-
112
- ActiveSupport::Deprecation.warn('`atomic: false` option is removed and not effective anymore, use `strategy: :atomic` option instead') if options.key?(:atomic)
113
110
  Chewy.strategy(options[:strategy] || :atomic) { block.call }
114
111
 
115
- @updated.each do |updated_document|
116
- if body = updated_document[:index]
117
- if document = @reindex[body[:_id].to_s]
112
+ mock_bulk_request.updates.each do |updated_document|
113
+ if (body = updated_document[:index])
114
+ if (document = @reindex[body[:_id].to_s])
118
115
  document[:real_count] += 1
119
116
  document[:real_attributes].merge!(body[:data])
120
- else
121
- @missed_reindex.push(body[:_id].to_s) if @only
117
+ elsif @only
118
+ @missed_reindex.push(body[:_id].to_s)
122
119
  end
123
- elsif body = updated_document[:delete]
124
- if document = @delete[body[:_id].to_s]
120
+ elsif (body = updated_document[:delete])
121
+ if (document = @delete[body[:_id].to_s])
125
122
  document[:real_count] += 1
126
- else
127
- @missed_delete.push(body[:_id].to_s) if @only
123
+ elsif @only
124
+ @missed_delete.push(body[:_id].to_s)
128
125
  end
129
126
  end
130
127
  end
131
128
 
132
- @reindex.each do |_, document|
129
+ @reindex.each_value do |document|
133
130
  document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
134
131
  (document[:expected_count] && document[:expected_count] == document[:real_count])
135
132
  document[:match_attributes] = document[:expected_attributes].blank? ||
136
133
  compare_attributes(document[:expected_attributes], document[:real_attributes])
137
134
  end
138
- @delete.each do |_, document|
135
+ @delete.each_value do |document|
139
136
  document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
140
137
  (document[:expected_count] && document[:expected_count] == document[:real_count])
141
138
  end
142
139
 
143
- @updated.present? && @missed_reindex.none? && @missed_delete.none? &&
144
- @reindex.all? { |_, document| document[:match_count] && document[:match_attributes] } &&
145
- @delete.all? { |_, document| document[:match_count] }
140
+ mock_bulk_request.updates.present? && @missed_reindex.none? && @missed_delete.none? &&
141
+ @reindex.all? { |_, document| document[:match_count] && document[:match_attributes] } &&
142
+ @delete.all? { |_, document| document[:match_count] }
146
143
  end
147
144
 
148
- failure_message do
145
+ failure_message do # rubocop:disable BlockLength
149
146
  output = ''
150
147
 
151
- if @updated.none?
148
+ if mock_bulk_request.updates.none?
152
149
  output << "Expected index `#{type_name}` to be updated, but it was not\n"
153
150
  elsif @missed_reindex.present? || @missed_delete.present?
154
151
  message = "Expected index `#{type_name}` "
@@ -166,28 +163,28 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
166
163
  output << message
167
164
  end
168
165
 
169
- output << @reindex.each.with_object('') do |(id, document), output|
166
+ output << @reindex.each.with_object('') do |(id, document), result|
170
167
  unless document[:match_count] && document[:match_attributes]
171
- output << "Expected document with id `#{id}` to be reindexed"
168
+ result << "Expected document with id `#{id}` to be reindexed"
172
169
  if document[:real_count] > 0
173
- output << "\n #{document[:expected_count]} times, but was reindexed #{document[:real_count]} times" if document[:expected_count] && !document[:match_count]
174
- output << "\n with #{document[:expected_attributes]}, but it was reindexed with #{document[:real_attributes]}" if document[:expected_attributes].present? && !document[:match_attributes]
170
+ result << "\n #{document[:expected_count]} times, but was reindexed #{document[:real_count]} times" if document[:expected_count] && !document[:match_count]
171
+ result << "\n with #{document[:expected_attributes]}, but it was reindexed with #{document[:real_attributes]}" if document[:expected_attributes].present? && !document[:match_attributes]
175
172
  else
176
- output << ", but it was not"
173
+ result << ', but it was not'
177
174
  end
178
- output << "\n"
175
+ result << "\n"
179
176
  end
180
177
  end
181
178
 
182
- output << @delete.each.with_object('') do |(id, document), output|
179
+ output << @delete.each.with_object('') do |(id, document), result|
183
180
  unless document[:match_count]
184
- output << "Expected document with id `#{id}` to be deleted"
185
- if document[:real_count] > 0 && document[:expected_count] && !document[:match_count]
186
- output << "\n #{document[:expected_count]} times, but was deleted #{document[:real_count]} times"
187
- else
188
- output << ", but it was not"
189
- end
190
- output << "\n"
181
+ result << "Expected document with id `#{id}` to be deleted"
182
+ result << if document[:real_count] > 0 && document[:expected_count]
183
+ "\n #{document[:expected_count]} times, but was deleted #{document[:real_count]} times"
184
+ else
185
+ ', but it was not'
186
+ end
187
+ result << "\n"
191
188
  end
192
189
  end
193
190
 
@@ -195,24 +192,18 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
195
192
  end
196
193
 
197
194
  failure_message_when_negated do
198
- if @updated.present?
199
- "Expected index `#{type_name}` not to be updated, but it was with #{
200
- @updated.map(&:values).flatten.group_by { |documents| documents[:_id] }.map do |id, documents|
201
- "\n document id `#{id}` (#{documents.count} times)"
202
- end.join
203
- }\n"
195
+ if mock_bulk_request.updates.present?
196
+ "Expected index `#{type_name}` not to be updated, but it was with #{mock_bulk_request.updates.map(&:values).flatten.group_by { |documents| documents[:_id] }.map do |id, documents|
197
+ "\n document id `#{id}` (#{documents.count} times)"
198
+ end.join}\n"
204
199
  end
205
200
  end
206
201
 
207
- def agnostic_stub
208
- if defined?(Mocha) && RSpec.configuration.mock_framework.to_s == 'RSpec::Core::MockingAdapters::Mocha'
209
- "type.stubs(:bulk).with"
210
- else
211
- "allow(type).to receive(:bulk)"
212
- end
202
+ def mock_bulk_request
203
+ @mock_bulk_request ||= MockBulkRequest.new
213
204
  end
214
205
 
215
- def extract_documents *args
206
+ def extract_documents(*args)
216
207
  options = args.extract_options!
217
208
 
218
209
  expected_count = options[:times] || options[:count]
@@ -230,7 +221,7 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
230
221
  end]
231
222
  end
232
223
 
233
- def compare_attributes expected, real
224
+ def compare_attributes(expected, real)
234
225
  expected.inject(true) do |result, (key, value)|
235
226
  equal = if value.is_a?(Array) && real[key].is_a?(Array)
236
227
  array_difference(value, real[key]) && array_difference(real[key], value)
@@ -243,13 +234,27 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
243
234
  end
244
235
  end
245
236
 
246
- def array_difference first, second
237
+ def array_difference(first, second)
247
238
  difference = first.to_ary.dup
248
239
  second.to_ary.each do |element|
249
- if index = difference.index(element)
250
- difference.delete_at(index)
251
- end
240
+ index = difference.index(element)
241
+ difference.delete_at(index) if index
252
242
  end
253
243
  difference.none?
254
244
  end
245
+
246
+ # Collects request bodies coming through the perform method for
247
+ # the further analysis.
248
+ class MockBulkRequest
249
+ attr_reader :updates
250
+
251
+ def initialize
252
+ @updates = []
253
+ end
254
+
255
+ def perform(body)
256
+ @updates.concat(body.map(&:deep_symbolize_keys))
257
+ []
258
+ end
259
+ end
255
260
  end
data/lib/chewy/rspec.rb CHANGED
@@ -1 +1 @@
1
- require 'chewy/rspec/update_index'
1
+ require 'chewy/rspec/update_index'
@@ -4,21 +4,21 @@ module Chewy
4
4
  include Comparable
5
5
  attr_reader :major, :minor, :patch
6
6
 
7
- def initialize version
8
- @major, @minor, @patch = *(version.to_s.split('.', 3) + [0]*3).first(3).map(&:to_i)
7
+ def initialize(version)
8
+ @major, @minor, @patch = *(version.to_s.split('.', 3) + [0] * 3).first(3).map(&:to_i)
9
9
  end
10
10
 
11
11
  def to_s
12
12
  [major, minor, patch].join('.')
13
13
  end
14
14
 
15
- def <=> other
15
+ def <=>(other)
16
16
  other = self.class.new(other) unless other.is_a?(self.class)
17
17
  [
18
18
  major <=> other.major,
19
19
  minor <=> other.minor,
20
20
  patch <=> other.patch
21
- ].detect { |c| c != 0 } || 0
21
+ ].detect(&:nonzero?) || 0
22
22
  end
23
23
  end
24
24
  end