chewy 0.8.4 → 7.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (340) hide show
  1. checksums.yaml +5 -5
  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 +74 -0
  7. data/.gitignore +1 -0
  8. data/.rubocop.yml +61 -0
  9. data/.rubocop_todo.yml +132 -0
  10. data/.yardopts +5 -0
  11. data/CHANGELOG.md +554 -245
  12. data/CODE_OF_CONDUCT.md +14 -0
  13. data/CONTRIBUTING.md +63 -0
  14. data/Gemfile +14 -11
  15. data/Guardfile +8 -6
  16. data/LICENSE.txt +1 -1
  17. data/README.md +748 -623
  18. data/Rakefile +11 -1
  19. data/chewy.gemspec +15 -19
  20. data/gemfiles/rails.5.2.activerecord.gemfile +11 -0
  21. data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
  22. data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
  23. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  24. data/lib/chewy/config.rb +64 -50
  25. data/lib/chewy/errors.rb +10 -16
  26. data/lib/chewy/fields/base.rb +122 -32
  27. data/lib/chewy/fields/root.rb +48 -23
  28. data/lib/chewy/index/actions.rb +140 -54
  29. data/lib/chewy/index/adapter/active_record.rb +112 -0
  30. data/lib/chewy/{type → index}/adapter/base.rb +31 -12
  31. data/lib/chewy/index/adapter/object.rb +249 -0
  32. data/lib/chewy/index/adapter/orm.rb +194 -0
  33. data/lib/chewy/index/aliases.rb +14 -4
  34. data/lib/chewy/index/crutch.rb +40 -0
  35. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  36. data/lib/chewy/index/import/bulk_request.rb +77 -0
  37. data/lib/chewy/index/import/journal_builder.rb +44 -0
  38. data/lib/chewy/index/import/routine.rb +139 -0
  39. data/lib/chewy/index/import.rb +243 -0
  40. data/lib/chewy/{type → index}/mapping.rb +79 -68
  41. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  42. data/lib/chewy/index/observe/callback.rb +34 -0
  43. data/lib/chewy/index/observe.rb +17 -0
  44. data/lib/chewy/index/settings.rb +10 -5
  45. data/lib/chewy/index/specification.rb +61 -0
  46. data/lib/chewy/index/syncer.rb +221 -0
  47. data/lib/chewy/{type → index}/witchcraft.rb +100 -39
  48. data/lib/chewy/index/wrapper.rb +95 -0
  49. data/lib/chewy/index.rb +216 -140
  50. data/lib/chewy/journal.rb +66 -0
  51. data/lib/chewy/log_subscriber.rb +8 -8
  52. data/lib/chewy/minitest/helpers.rb +150 -0
  53. data/lib/chewy/minitest/search_index_receiver.rb +76 -0
  54. data/lib/chewy/minitest.rb +1 -0
  55. data/lib/chewy/multi_search.rb +62 -0
  56. data/lib/chewy/railtie.rb +12 -25
  57. data/lib/chewy/rake_helper.rb +335 -37
  58. data/lib/chewy/repository.rb +2 -2
  59. data/lib/chewy/rspec/build_query.rb +12 -0
  60. data/lib/chewy/rspec/helpers.rb +55 -0
  61. data/lib/chewy/rspec/update_index.rb +106 -90
  62. data/lib/chewy/rspec.rb +3 -1
  63. data/lib/chewy/runtime/version.rb +4 -4
  64. data/lib/chewy/runtime.rb +1 -1
  65. data/lib/chewy/search/loader.rb +61 -0
  66. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  67. data/lib/chewy/search/parameters/aggs.rb +16 -0
  68. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  69. data/lib/chewy/search/parameters/collapse.rb +16 -0
  70. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  71. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  72. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  73. data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
  74. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  75. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  76. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  77. data/lib/chewy/search/parameters/explain.rb +16 -0
  78. data/lib/chewy/search/parameters/filter.rb +47 -0
  79. data/lib/chewy/search/parameters/highlight.rb +16 -0
  80. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  81. data/lib/chewy/search/parameters/indices.rb +78 -0
  82. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  83. data/lib/chewy/search/parameters/limit.rb +17 -0
  84. data/lib/chewy/search/parameters/load.rb +32 -0
  85. data/lib/chewy/search/parameters/min_score.rb +16 -0
  86. data/lib/chewy/search/parameters/none.rb +25 -0
  87. data/lib/chewy/search/parameters/offset.rb +17 -0
  88. data/lib/chewy/search/parameters/order.rb +51 -0
  89. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  90. data/lib/chewy/search/parameters/preference.rb +16 -0
  91. data/lib/chewy/search/parameters/profile.rb +16 -0
  92. data/lib/chewy/search/parameters/query.rb +19 -0
  93. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  94. data/lib/chewy/search/parameters/rescore.rb +29 -0
  95. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  96. data/lib/chewy/search/parameters/search_after.rb +20 -0
  97. data/lib/chewy/search/parameters/search_type.rb +16 -0
  98. data/lib/chewy/search/parameters/source.rb +77 -0
  99. data/lib/chewy/search/parameters/storage.rb +95 -0
  100. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  101. data/lib/chewy/search/parameters/suggest.rb +16 -0
  102. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  103. data/lib/chewy/search/parameters/timeout.rb +16 -0
  104. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  105. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  106. data/lib/chewy/search/parameters/version.rb +16 -0
  107. data/lib/chewy/search/parameters.rb +170 -0
  108. data/lib/chewy/search/query_proxy.rb +264 -0
  109. data/lib/chewy/search/request.rb +1071 -0
  110. data/lib/chewy/search/response.rb +119 -0
  111. data/lib/chewy/search/scoping.rb +49 -0
  112. data/lib/chewy/search/scrolling.rb +137 -0
  113. data/lib/chewy/search.rb +68 -28
  114. data/lib/chewy/stash.rb +68 -0
  115. data/lib/chewy/strategy/active_job.rb +3 -2
  116. data/lib/chewy/strategy/atomic.rb +2 -4
  117. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  118. data/lib/chewy/strategy/base.rb +13 -3
  119. data/lib/chewy/strategy/bypass.rb +1 -2
  120. data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +148 -0
  121. data/lib/chewy/strategy/delayed_sidekiq/worker.rb +52 -0
  122. data/lib/chewy/strategy/delayed_sidekiq.rb +17 -0
  123. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  124. data/lib/chewy/strategy/sidekiq.rb +15 -2
  125. data/lib/chewy/strategy/urgent.rb +1 -1
  126. data/lib/chewy/strategy.rb +16 -20
  127. data/lib/chewy/version.rb +1 -1
  128. data/lib/chewy.rb +81 -82
  129. data/lib/generators/chewy/install_generator.rb +3 -3
  130. data/lib/tasks/chewy.rake +99 -32
  131. data/migration_guide.md +56 -0
  132. data/spec/chewy/config_spec.rb +87 -15
  133. data/spec/chewy/fields/base_spec.rb +542 -233
  134. data/spec/chewy/fields/root_spec.rb +115 -17
  135. data/spec/chewy/fields/time_fields_spec.rb +13 -12
  136. data/spec/chewy/index/actions_spec.rb +595 -77
  137. data/spec/chewy/index/adapter/active_record_spec.rb +601 -0
  138. data/spec/chewy/index/adapter/object_spec.rb +243 -0
  139. data/spec/chewy/index/aliases_spec.rb +5 -5
  140. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  141. data/spec/chewy/index/import/bulk_request_spec.rb +95 -0
  142. data/spec/chewy/index/import/journal_builder_spec.rb +87 -0
  143. data/spec/chewy/index/import/routine_spec.rb +110 -0
  144. data/spec/chewy/index/import_spec.rb +615 -0
  145. data/spec/chewy/index/mapping_spec.rb +135 -0
  146. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  147. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  148. data/spec/chewy/index/observe_spec.rb +143 -0
  149. data/spec/chewy/index/settings_spec.rb +103 -50
  150. data/spec/chewy/index/specification_spec.rb +159 -0
  151. data/spec/chewy/index/syncer_spec.rb +118 -0
  152. data/spec/chewy/index/witchcraft_spec.rb +245 -0
  153. data/spec/chewy/index/wrapper_spec.rb +100 -0
  154. data/spec/chewy/index_spec.rb +149 -121
  155. data/spec/chewy/journal_spec.rb +223 -0
  156. data/spec/chewy/minitest/helpers_spec.rb +198 -0
  157. data/spec/chewy/minitest/search_index_receiver_spec.rb +118 -0
  158. data/spec/chewy/multi_search_spec.rb +84 -0
  159. data/spec/chewy/rake_helper_spec.rb +656 -0
  160. data/spec/chewy/repository_spec.rb +8 -8
  161. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  162. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  163. data/spec/chewy/rspec/update_index_spec.rb +220 -114
  164. data/spec/chewy/runtime_spec.rb +2 -2
  165. data/spec/chewy/search/loader_spec.rb +83 -0
  166. data/spec/chewy/search/pagination/kaminari_examples.rb +69 -0
  167. data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
  168. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  169. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  170. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  171. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  172. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  173. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  174. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  175. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  176. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  177. data/spec/chewy/search/parameters/indices_spec.rb +99 -0
  178. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  179. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  180. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  181. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  182. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  183. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  184. data/spec/chewy/search/parameters/order_spec.rb +72 -0
  185. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  186. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  187. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  188. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  189. data/spec/chewy/search/parameters/query_storage_examples.rb +434 -0
  190. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  191. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  192. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  193. data/spec/chewy/search/parameters/search_after_spec.rb +35 -0
  194. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  195. data/spec/chewy/search/parameters/source_spec.rb +162 -0
  196. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  197. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  198. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  199. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  200. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  201. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  202. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  203. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  204. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  205. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  206. data/spec/chewy/search/parameters_spec.rb +161 -0
  207. data/spec/chewy/search/query_proxy_spec.rb +119 -0
  208. data/spec/chewy/search/request_spec.rb +880 -0
  209. data/spec/chewy/search/response_spec.rb +202 -0
  210. data/spec/chewy/search/scrolling_spec.rb +171 -0
  211. data/spec/chewy/search_spec.rb +82 -55
  212. data/spec/chewy/stash_spec.rb +85 -0
  213. data/spec/chewy/strategy/active_job_spec.rb +27 -8
  214. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  215. data/spec/chewy/strategy/atomic_spec.rb +13 -11
  216. data/spec/chewy/strategy/delayed_sidekiq_spec.rb +190 -0
  217. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  218. data/spec/chewy/strategy/sidekiq_spec.rb +19 -7
  219. data/spec/chewy/strategy_spec.rb +19 -15
  220. data/spec/chewy_spec.rb +65 -88
  221. data/spec/spec_helper.rb +11 -20
  222. data/spec/support/active_record.rb +48 -6
  223. data/spec/support/class_helpers.rb +4 -19
  224. metadata +299 -183
  225. data/.travis.yml +0 -76
  226. data/Appraisals +0 -76
  227. data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
  228. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
  229. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
  230. data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
  231. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  232. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  233. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  234. data/gemfiles/rails.4.0.mongoid.4.0.0.kaminari.gemfile +0 -14
  235. data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
  236. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  237. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  238. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  239. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  240. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  241. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  242. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  243. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  244. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  245. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  246. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  247. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  248. data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
  249. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -15
  250. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
  251. data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
  252. data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
  253. data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
  254. data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
  255. data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
  256. data/gemfiles/rails.4.2.mongoid.5.1.0.will_paginate.gemfile +0 -14
  257. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  258. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  259. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
  260. data/gemfiles/sequel.4.31.gemfile +0 -13
  261. data/lib/chewy/backports/deep_dup.rb +0 -46
  262. data/lib/chewy/backports/duplicable.rb +0 -90
  263. data/lib/chewy/query/compose.rb +0 -69
  264. data/lib/chewy/query/criteria.rb +0 -181
  265. data/lib/chewy/query/filters.rb +0 -227
  266. data/lib/chewy/query/loading.rb +0 -111
  267. data/lib/chewy/query/nodes/and.rb +0 -25
  268. data/lib/chewy/query/nodes/base.rb +0 -17
  269. data/lib/chewy/query/nodes/bool.rb +0 -32
  270. data/lib/chewy/query/nodes/equal.rb +0 -34
  271. data/lib/chewy/query/nodes/exists.rb +0 -20
  272. data/lib/chewy/query/nodes/expr.rb +0 -28
  273. data/lib/chewy/query/nodes/field.rb +0 -106
  274. data/lib/chewy/query/nodes/has_child.rb +0 -14
  275. data/lib/chewy/query/nodes/has_parent.rb +0 -14
  276. data/lib/chewy/query/nodes/has_relation.rb +0 -61
  277. data/lib/chewy/query/nodes/match_all.rb +0 -11
  278. data/lib/chewy/query/nodes/missing.rb +0 -20
  279. data/lib/chewy/query/nodes/not.rb +0 -25
  280. data/lib/chewy/query/nodes/or.rb +0 -25
  281. data/lib/chewy/query/nodes/prefix.rb +0 -18
  282. data/lib/chewy/query/nodes/query.rb +0 -20
  283. data/lib/chewy/query/nodes/range.rb +0 -63
  284. data/lib/chewy/query/nodes/raw.rb +0 -15
  285. data/lib/chewy/query/nodes/regexp.rb +0 -31
  286. data/lib/chewy/query/nodes/script.rb +0 -20
  287. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  288. data/lib/chewy/query/pagination.rb +0 -16
  289. data/lib/chewy/query/scoping.rb +0 -20
  290. data/lib/chewy/query.rb +0 -1026
  291. data/lib/chewy/strategy/resque.rb +0 -26
  292. data/lib/chewy/type/actions.rb +0 -19
  293. data/lib/chewy/type/adapter/active_record.rb +0 -72
  294. data/lib/chewy/type/adapter/mongoid.rb +0 -58
  295. data/lib/chewy/type/adapter/object.rb +0 -89
  296. data/lib/chewy/type/adapter/orm.rb +0 -156
  297. data/lib/chewy/type/adapter/sequel.rb +0 -75
  298. data/lib/chewy/type/crutch.rb +0 -31
  299. data/lib/chewy/type/import.rb +0 -224
  300. data/lib/chewy/type/observe.rb +0 -76
  301. data/lib/chewy/type/wrapper.rb +0 -53
  302. data/lib/chewy/type.rb +0 -89
  303. data/lib/sequel/plugins/chewy_observe.rb +0 -78
  304. data/spec/chewy/query/criteria_spec.rb +0 -433
  305. data/spec/chewy/query/filters_spec.rb +0 -173
  306. data/spec/chewy/query/loading_spec.rb +0 -86
  307. data/spec/chewy/query/nodes/and_spec.rb +0 -16
  308. data/spec/chewy/query/nodes/bool_spec.rb +0 -22
  309. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  310. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  311. data/spec/chewy/query/nodes/has_child_spec.rb +0 -40
  312. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -40
  313. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  314. data/spec/chewy/query/nodes/missing_spec.rb +0 -15
  315. data/spec/chewy/query/nodes/not_spec.rb +0 -16
  316. data/spec/chewy/query/nodes/or_spec.rb +0 -16
  317. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  318. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  319. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  320. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  321. data/spec/chewy/query/nodes/regexp_spec.rb +0 -31
  322. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  323. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -57
  324. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -60
  325. data/spec/chewy/query/pagination_spec.rb +0 -36
  326. data/spec/chewy/query_spec.rb +0 -632
  327. data/spec/chewy/strategy/resque_spec.rb +0 -40
  328. data/spec/chewy/type/actions_spec.rb +0 -31
  329. data/spec/chewy/type/adapter/active_record_spec.rb +0 -317
  330. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -253
  331. data/spec/chewy/type/adapter/object_spec.rb +0 -139
  332. data/spec/chewy/type/adapter/sequel_spec.rb +0 -320
  333. data/spec/chewy/type/import_spec.rb +0 -433
  334. data/spec/chewy/type/mapping_spec.rb +0 -106
  335. data/spec/chewy/type/observe_spec.rb +0 -127
  336. data/spec/chewy/type/witchcraft_spec.rb +0 -154
  337. data/spec/chewy/type/wrapper_spec.rb +0 -58
  338. data/spec/chewy/type_spec.rb +0 -33
  339. data/spec/support/mongoid.rb +0 -81
  340. data/spec/support/sequel.rb +0 -75
