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/type.rb CHANGED
@@ -1,19 +1,20 @@
1
1
  require 'chewy/search'
2
+ require 'chewy/type/adapter/object'
3
+ require 'chewy/type/adapter/active_record'
4
+ require 'chewy/type/adapter/mongoid'
5
+ require 'chewy/type/adapter/sequel'
2
6
  require 'chewy/type/mapping'
3
7
  require 'chewy/type/wrapper'
4
8
  require 'chewy/type/observe'
5
9
  require 'chewy/type/actions'
10
+ require 'chewy/type/syncer'
6
11
  require 'chewy/type/crutch'
7
12
  require 'chewy/type/import'
8
13
  require 'chewy/type/witchcraft'
9
- require 'chewy/type/adapter/object'
10
- require 'chewy/type/adapter/active_record'
11
- require 'chewy/type/adapter/mongoid'
12
- require 'chewy/type/adapter/sequel'
13
14
 
14
15
  module Chewy
15
16
  class Type
16
- IMPORT_OPTIONS_KEYS = [:batch_size, :bulk_size, :refresh, :consistency, :replication]
17
+ IMPORT_OPTIONS_KEYS = %i[batch_size bulk_size refresh consistency replication raw_import journal pipeline].freeze
17
18
 
18
19
  include Search
19
20
  include Mapping
@@ -24,66 +25,93 @@ module Chewy
24
25
  include Witchcraft
25
26
  include Import
26
27
 
27
- singleton_class.delegate :index_name, :client, to: :index
28
+ singleton_class.delegate :index_name, :derivable_index_name, :client, to: :index
28
29
 
29
30
  class_attribute :_default_import_options
30
31
  self._default_import_options = {}
31
32
 
32
- # Chewy index current type belongs to. Defined inside `Chewy.create_type`
33
- #
34
- def self.index
35
- raise NotImplementedError
36
- end
33
+ class << self
34
+ # Chewy index current type belongs to. Defined inside `Chewy.create_type`
35
+ #
36
+ def index
37
+ raise NotImplementedError, 'Looks like this type was defined outside the index scope and `.index` method is undefined for it'
38
+ end
37
39
 
38
- # Current type adapter. Defined inside `Chewy.create_type`, derived from
39
- # `Chewy::Index.define_type` arguments.
40
- #
41
- def self.adapter
42
- raise NotImplementedError
43
- end
40
+ # Current type adapter. Defined inside `Chewy.create_type`, derived from
41
+ # `Chewy::Index.define_type` arguments.
42
+ #
43
+ def adapter
44
+ raise NotImplementedError
45
+ end
44
46
 
45
- # Returns type name string
46
- #
47
- def self.type_name
48
- adapter.type_name
49
- end
47
+ # Returns type name string
48
+ #
49
+ def type_name
50
+ adapter.type_name
51
+ end
50
52
 
51
- # Returns list of public class methods defined in current type
52
- #
53
- def self.scopes
54
- public_methods - Chewy::Type.public_methods
55
- end
53
+ # Appends type name to {Chewy::Index.derivable_name}
54
+ #
55
+ # @example
56
+ # class Namespace::UsersIndex < Chewy::Index
57
+ # define_type User
58
+ # end
59
+ # UsersIndex::User.derivable_name # => 'namespace/users#user'
60
+ #
61
+ # @see Chewy::Index.derivable_name
62
+ # @return [String, nil] derivable name or nil when it is impossible to calculate
63
+ def derivable_name
64
+ @derivable_name ||= [index.derivable_name, type_name].join('#') if index && index.derivable_name
65
+ end
56
66
 
57
- def self.default_import_options(params)
58
- params.assert_valid_keys(IMPORT_OPTIONS_KEYS)
59
- self._default_import_options = _default_import_options.merge(params)
60
- end
67
+ # This method is an API shared with {Chewy::Index}, added for convenience.
68
+ #
69
+ # @return [Chewy::Type] array containing itself
70
+ def types
71
+ [self]
72
+ end
61
73
 