@@ -3,7 +3,10 @@ require 'spec_helper'
3
3
  describe Chewy::Index::Actions do
4
4
  before { Chewy.massacre }
5
5
 
6
- before { stub_index :dummies }
6
+ before do
7
+ stub_index :dummies
8
+ stub_index :dummies_suffixed
9
+ end
7
10
 
8
11
  describe '.exists?' do
9
12
  specify { expect(DummiesIndex.exists?).to eq(false) }
@@ -25,26 +28,40 @@ describe Chewy::Index::Actions do
25
28
  end
26
29
 
27
30
  context do
28
- before { DummiesIndex.create '2013' }
31
+ before do
32
+ DummiesIndex.create '2013'
33
+ DummiesSuffixedIndex.create 'should_not_appear'
34
+ end
35
+
29
36
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(true) }
30
37
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(true) }
31
- specify { expect(DummiesIndex.aliases).to eq([]) }
38
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
32
39
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
33
40
  specify { expect(DummiesIndex.create('2013')).to eq(false) }
34
41
  specify { expect(DummiesIndex.create('2014')['acknowledged']).to eq(true) }
35
42
 
36
43
  context do
37
44
  before { DummiesIndex.create '2014' }
38
- specify { expect(DummiesIndex.indexes).to match_array(['dummies_2013', 'dummies_2014']) }
45
+
46
+ specify { expect(DummiesIndex.indexes).to match_array(%w[dummies_2013 dummies_2014]) }
39
47
  end
40
48
  end
41
49
 
42
50
  context do
43
- before { DummiesIndex.create '2013', alias: false }
51
+ before do
52
+ DummiesIndex.create '2013', alias: false
53
+ DummiesSuffixedIndex.create 'should_not_appear'
54
+ end
55
+
44
56
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
45
57
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(true) }
46
58
  specify { expect(DummiesIndex.aliases).to eq([]) }
47
59
  specify { expect(DummiesIndex.indexes).to eq([]) }
60
+ specify { expect(DummiesIndex.exists?).to eq(false) }
61
+ # Unfortunately, without alias we can't figure out that this dummies_2013 index is related to DummiesIndex
62
+ # it would be awesome to have the following specs passing
63
+ # specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
64
+ # specify { expect(DummiesIndex.exists?).to eq(true) }
48
65
  end
49
66
  end
50
67
 
@@ -53,46 +70,62 @@ describe Chewy::Index::Actions do
53
70
  specify { expect(DummiesIndex.create!('2013')['acknowledged']).to eq(true) }
54
71
 
55
72
  context do
56
- before { DummiesIndex.create }
73
+ before do
74
+ DummiesIndex.create
75
+ DummiesSuffixedIndex.create 'should_not_appear'
76
+ end
77
+
57
78
  specify do