62
- def self.method_missing(method, *args, &block)
63
- if index.scopes.include?(method)
64
- define_singleton_method method do |*args, &block|
65
- all.scoping { index.public_send(method, *args, &block) }
74
+ # Returns list of public class methods defined in current type
75
+ #
76
+ def scopes
77
+ public_methods - Chewy::Type.public_methods
78
+ end
79
+
80
+ def default_import_options(params)
81
+ params.assert_valid_keys(IMPORT_OPTIONS_KEYS)
82
+ self._default_import_options = _default_import_options.merge(params)
83
+ end
84
+
85
+ def method_missing(method, *args, &block)
86
+ if index.scopes.include?(method)
87
+ define_singleton_method method do |*method_args, &method_block|
88
+ all.scoping { index.public_send(method, *method_args, &method_block) }
89
+ end
90
+ send(method, *args, &block)
91
+ else
92
+ super
66
93
  end
67
- send(method, *args, &block)
68
- else
69
- super
70
94
  end
71
- end
72
95
 
73
- def self.const_missing(name)
74
- to_resolve = "#{self.to_s}::#{name}"
75
- to_resolve[index.to_s] = ''
96
+ def respond_to_missing?(method, _)
97
+ index.scopes.include?(method) || super
98
+ end
99
+
100
+ def const_missing(name)
101
+ to_resolve = "#{self}::#{name}"
102
+ to_resolve[index.to_s] = ''
76
103
 
77
- @__resolved_constants ||= {}
104
+ @__resolved_constants ||= {}
78
105
 
79
- if to_resolve.empty? || @__resolved_constants[to_resolve]
106
+ if to_resolve.empty? || @__resolved_constants[to_resolve]
107
+ super
108
+ else
109
+ @__resolved_constants[to_resolve] = true
110
+ to_resolve.constantize
111
+ end
112
+ rescue NotImplementedError
80
113
  super
81
- else
82
- @__resolved_constants[to_resolve] = true
83
- to_resolve.constantize
84
114
  end
85
- rescue NotImplementedError
86
- super
87
115
  end
88
116
  end
89
117
  end
data/lib/chewy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chewy
2
- VERSION = '0.8.4'
2
+ VERSION = '5.1.0'.freeze
3
3
  end
data/lib/chewy.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'active_support/version'
1
2
  require 'active_support/concern'
2
3
  require 'active_support/deprecation'
3
4
  require 'active_support/json'
@@ -16,9 +17,32 @@ require 'active_support/core_ext/string/inflections'
16
17
  require 'i18n/core_ext/hash'
17
18
  require 'chewy/backports/deep_dup' unless Object.respond_to?(:deep_dup)
18
19
  require 'singleton'
20
+ require 'base64'
19
21
 
20
22
  require 'elasticsearch'
21
23
 
24
+ def try_require(path)
25
+ require path
26
+ rescue LoadError
27
+ nil
28
+ end
29
+
30
+ try_require 'kaminari'
31
+ try_require 'kaminari/core'
32
+ try_require 'will_paginate'
33
+ try_require 'will_paginate/collection'
34
+ try_require 'parallel'
35
+
36
+ ActiveSupport.on_load(:active_record) do
37
+ try_require 'will_paginate/active_record'
38
+ try_require 'kaminari/activerecord'
39
+ end
40
+
41
+ ActiveSupport.on_load(:mongoid) do
42
+ try_require 'will_paginate/mongoid'
43
+ try_require 'kaminari/mongoid'
44
+ end
45
+
22
46
  require 'chewy/version'
23
47
  require 'chewy/errors'
24
48
  require 'chewy/config'
@@ -31,44 +55,24 @@ require 'chewy/index'
31
55
  require 'chewy/type'
32
56
  require 'chewy/fields/base'
33
57
  require 'chewy/fields/root'