58
- skip_on_version_gte('2.0', 'format of exception changed in 2.x')
59
- expect { DummiesIndex.create! }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/\[\[dummies\] already exists\]/)
79
+ expect do
80
+ DummiesIndex.create!
81
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/already exists.*dummies/)
60
82
  end
61
83
  specify do
62
- skip_on_version_lt('2.0', 'format of exception was changed')
63
- expect { DummiesIndex.create! }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/index_already_exists_exception.*dummies/)
84
+ expect do
85
+ DummiesIndex.create!('2013')
86
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/Invalid alias name \[dummies\]/)
64
87
  end
65
- specify { expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/Invalid alias name \[dummies\]/) }
66
88
  end
67
89
 
68
90
  context do
69
- before { DummiesIndex.create! '2013' }
91
+ before do
92
+ DummiesIndex.create! '2013'
93
+ DummiesSuffixedIndex.create! 'should_not_appear'
94
+ end
95
+
70
96
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(true) }
71
97
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(true) }
72
- specify { expect(DummiesIndex.aliases).to eq([]) }
98
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
73
99
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
74
100
  specify do
75
- skip_on_version_gte('2.0', 'format of exception changed in 2.x')
76
- expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/\[\[dummies_2013\] already exists\]/)
77
- end
78
- specify do
79
- skip_on_version_lt('2.0', 'format of exception was changed')
80
- expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/index_already_exists_exception.*dummies_2013/)
101
+ expect do
102
+ DummiesIndex.create!('2013')
103
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/already exists.*dummies_2013/)
81
104
  end
82
105
  specify { expect(DummiesIndex.create!('2014')['acknowledged']).to eq(true) }
83
106
 
84
107
  context do
85
108
  before { DummiesIndex.create! '2014' }
86
- specify { expect(DummiesIndex.indexes).to match_array(['dummies_2013', 'dummies_2014']) }
109
+
110
+ specify { expect(DummiesIndex.indexes).to match_array(%w[dummies_2013 dummies_2014]) }
87
111
  end
88
112
  end
89
113
 
90
114
  context do
91
- before { DummiesIndex.create! '2013', alias: false }
115
+ before do
116
+ DummiesIndex.create! '2013', alias: false
117
+ DummiesSuffixedIndex.create! 'should_not_appear'
118
+ end
119
+
92
120
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
93
121
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(true) }
94
122
  specify { expect(DummiesIndex.aliases).to eq([]) }
95
123
  specify { expect(DummiesIndex.indexes).to eq([]) }
124
+ specify { expect(DummiesIndex.exists?).to eq(false) }
125
+ # Unfortunately, without alias we can't figure out that this dummies_2013 index is related to DummiesIndex
126
+ # it would be awesome to have the following specs passing
127
+ # specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
128
+ # specify { expect(DummiesIndex.exists?).to eq(true) }
96
129
  end
97
130
  end
98
131
 
@@ -101,27 +134,40 @@ describe Chewy::Index::Actions do
101
134
  specify { expect(DummiesIndex.delete('dummies_2013')).to eq(false) }
102
135
 
103
136
  context do
104
- before { DummiesIndex.create }
137
+ before do
138
+ DummiesIndex.create
139
+ DummiesSuffixedIndex.create 'should_not_appear'
140
+ end
141
+
105
142
  specify { expect(DummiesIndex.delete['acknowledged']).to eq(true) }
106
143
 
107
144
  context do
108
145
  before { DummiesIndex.delete }
109
146
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
147
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
110
148
  end
111
149
  end
112
150
 
113
151
  context do
114
- before { DummiesIndex.create '2013' }
152
+ before do
153
+ DummiesIndex.create '2013'
154
+ DummiesSuffixedIndex.create 'should_not_appear'
155
+ end
156
+
115
157
  specify { expect(DummiesIndex.delete('2013')['acknowledged']).to eq(true) }
116
158
 
117
159
  context do
118
160
  before { DummiesIndex.delete('2013') }
119
161
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
120
162
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(false) }
163
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
121
164
  end
122
165
 
123
166
  context do
124
- before { DummiesIndex.create '2014' }
167
+ before do
168
+ DummiesIndex.create '2014'
169
+ end
170
+
125
171
  specify { expect(DummiesIndex.delete['acknowledged']).to eq(true) }
126
172
 
127
173
  context do
@@ -129,6 +175,7 @@ describe Chewy::Index::Actions do
129
175
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
130
176
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(false) }
131
177
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2014')).to eq(false) }
178
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
132
179
  end
133
180
 
134
181
  context do
@@ -136,6 +183,7 @@ describe Chewy::Index::Actions do
136
183
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(true) }
137
184
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(true) }
138
185
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2014')).to eq(false) }
186
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
139
187
  end
140
188
  end
141
189
  end
@@ -143,30 +191,45 @@ describe Chewy::Index::Actions do
143
191
 
144
192
  describe '.delete!' do
145
193
  specify { expect { DummiesIndex.delete! }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound) }
146
- specify { expect { DummiesIndex.delete!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound) }
194
+ specify do
195
+ expect do
196
+ DummiesIndex.delete!('2013')
197
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
198
+ end
147
199
 
148
200
  context do
149
- before { DummiesIndex.create }
201
+ before do
202
+ DummiesIndex.create
203
+ DummiesSuffixedIndex.create 'should_not_appear'
204
+ end
205
+
150
206
  specify { expect(DummiesIndex.delete!['acknowledged']).to eq(true) }
151
207
 
152
208
  context do
153
209
  before { DummiesIndex.delete! }
154
210
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
211
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
155
212
  end
156
213
  end
157
214
 
158
215
  context do
159
- before { DummiesIndex.create '2013' }
216
+ before do
217
+ DummiesIndex.create '2013'
218
+ DummiesSuffixedIndex.create 'should_not_appear'
219
+ end
220
+
160
221
  specify { expect(DummiesIndex.delete!('2013')['acknowledged']).to eq(true) }