34
- require 'chewy/railtie' if defined?(::Rails)
35
-
36
- begin
37
- require 'kaminari'
38
- require 'chewy/query/pagination/kaminari'
39
- rescue LoadError
40
- end
41
-
42
- begin
43
- require 'will_paginate'
44
- require 'will_paginate/collection'
45
- require 'chewy/query/pagination/will_paginate'
46
- rescue LoadError
47
- end
58
+ require 'chewy/journal'
59
+ require 'chewy/railtie' if defined?(::Rails::Railtie)
48
60
 
49
61
  ActiveSupport.on_load(:active_record) do
50
62
  extend Chewy::Type::Observe::ActiveRecordMethods
51
-
52
- begin
53
- require 'will_paginate/active_record'
54
- rescue LoadError
55
- end
56
63
  end
57
64
 
58
65
  ActiveSupport.on_load(:mongoid) do
59
- module Mongoid::Document::ClassMethods
60
- include Chewy::Type::Observe::MongoidMethods
61
- end
62
-
63
- begin
64
- require 'will_paginate/mongoid'
65
- require 'chewy/query/pagination/will_paginate'
66
- rescue LoadError
66
+ module Mongoid
67
+ module Document
68
+ module ClassMethods
69
+ include Chewy::Type::Observe::MongoidMethods
70
+ end
71
+ end
67
72
  end
68
73
  end
69
74
 
70
75
  module Chewy
71
-
72
76
  @adapters = [
73
77
  Chewy::Type::Adapter::ActiveRecord,
74
78
  Chewy::Type::Adapter::Mongoid,
@@ -79,44 +83,62 @@ module Chewy
79
83
  class << self
80
84
  attr_accessor :adapters
81
85
 
82
- # Derives type from string `index#type` representation:
83
- #
84
- # Chewy.derive_type('users#user') # => UsersIndex::User
85
- #
86
- # If index has only one type - it is possible to derive it without specification:
86
+ # Derives a single type for the passed string identifier if possible.
87
87
  #
88
- # Chewy.derive_type('users') # => UsersIndex::User
88
+ # @example
89
+ # Chewy.derive_types(UsersIndex::User) # => UsersIndex::User
90
+ # Chewy.derive_types('namespace/users') # => Namespace::UsersIndex::User
91
+ # Chewy.derive_types('places') # => raises Chewy::UnderivableType
92
+ # Chewy.derive_types('places#city') # => PlacesIndex::City
89
93
  #
90
- # If index has more then one type - it raises Chewy::UnderivableType.
91
- #
92
- def derive_type name
94
+ # @param name [String, Chewy::Type] string type identifier
95
+ # @raise [Chewy::UnderivableType] in cases when it is impossble to find index or type or more than one type found
96
+ # @return [Chewy::Type] an array of derived types
97
+ def derive_type(name)
93
98
  return name if name.is_a?(Class) && name < Chewy::Type
94
99
 
95
- index_name, type_name = name.split('#', 2)
96
- class_name = "#{index_name.camelize}Index"
100
+ types = derive_types(name)
101
+ raise Chewy::UnderivableType, "Index `#{types.first.index}` has more than one type, please specify type via `#{types.first.index.derivable_name}#type_name`" unless types.one?
102
+ types.first
103
+ end
104
+
105
+ # Derives all the types for the passed string identifier if possible.
106
+ #
107
+ # @example
108
+ # Chewy.derive_types('namespace/users') # => [Namespace::UsersIndex::User]
109
+ # Chewy.derive_types('places') # => [PlacesIndex::City, PlacesIndex::Country]
110
+ # Chewy.derive_types('places#city') # => [PlacesIndex::City]
111
+ #
112
+ # @param from [String] string type identifier
113
+ # @raise [Chewy::UnderivableType] in cases when it is impossible to find index or type
114
+ # @return [Array<Chewy::Type>] an array of derived types
115
+ def derive_types(from)
116
+ return from.types if from.is_a?(Class) && (from < Chewy::Index || from < Chewy::Type)
117
+
118
+ index_name, type_name = from.split('#', 2)
119
+ class_name = "#{index_name.camelize.gsub(/Index\z/, '')}Index"
97
120
  index = class_name.safe_constantize
98
- raise Chewy::UnderivableType.new("Can not find index named `#{class_name}`") unless index && index < Chewy::Index
99
- type = if type_name.present?
100
- index.type_hash[type_name] or raise Chewy::UnderivableType.new("Index `#{class_name}` doesn`t have type named `#{type_name}`")
101
- elsif index.types.one?
102
- index.types.first
121
+ raise Chewy::UnderivableType, "Can not find index named `#{class_name}`" unless index && index < Chewy::Index
122
+ if type_name.present?
123
+ type = index.type_hash[type_name] or raise Chewy::UnderivableType, "Index `#{class_name}` doesn`t have type named `#{type_name}`"
124
+ [type]
103
125
  else
104
- raise Chewy::UnderivableType.new("Index `#{class_name}` has more than one type, please specify type via `#{index_name}#type_name`")
126
+ index.types
105
127
  end
106
128
  end
107
129
 
108
130
  # Creates Chewy::Type ancestor defining index and adapter methods.
109
131
  #
110
- def create_type index, target, options = {}, &block
132
+ def create_type(index, target, options = {}, &block)
111
133
  type = Class.new(Chewy::Type)
112
134
 
113
- adapter = adapters.find { |adapter| adapter.accepts?(target) }.new(target, options)
135
+ adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
114
136
 
115
137
  index.const_set(adapter.name, type)
116
138
  type.send(:define_singleton_method, :index) { index }
117
139
  type.send(:define_singleton_method, :adapter) { adapter }
118
140
 
119
- type.class_eval &block if block
141
+ type.class_eval(&block) if block
120
142
  type
121
143
  end
122
144
 
@@ -144,7 +166,7 @@ module Chewy
144
166
  # Be careful, if current prefix is blank, this will destroy all the indexes.
145
167
  #
146
168
  def massacre
147
- Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].delete_if(&:blank?).join(?_))
169
+ Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].reject(&:blank?).join('_'))
148
170
  Chewy.wait_for_status