161
222
 
162
223
  context do
163
224
  before { DummiesIndex.delete!('2013') }
164
225
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
165
226
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(false) }
227
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
166
228
  end
167
229
 
168
230
  context do
169
231
  before { DummiesIndex.create '2014' }
232
+
170
233
  specify { expect(DummiesIndex.delete!['acknowledged']).to eq(true) }
171
234
 
172
235
  context do
@@ -174,6 +237,7 @@ describe Chewy::Index::Actions do
174
237
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(false) }
175
238
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(false) }
176
239
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2014')).to eq(false) }
240
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
177
241
  end
178
242
 
179
243
  context do
@@ -181,6 +245,7 @@ describe Chewy::Index::Actions do
181
245
  specify { expect(Chewy.client.indices.exists(index: 'dummies')).to eq(true) }
182
246
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2013')).to eq(true) }
183
247
  specify { expect(Chewy.client.indices.exists(index: 'dummies_2014')).to eq(false) }
248
+ specify { expect(Chewy.client.indices.exists(index: 'dummies_suffixed')).to eq(true) }
184
249
  end
185
250
  end
186
251
  end
@@ -194,19 +259,19 @@ describe Chewy::Index::Actions do
194
259
  before { DummiesIndex.purge }
195
260
  specify { expect(DummiesIndex).to be_exists }
196
261
  specify { expect(DummiesIndex.aliases).to eq([]) }
197
- specify { expect(DummiesIndex.indexes).to eq([]) }
262
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
198
263
 
199
264
  context do
200
265
  before { DummiesIndex.purge }
201
266
  specify { expect(DummiesIndex).to be_exists }
202
267
  specify { expect(DummiesIndex.aliases).to eq([]) }
203
- specify { expect(DummiesIndex.indexes).to eq([]) }
268
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
204
269
  end
205
270
 
206
271
  context do
207
272
  before { DummiesIndex.purge('2013') }
208
273
  specify { expect(DummiesIndex).to be_exists }
209
- specify { expect(DummiesIndex.aliases).to eq([]) }
274
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
210
275
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
211
276
  end
212
277
  end
@@ -214,20 +279,20 @@ describe Chewy::Index::Actions do
214
279
  context do
215
280
  before { DummiesIndex.purge('2013') }
216
281
  specify { expect(DummiesIndex).to be_exists }
217
- specify { expect(DummiesIndex.aliases).to eq([]) }
282
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
218
283
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
219
284
 
220
285
  context do
221
286
  before { DummiesIndex.purge }
222
287
  specify { expect(DummiesIndex).to be_exists }
223
288
  specify { expect(DummiesIndex.aliases).to eq([]) }
224
- specify { expect(DummiesIndex.indexes).to eq([]) }
289
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
225
290
  end
226
291
 
227
292
  context do
228
293
  before { DummiesIndex.purge('2014') }
229
294
  specify { expect(DummiesIndex).to be_exists }
230
- specify { expect(DummiesIndex.aliases).to eq([]) }
295
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
231
296
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2014']) }
232
297
  end
233
298
  end
@@ -241,19 +306,19 @@ describe Chewy::Index::Actions do
241
306
  before { DummiesIndex.purge! }
242
307
  specify { expect(DummiesIndex).to be_exists }
243
308
  specify { expect(DummiesIndex.aliases).to eq([]) }
244
- specify { expect(DummiesIndex.indexes).to eq([]) }
309
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
245
310
 
246
311
  context do
247
312
  before { DummiesIndex.purge! }
248
313
  specify { expect(DummiesIndex).to be_exists }
249
314
  specify { expect(DummiesIndex.aliases).to eq([]) }
250
- specify { expect(DummiesIndex.indexes).to eq([]) }
315
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
251
316
  end
252
317
 
253
318
  context do
254
319
  before { DummiesIndex.purge!('2013') }
255
320
  specify { expect(DummiesIndex).to be_exists }
256
- specify { expect(DummiesIndex.aliases).to eq([]) }
321
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
257
322
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
258
323
  end
259
324
  end
@@ -261,20 +326,20 @@ describe Chewy::Index::Actions do
261
326
  context do
262
327
  before { DummiesIndex.purge!('2013') }
263
328
  specify { expect(DummiesIndex).to be_exists }
264
- specify { expect(DummiesIndex.aliases).to eq([]) }
329
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
265
330
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
266
331
 
267
332
  context do
268
333
  before { DummiesIndex.purge! }
269
334
  specify { expect(DummiesIndex).to be_exists }
270
335
  specify { expect(DummiesIndex.aliases).to eq([]) }
271
- specify { expect(DummiesIndex.indexes).to eq([]) }
336
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
272
337
  end
273
338
 
274
339
  context do
275
340
  before { DummiesIndex.purge!('2014') }
276
341
  specify { expect(DummiesIndex).to be_exists }
277
- specify { expect(DummiesIndex.aliases).to eq([]) }
342
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
278
343
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2014']) }
279
344
  end
280
345
  end
@@ -284,19 +349,30 @@ describe Chewy::Index::Actions do
284
349
  before do
285
350
  stub_model(:city)
286
351
  stub_index(:cities) do
287
- define_type City
352
+ index_scope City
288
353
  end
289
354
  end
290
- let!(:dummy_cities) { 3.times.map { |i| City.create(id: i + 1, name: "name#{i}") } }
355
+ let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}") } }
291
356
 
292
357
  specify { expect(CitiesIndex.import).to eq(true) }
293
358
 
359
+ specify 'with an empty array' do
360
+ expect(CitiesIndex).not_to receive(:exists?)
361
+ expect(CitiesIndex).not_to receive(:create!)
362
+ expect(CitiesIndex.import([])).to eq(true)
363
+ end
364
+
365
+ specify 'with an empty relation' do
366
+ expect(CitiesIndex).not_to receive(:exists?)
367
+ expect(CitiesIndex).not_to receive(:create!)
368
+ expect(CitiesIndex.import(City.where('1 = 2'))).to eq(true)
369
+ end
370
+
294
371
  context do
295
372
  before do
296
373
  stub_index(:cities) do
297
- define_type City do
298
- field :name, type: 'object'
299
- end
374
+ index_scope City
375
+ field :name, type: 'object'
300
376
  end
301
377
  end
302
378
 
@@ -308,19 +384,30 @@ describe Chewy::Index::Actions do
308
384
  before do