149
171
  end
150
172
  alias_method :delete_all, :massacre
@@ -172,7 +194,7 @@ module Chewy
172
194
  # Chewy.strategy.pop
173
195
  # city3.do_update! # index updated again
174
196
  #
175
- def strategy name = nil, &block
197
+ def strategy(name = nil, &block)
176
198
  Thread.current[:chewy_strategy] ||= Chewy::Strategy.new
177
199
  if name
178
200
  if block
@@ -188,11 +210,32 @@ module Chewy
188
210
  def config
189
211
  Chewy::Config.instance
190
212
  end
191
- delegate *Chewy::Config.delegated, to: :config
213
+ delegate(*Chewy::Config.delegated, to: :config)
192
214
 
193
215
  def repository
194
216
  Chewy::Repository.instance
195
217
  end
196
- delegate *Chewy::Repository.delegated, to: :repository
218
+ delegate(*Chewy::Repository.delegated, to: :repository)
219
+
220
+ def create_indices
221
+ Chewy::Index.descendants.each(&:create)
222
+ end
223
+
224
+ def create_indices!
225
+ Chewy::Index.descendants.each(&:create!)
226
+ end
227
+
228
+ def eager_load!
229
+ return unless defined?(Chewy::Railtie)
230
+ dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[Chewy.configuration[:indices_path]] }.compact.map(&:existent).flatten.uniq
231
+
232
+ dirs.each do |dir|
233
+ Dir.glob(File.join(dir, '**/*.rb')).each do |file|
234
+ require_dependency file
235
+ end
236
+ end
237
+ end
197
238
  end
198
239
  end
240
+
241
+ require 'chewy/stash'
@@ -1,11 +1,11 @@
1
1
  module Chewy
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
- source_root File.expand_path("../../templates", __FILE__)
4
+ source_root File.expand_path('../../templates', __FILE__)
5
5
 
6
6
  def copy_configuration