309
385
  stub_model(:city)
310
386
  stub_index(:cities) do
311
- define_type City
387
+ index_scope City
312
388
  end
313
389
  end
314
- let!(:dummy_cities) { 3.times.map { |i| City.create(id: i + 1, name: "name#{i}") } }
390
+ let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}") } }
315
391
 
316
392
  specify { expect(CitiesIndex.import!).to eq(true) }
317
393
 
394
+ specify 'with an empty array' do
395
+ expect(CitiesIndex).not_to receive(:exists?)
396
+ expect(CitiesIndex).not_to receive(:create!)
397
+ expect(CitiesIndex.import!([])).to eq(true)
398
+ end
399
+
400
+ specify 'with an empty relation' do
401
+ expect(CitiesIndex).not_to receive(:exists?)
402
+ expect(CitiesIndex).not_to receive(:create!)
403
+ expect(CitiesIndex.import!(City.where('1 = 2'))).to eq(true)
404
+ end
405
+
318
406
  context do
319
407
  before do
320
408
  stub_index(:cities) do
321
- define_type City do
322
- field :name, type: 'object'
323
- end
409
+ index_scope City
410
+ field :name, type: 'object'
324
411
  end
325
412
  end
326
413
 
@@ -332,62 +419,493 @@ describe Chewy::Index::Actions do
332
419
  before do
333
420
  stub_model(:city)
334
421
  stub_index(:cities) do
335
- define_type City
422
+ index_scope City
336
423
  end
337
424
  end
338
425
 
339
- before { City.create!(id: 1, name: 'Moscow') }
340
-
341
- specify { expect(CitiesIndex.reset!).to eq(true) }
342
- specify { expect(CitiesIndex.reset!('2013')).to eq(true) }
343
-
344
426
  context do
345
- before { CitiesIndex.reset! }
427
+ before { City.create!(id: 1, name: 'Moscow') }
346
428
 
347
- specify { expect(CitiesIndex.all).to have(1).item }
348
- specify { expect(CitiesIndex.aliases).to eq([]) }
349
- specify { expect(CitiesIndex.indexes).to eq([]) }
429
+ specify { expect(CitiesIndex.reset!).to eq(true) }
430
+ specify { expect(CitiesIndex.reset!('2013')).to eq(true) }
350
431
 
351
432
  context do
352
- before { CitiesIndex.reset!('2013') }
433
+ before { CitiesIndex.reset! }
353
434
 
354
435
  specify { expect(CitiesIndex.all).to have(1).item }
355
436
  specify { expect(CitiesIndex.aliases).to eq([]) }
356
- specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
437
+ specify { expect(CitiesIndex.indexes).to eq(['cities']) }
438
+
439
+ context do
440
+ before { CitiesIndex.reset!('2013') }
441
+
442
+ specify { expect(CitiesIndex.all).to have(1).item }
443
+ specify { expect(CitiesIndex.aliases).to eq(['cities']) }
444
+ specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
445
+ end
446
+
447
+ context do
448
+ before { CitiesIndex.reset! }
449
+
450
+ specify { expect(CitiesIndex.all).to have(1).item }
451
+ specify { expect(CitiesIndex.aliases).to eq([]) }
452
+ specify { expect(CitiesIndex.indexes).to eq(['cities']) }
453
+ end
357
454
  end
358
455
 
359
456
  context do
360
- before { CitiesIndex.reset! }
457
+ before { CitiesIndex.reset!('2013') }
361
458
 
362
459
  specify { expect(CitiesIndex.all).to have(1).item }
363
- specify { expect(CitiesIndex.aliases).to eq([]) }
364
- specify { expect(CitiesIndex.indexes).to eq([]) }
460
+ specify { expect(CitiesIndex.aliases).to eq(['cities']) }
461
+ specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
462
+
463
+ context do
464
+ before { CitiesIndex.reset!('2014') }
465
+
466
+ specify { expect(CitiesIndex.all).to have(1).item }
467
+ specify { expect(CitiesIndex.aliases).to eq(['cities']) }
468
+ specify { expect(CitiesIndex.indexes).to eq(['cities_2014']) }
469
+ specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
470
+ end
471
+
472
+ context do
473
+ before { CitiesIndex.reset! }
474
+
475
+ specify { expect(CitiesIndex.all).to have(1).item }
476
+ specify { expect(CitiesIndex.aliases).to eq([]) }
477
+ specify { expect(CitiesIndex.indexes).to eq(['cities']) }
478
+ specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
479
+ end
365
480
  end
366
481
  end
367
482
 
368
- context do
483
+ context 'reset_disable_refresh_interval' do
484
+ let(:suffix) { Time.now.to_i }
485
+ let(:name) { CitiesIndex.index_name(suffix: suffix) }
486
+ let(:before_import_body) do
487
+ {
488
+ index: {refresh_interval: -1}
489
+ }
490
+ end
491
+ let(:after_import_body) do
492
+ {
493
+ index: {refresh_interval: '1s'}
494
+ }
495
+ end
496
+
369
497
  before { CitiesIndex.reset!('2013') }
498
+ before { allow(Chewy).to receive(:reset_disable_refresh_interval).and_return(reset_disable_refresh_interval) }
499
+
500
+ context 'activated' do
501
+ let(:reset_disable_refresh_interval) { true }
502
+ specify do
503
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: before_import_body).once
504
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: after_import_body).once
505
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: false).and_call_original
506
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
507
+ end
370
508
 
371
- specify { expect(CitiesIndex.all).to have(1).item }
372
- specify { expect(CitiesIndex.aliases).to eq([]) }
373
- specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
509
+ context 'refresh_interval already defined' do
510
+ before do
511
+ stub_index(:cities) do
512
+ settings index: {refresh_interval: '2s'}
513
+ index_scope City
514
+ end
515
+ end
374
516
 
375
- context do
376
- before { CitiesIndex.reset!('2014') }
517
+ let(:after_import_body) do
518
+ {
519
+ index: {refresh_interval: '2s'}
520
+ }
521
+ end
377
522
 
378
- specify { expect(CitiesIndex.all).to have(1).item }
379
- specify { expect(CitiesIndex.aliases).to eq([]) }
380
- specify { expect(CitiesIndex.indexes).to eq(['cities_2014']) }
381
- specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
523
+ specify do
524
+ expect(CitiesIndex.client.indices)
525
+ .to receive(:put_settings).with(index: name, body: before_import_body).once
526
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: after_import_body).once
527
+ expect(CitiesIndex)
528
+ .to receive(:import).with(suffix: suffix, journal: false, refresh: false).and_call_original
529
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
530
+ end
531
+
532
+ specify 'uses empty index settings if not defined' do
533
+ allow(Chewy).to receive(:wait_for_status).and_return(nil)
534
+ allow(CitiesIndex).to receive(:settings_hash).and_return({})
535
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
536
+ end
537
+ end
538
+ end
539
+
540
+ context 'not activated' do
541
+ let(:reset_disable_refresh_interval) { false }
542
+ specify do
543
+ expect(CitiesIndex.client.indices).not_to receive(:put_settings)
544
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: true).and_call_original
545
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
546
+ end
547
+ end
548
+ end
549
+
550
+ context 'reset_no_replicas' do
551
+ let(:suffix) { Time.now.to_i }
552
+ let(:name) { CitiesIndex.index_name(suffix: suffix) }
553
+ let(:before_import_body) do
554
+ {
555
+ index: {number_of_replicas: 0}
556
+ }
557
+ end
558
+ let(:after_import_body) do
559
+ {
560
+ index: {number_of_replicas: 0}
561
+ }
562
+ end
563
+
564
+ before { allow(Chewy).to receive(:reset_no_replicas).and_return(reset_no_replicas) }
565
+
566
+ context 'activated' do
567
+ let(:reset_no_replicas) { true }
568
+ specify do
569
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: before_import_body).once
570
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: after_import_body).once
571
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: true).and_call_original
572
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
573
+ end
574
+ end
575
+
576
+ context 'not activated' do
577
+ let(:reset_no_replicas) { false }
578
+ specify do
579
+ expect(CitiesIndex.client.indices).not_to receive(:put_settings)
580
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: true).and_call_original
581
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
582
+ end
583
+ end
584
+ end
585
+
586
+ xcontext 'applying journal' do
587
+ before do
588
+ stub_index(:cities) do
589
+ index_scope City
590
+ field :name, value: (lambda do
591
+ sleep(rating)
592
+ name
593
+ end)
594
+ end
595
+ end
596
+
597
+ let!(:cities) { Array.new(3) { |i| City.create!(id: i + 1, name: "Name#{i + 1}", rating: 1) } }
598
+
599
+ let(:parallel_update) do
600
+ Thread.new do
601
+ p 'start parallel'
602
+ sleep(1.5)
603
+ cities.first.update(name: 'NewName1', rating: 0)
604
+ cities.last.update(name: 'NewName3', rating: 0)
605
+ CitiesIndex::City.import!([cities.first, cities.last], journal: true)
606
+ p 'end parallel'
607
+ end
608
+ end
609
+
610
+ specify 'with journal application' do
611
+ cities
612
+ p 'cities created1'
613
+ ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
614
+ [
615
+ parallel_update,
616
+ Thread.new do
617
+ p 'start reset1'
618
+ CitiesIndex.reset!('suffix')
619
+ p 'end reset1'
620
+ end
621
+ ].map(&:join)
622
+ ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
623
+ p 'expect1'
624
+ expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 NewName1], %w[2 Name2], %w[3 NewName3])
625
+ p 'end expect1'
382
626
  end
383
627
 
628
+ specify 'without journal application' do
629
+ cities
630
+ p 'cities created2'
631
+ ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
632
+ [
633
+ parallel_update,
634
+ Thread.new do
635
+ p 'start reset2'
636
+ CitiesIndex.reset!('suffix', apply_journal: false)
637
+ p 'end reset2'
638
+ end
639
+ ].map(&:join)
640
+ ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
641
+ p 'expect2'
642
+ expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 Name1], %w[2 Name2], %w[3 Name3])
643
+ p 'end expect2'
644
+ end
645
+ end
646
+
647
+ context 'journaling' do
648
+ before { City.create!(id: 1, name: 'Moscow') }
649
+
650
+ specify do
651
+ CitiesIndex.reset!
652
+ expect(Chewy::Stash::Journal.count).to eq(0)
653
+ end
654
+
655
+ specify do
656
+ CitiesIndex.reset! journal: true
657
+ expect(Chewy::Stash::Journal.count).to be > 0
658
+ end
659
+ end
660
+
661
+ context 'other options' do
662
+ specify do
663
+ expect(CitiesIndex).to receive(:import).with(parallel: true, journal: false).once.and_return(true)
664
+ expect(CitiesIndex.reset!(parallel: true)).to eq(true)
665
+ end
666
+
667
+ specify do
668
+ expect(CitiesIndex)
669
+ .to receive(:import)
670
+ .with(suffix: 'suffix', parallel: true, journal: false, refresh: true)
671
+ .once.and_return(true)
672
+ expect(CitiesIndex.reset!('suffix', parallel: true)).to eq(true)
673
+ end
674
+ end
675
+ end
676
+
677
+ describe '.reset' do
678
+ before do
679
+ stub_model(:city)
680
+ stub_index(:cities) do
681
+ index_scope City
682
+ end
683
+ end
684
+
685
+ context do
686
+ before { City.create!(id: 1, name: 'Moscow') }
687
+
688
+ specify { expect(CitiesIndex.reset).to eq(true) }
689
+ specify { expect(CitiesIndex.reset('2013')).to eq(true) }
690
+
384
691
  context do
385
- before { CitiesIndex.reset! }
692
+ before { CitiesIndex.reset }
386
693
 
387
694
  specify { expect(CitiesIndex.all).to have(1).item }
388
695
  specify { expect(CitiesIndex.aliases).to eq([]) }