7
- template "chewy.yml", "config/chewy.yml"
7
+ template 'chewy.yml', 'config/chewy.yml'
8
8
  end
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -36,40 +36,25 @@ module Sequel
36
36
  callback_options = ChewyObserve.extract_callback_options!(args)
37
37
  update_proc = ChewyObserve.update_proc(type_name, *args, &block)
38
38
 
39
- if Chewy.use_after_commit_callbacks
40
- set_callback(:commit, callback_options, &update_proc)
41
- set_callback(:destroy_commit, callback_options, &update_proc)
42
- else
43
- set_callback(:save, callback_options, &update_proc)
44
- set_callback(:destroy, callback_options, &update_proc)
45
- end
39
+ set_callback(:save, callback_options, &update_proc)
40
+ set_callback(:destroy, callback_options, &update_proc)
46
41
  end
47
42
  end
48
43
 
49
44
  # Instance level methods for Sequel::Model
50
45
  #
51
46
  module InstanceMethods
52
- def after_commit
53
- run_callbacks(:commit) do
54
- super
55
- end
56
- end
57
-
58
- def after_destroy_commit
59
- run_callbacks(:destroy_commit) do
60
- super
61
- end
62
- end
63
-
64
47
  def after_save
65
48
  run_callbacks(:save) do
66
49
  super
50
+ db.after_commit {} if Chewy.use_after_commit_callbacks
67
51
  end
68
52
  end
69
53
 
70
54
  def after_destroy
71
55
  run_callbacks(:destroy) do
72
56
  super
57
+ db.after_commit {} if Chewy.use_after_commit_callbacks
73
58
  end
74
59
  end
75
60
  end
data/lib/tasks/chewy.rake CHANGED
@@ -1,49 +1,112 @@
1
1
  require 'chewy/rake_helper'
2
2
 
3
+ def parse_classes(args)
4
+ if args.present? && args.first.tr!('-', '')
5
+ {except: args}
6
+ else
7
+ {only: args}
8
+ end
9
+ end
10
+
11
+ def parse_parallel_args(args)
12
+ options = {}
13
+ options[:parallel] = args.first =~ /\A\d+\z/ ? Integer(args.shift) : true
14
+ options.merge!(parse_classes(args))
15
+ end
16
+
17
+ def parse_journal_args(args)
18
+ options = {}
19
+ options[:time] = Time.parse(args.shift) if args.first =~ /\A\d+/
20
+ options.merge!(parse_classes(args))
21
+ end
22
+
3
23
  namespace :chewy do
4
- desc 'Destroy, recreate and import data to specified index'
5
- task :reset, [:index] => :environment do |task, args|
6
- Chewy::RakeHelper.subscribed_task_stats do
24
+ desc 'Destroys, recreates and imports data for the specified indexes or all of them'
25
+ task reset: :environment do |_task, args|
26
+ Chewy::RakeHelper.reset(parse_classes(args.extras))
27
+ end
7
28
 