389
- specify { expect(CitiesIndex.indexes).to eq([]) }
390
- specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
696
+ specify { expect(CitiesIndex.indexes).to eq(['cities']) }
697
+ end
698
+ end
699
+ end
700
+
701
+ describe '.sync' do
702
+ before do
703
+ stub_model(:city)
704
+ stub_index(:cities) do
705
+ index_scope City
706
+ field :name
707
+ field :updated_at, type: 'date'
708
+ end
709
+ end
710
+
711
+ let!(:cities) { Array.new(3) { |i| City.create!(name: "Name#{i + 1}") } }
712
+
713
+ before do
714
+ CitiesIndex.import
715
+ cities.first.destroy
716
+ cities.last.update(name: 'Name5')
717
+ end
718
+
719
+ let!(:additional_city) { City.create!(name: 'Name4') }
720
+
721
+ specify do
722
+ expect(CitiesIndex.sync).to match(
723
+ count: 3,
724
+ missing: contain_exactly(cities.first.id.to_s, additional_city.id.to_s),
725
+ outdated: [cities.last.id.to_s]
726
+ )
727
+ end
728
+ specify do
729
+ expect { CitiesIndex.sync }.to update_index(CitiesIndex)
730
+ .and_reindex(additional_city, cities.last)
731
+ .and_delete(cities.first).only
732
+ end
733
+ end
734
+
735
+ describe '.journal' do
736
+ specify { expect(DummiesIndex.journal).to be_a(Chewy::Journal) }
737
+ end
738
+
739
+ describe '.clear_cache' do
740
+ before do
741
+ stub_model(:city)
742
+ stub_index(:cities) do
743
+ index_scope City
744
+ end
745
+ end
746
+
747
+ let(:index_name) { 'test_index' }
748
+ let(:index_name_with_prefix) { 'cities_test_index' }
749
+ let(:unexisted_index_name) { 'wrong_index' }
750
+
751
+ context 'with existing index' do
752
+ before do
753
+ CitiesIndex.create(index_name)
754
+ end
755
+
756
+ specify do
757
+ expect(CitiesIndex)
758
+ .to receive(:clear_cache)
759
+ .and_call_original
760
+ expect { CitiesIndex.clear_cache({index: index_name_with_prefix}) }
761
+ .not_to raise_error
762
+ end
763
+ end
764
+
765
+ context 'with unexisting index' do
766
+ specify do
767
+ expect(CitiesIndex)
768
+ .to receive(:clear_cache)
769
+ .and_call_original
770
+ expect { CitiesIndex.clear_cache({index: unexisted_index_name}) }
771
+ .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound
772
+ end
773
+ end
774
+
775
+ context 'without arguments' do
776
+ before do
777
+ CitiesIndex.create
778
+ end
779
+
780
+ specify do
781
+ expect(CitiesIndex)
782
+ .to receive(:clear_cache)
783
+ .and_call_original
784
+ expect { CitiesIndex.clear_cache }
785
+ .not_to raise_error
786
+ end
787
+ end
788
+ end
789
+
790
+ describe '.reindex' do
791
+ before do
792
+ stub_model(:city)
793
+ stub_index(:cities) do
794
+ index_scope City
795
+ end
796
+ CitiesIndex.create(source_index)
797
+ DummiesIndex.create(dest_index)
798
+ end
799
+
800
+ let(:source_index) { 'source_index' }
801
+ let(:source_index_with_prefix) { 'cities_source_index' }
802
+ let(:dest_index) { 'dest_index' }
803
+ let(:dest_index_with_prefix) { 'dummies_dest_index' }
804
+ let(:unexisting_index) { 'wrong_index' }
805
+
806
+ context 'with existing indexes' do
807
+ specify do
808
+ expect(CitiesIndex)
809
+ .to receive(:reindex)
810
+ .and_call_original
811
+ expect { CitiesIndex.reindex(source: source_index_with_prefix, dest: dest_index_with_prefix) }
812
+ .not_to raise_error
813
+ end
814
+ end
815
+
816
+ context 'with unexisting indexes' do
817
+ context 'source index' do
818
+ specify do
819
+ expect(CitiesIndex)
820
+ .to receive(:reindex)
821
+ .and_call_original
822
+ expect { CitiesIndex.reindex(source: unexisting_index, dest: dest_index_with_prefix) }
823
+ .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound
824
+ end
825
+ end
826
+
827
+ context 'dest index' do
828
+ specify do
829
+ expect(CitiesIndex)
830
+ .to receive(:reindex)
831
+ .and_call_original
832
+ expect { CitiesIndex.reindex(source: source_index_with_prefix, dest: unexisting_index) }
833
+ .not_to raise_error
834
+ end
835
+ end
836
+ end
837
+
838
+ context 'with missing indexes' do
839
+ context 'without dest index' do
840
+ specify do
841
+ expect(DummiesIndex)
842
+ .to receive(:reindex)
843
+ .and_call_original
844
+ expect { DummiesIndex.reindex(source: source_index_with_prefix) }
845
+ .not_to raise_error
846
+ end
847
+ end
848
+
849
+ context 'without source index' do
850
+ specify do
851
+ expect(CitiesIndex)
852
+ .to receive(:reindex)
853
+ .and_call_original
854
+ expect { CitiesIndex.reindex(dest: dest_index_with_prefix) }
855
+ .not_to raise_error
856
+ end
857
+ end
858
+ end
859
+ end
860
+
861
+ describe 'update_mapping' do
862
+ before do
863
+ stub_model(:city)
864
+ stub_index(:cities) do
865
+ index_scope City
866
+ end
867
+ CitiesIndex.create
868
+ end
869
+
870
+ let(:index_name) { CitiesIndex.index_name }
871
+ let(:body_hash) { {properties: {new_field: {type: :text}}} }
872
+ let(:unexisting_index) { 'wrong_index' }
873
+ let(:empty_body_hash) { {} }
874
+
875
+ context 'with existing index' do
876
+ specify do
877
+ expect { CitiesIndex.update_mapping(index_name, body_hash) }
878
+ .not_to raise_error
879
+ end
880
+ end
881
+
882
+ context 'with unexisting arguments' do
883
+ context 'index name' do
884
+ specify do
885
+ expect { CitiesIndex.update_mapping(unexisting_index, body_hash) }
886
+ .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound
887
+ end
888
+ end
889
+
890
+ context 'body hash' do
891
+ specify do
892
+ expect { CitiesIndex.update_mapping(index_name, empty_body_hash) }
893
+ .not_to raise_error
894
+ end
895
+ end
896
+ end
897
+
898
+ context 'with only argument' do
899
+ specify do
900
+ expect { CitiesIndex.update_mapping(index_name) }
901
+ .not_to raise_error
902
+ end
903
+ end
904
+
905
+ context 'without arguments' do
906
+ specify do
907
+ expect { CitiesIndex.update_mapping }
908
+ .not_to raise_error
391
909
  end
392
910
  end
393
911
  end