8
- if args[:index].present?
9
- Chewy::RakeHelper.reset_index(args[:index])
10
- else
11
- Chewy::RakeHelper.reset_all
12
- end
29
+ desc 'Resets data for the specified indexes or all of them only if the index specification is changed'
30
+ task upgrade: :environment do |_task, args|
31
+ Chewy::RakeHelper.upgrade(parse_classes(args.extras))
32
+ end
33
+
34
+ desc 'Updates data for the specified indexes/types or all of them'
35
+ task update: :environment do |_task, args|
36
+ Chewy::RakeHelper.update(parse_classes(args.extras))
37
+ end
38
+
39
+ desc 'Synchronizes data for the specified indexes/types or all of them'
40
+ task sync: :environment do |_task, args|
41
+ Chewy::RakeHelper.sync(parse_classes(args.extras))
42
+ end
43
+
44
+ desc 'Resets all the indexes with the specification changed and synchronizes the rest of them'
45
+ task deploy: :environment do
46
+ processed = Chewy::RakeHelper.upgrade
47
+ Chewy::RakeHelper.sync(except: processed)
48
+ end
49
+
50
+ namespace :parallel do
51
+ desc 'Parallel version of `rake chewy:reset`'
52
+ task reset: :environment do |_task, args|
53
+ Chewy::RakeHelper.reset(parse_parallel_args(args.extras))
54
+ end
55
+
56
+ desc 'Parallel version of `rake chewy:upgrade`'
57
+ task upgrade: :environment do |_task, args|
58
+ Chewy::RakeHelper.upgrade(parse_parallel_args(args.extras))
59
+ end
60
+
61
+ desc 'Parallel version of `rake chewy:update`'
62
+ task update: :environment do |_task, args|
63
+ Chewy::RakeHelper.update(parse_parallel_args(args.extras))
64
+ end
65
+
66
+ desc 'Parallel version of `rake chewy:sync`'
67
+ task sync: :environment do |_task, args|
68
+ Chewy::RakeHelper.sync(parse_parallel_args(args.extras))
69
+ end
70
+
71
+ desc 'Parallel version of `rake chewy:deploy`'
72
+ task deploy: :environment do |_task, args|
73
+ parallel = args.extras.first =~ /\A\d+\z/ ? Integer(args.extras.first) : true
74
+ processed = Chewy::RakeHelper.upgrade(parallel: parallel)
75
+ Chewy::RakeHelper.sync(except: processed, parallel: parallel)
13
76
  end
14
77
  end
15
78
 
16
- namespace :reset do
17
- desc 'Destroy, recreate and import data for all found indexes'
18
- task all: :environment do
19
- ActiveSupport::Deprecation.warn('`rake chewy:reset:all` is deprecated and will be removed soon. Use `rake chewy:reset` instead')
79
+ namespace :journal do
80
+ desc 'Applies changes that were done after the specified time for the specified indexes/types or all of them'
81
+ task apply: :environment do |_task, args|
82
+ Chewy::RakeHelper.journal_apply(parse_journal_args(args.extras))
83
+ end
20
84
 
21
- Chewy::RakeHelper.subscribed_task_stats do
22
- Chewy::RakeHelper.reset_all
23
- end
85
+ desc 'Removes journal records created before the specified timestamp for the specified indexes/types or all of them'
86
+ task clean: :environment do |_task, args|
87
+ Chewy::RakeHelper.journal_clean(parse_journal_args(args.extras))
24
88
  end
25
89
  end
26
90
 
27
- desc 'Updates data specified index'
28
- task :update, [:index] => :environment do |task, args|
91
+ task apply_changes_from: :environment do |_task, args|
92
+ ActiveSupport::Deprecation.warn '`rake chewy:apply_changes_from` is deprecated and will be removed soon, use `rake chewy:journal:apply` instead'
93
+
29
94
  Chewy::RakeHelper.subscribed_task_stats do
95
+ params = args.extras
30
96
 
31
- if args[:index].present?
32
- Chewy::RakeHelper.update_index(args[:index])
97
+ if params.empty?
98
+ puts 'Please specify a timestamp like chewy:apply_changes_from[1469528705]'
33
99
  else
34
- Chewy::RakeHelper.update_all
100
+ timestamp, retries = params
101
+ time = Time.at(timestamp.to_i)
102
+ Chewy::Journal.new.apply(time, retries: (retries.to_i if retries))
35
103
  end
36
104
  end
37
105
  end
38
106
 
39
- namespace :update do
40
- desc 'Updates data for all found indexes'
41
- task all: :environment do
42
- ActiveSupport::Deprecation.warn('`rake chewy:update:all` is deprecated and will be removed soon. Use `rake chewy:update` instead')
107
+ task clean_journal: :environment do |_task, args|
108
+ ActiveSupport::Deprecation.warn '`rake chewy:clean_journal` is deprecated and will be removed soon, use `rake chewy:journal:clean` instead'
43
109
 
44
- Chewy::RakeHelper.subscribed_task_stats do
45
- Chewy::RakeHelper.update_all
46
- end
47
- end
110
+ Chewy::Journal.new.clean(args.extras.first)
48
111
  end
49
112
  end