chewy 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (265) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +24 -2
  4. data/.rubocop_todo.yml +2 -2
  5. data/.travis.yml +38 -21
  6. data/.yardopts +5 -0
  7. data/Appraisals +55 -27
  8. data/CHANGELOG.md +57 -12
  9. data/Gemfile +14 -10
  10. data/LEGACY_DSL.md +497 -0
  11. data/README.md +249 -515
  12. data/chewy.gemspec +5 -4
  13. data/gemfiles/rails.4.0.activerecord.gemfile +14 -0
  14. data/gemfiles/rails.4.1.activerecord.gemfile +14 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +8 -10
  16. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +9 -10
  17. data/gemfiles/rails.5.0.activerecord.gemfile +8 -10
  18. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +15 -0
  19. data/gemfiles/rails.5.1.activerecord.gemfile +15 -0
  20. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +15 -0
  21. data/gemfiles/sequel.4.45.gemfile +11 -0
  22. data/lib/chewy.rb +77 -43
  23. data/lib/chewy/config.rb +44 -7
  24. data/lib/chewy/errors.rb +2 -2
  25. data/lib/chewy/fields/base.rb +39 -32
  26. data/lib/chewy/fields/root.rb +33 -7
  27. data/lib/chewy/index.rb +237 -149
  28. data/lib/chewy/index/actions.rb +85 -28
  29. data/lib/chewy/index/aliases.rb +2 -1
  30. data/lib/chewy/index/settings.rb +9 -5
  31. data/lib/chewy/index/specification.rb +58 -0
  32. data/lib/chewy/journal.rb +40 -92
  33. data/lib/chewy/query.rb +43 -27
  34. data/lib/chewy/query/compose.rb +13 -13
  35. data/lib/chewy/query/criteria.rb +13 -13
  36. data/lib/chewy/query/filters.rb +1 -1
  37. data/lib/chewy/query/loading.rb +1 -1
  38. data/lib/chewy/query/nodes/and.rb +2 -2
  39. data/lib/chewy/query/nodes/bool.rb +1 -1
  40. data/lib/chewy/query/nodes/equal.rb +2 -2
  41. data/lib/chewy/query/nodes/exists.rb +1 -1
  42. data/lib/chewy/query/nodes/has_relation.rb +2 -2
  43. data/lib/chewy/query/nodes/match_all.rb +1 -1
  44. data/lib/chewy/query/nodes/missing.rb +1 -1
  45. data/lib/chewy/query/nodes/not.rb +2 -2
  46. data/lib/chewy/query/nodes/or.rb +2 -2
  47. data/lib/chewy/query/nodes/prefix.rb +1 -1
  48. data/lib/chewy/query/nodes/query.rb +2 -2
  49. data/lib/chewy/query/nodes/range.rb +4 -4
  50. data/lib/chewy/query/nodes/regexp.rb +4 -4
  51. data/lib/chewy/query/nodes/script.rb +3 -3
  52. data/lib/chewy/query/pagination.rb +10 -1
  53. data/lib/chewy/railtie.rb +1 -0
  54. data/lib/chewy/rake_helper.rb +265 -48
  55. data/lib/chewy/rspec/update_index.rb +30 -22
  56. data/lib/chewy/search.rb +78 -21
  57. data/lib/chewy/search/loader.rb +83 -0
  58. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  59. data/lib/chewy/search/pagination/will_paginate.rb +41 -0
  60. data/lib/chewy/search/parameters.rb +150 -0
  61. data/lib/chewy/search/parameters/aggs.rb +16 -0
  62. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  63. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  64. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  65. data/lib/chewy/search/parameters/concerns/query_storage.rb +237 -0
  66. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  67. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  68. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  69. data/lib/chewy/search/parameters/explain.rb +16 -0
  70. data/lib/chewy/search/parameters/filter.rb +47 -0
  71. data/lib/chewy/search/parameters/highlight.rb +16 -0
  72. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  73. data/lib/chewy/search/parameters/limit.rb +17 -0
  74. data/lib/chewy/search/parameters/load.rb +32 -0
  75. data/lib/chewy/search/parameters/min_score.rb +16 -0
  76. data/lib/chewy/search/parameters/none.rb +27 -0
  77. data/lib/chewy/search/parameters/offset.rb +17 -0
  78. data/lib/chewy/search/parameters/order.rb +64 -0
  79. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  80. data/lib/chewy/search/parameters/preference.rb +16 -0
  81. data/lib/chewy/search/parameters/profile.rb +16 -0
  82. data/lib/chewy/search/parameters/query.rb +19 -0
  83. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  84. data/lib/chewy/search/parameters/rescore.rb +29 -0
  85. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  86. data/lib/chewy/search/parameters/search_after.rb +20 -0
  87. data/lib/chewy/search/parameters/search_type.rb +16 -0
  88. data/lib/chewy/search/parameters/source.rb +73 -0
  89. data/lib/chewy/search/parameters/storage.rb +95 -0
  90. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  91. data/lib/chewy/search/parameters/suggest.rb +16 -0
  92. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  93. data/lib/chewy/search/parameters/timeout.rb +16 -0
  94. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  95. data/lib/chewy/search/parameters/types.rb +20 -0
  96. data/lib/chewy/search/parameters/version.rb +16 -0
  97. data/lib/chewy/search/query_proxy.rb +257 -0
  98. data/lib/chewy/search/request.rb +1021 -0
  99. data/lib/chewy/search/response.rb +119 -0
  100. data/lib/chewy/search/scoping.rb +50 -0
  101. data/lib/chewy/search/scrolling.rb +136 -0
  102. data/lib/chewy/stash.rb +70 -0
  103. data/lib/chewy/strategy.rb +10 -3
  104. data/lib/chewy/strategy/active_job.rb +1 -0
  105. data/lib/chewy/strategy/atomic.rb +1 -3
  106. data/lib/chewy/strategy/bypass.rb +1 -1
  107. data/lib/chewy/strategy/resque.rb +1 -0
  108. data/lib/chewy/strategy/shoryuken.rb +40 -0
  109. data/lib/chewy/strategy/sidekiq.rb +13 -3
  110. data/lib/chewy/type.rb +29 -7
  111. data/lib/chewy/type/actions.rb +26 -2
  112. data/lib/chewy/type/adapter/active_record.rb +44 -29
  113. data/lib/chewy/type/adapter/base.rb +27 -7
  114. data/lib/chewy/type/adapter/mongoid.rb +18 -7
  115. data/lib/chewy/type/adapter/object.rb +187 -26
  116. data/lib/chewy/type/adapter/orm.rb +59 -32
  117. data/lib/chewy/type/adapter/sequel.rb +32 -16
  118. data/lib/chewy/type/import.rb +145 -191
  119. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  120. data/lib/chewy/type/import/bulk_request.rb +76 -0
  121. data/lib/chewy/type/import/journal_builder.rb +45 -0
  122. data/lib/chewy/type/import/routine.rb +138 -0
  123. data/lib/chewy/type/mapping.rb +11 -1
  124. data/lib/chewy/type/observe.rb +1 -1
  125. data/lib/chewy/type/syncer.rb +220 -0
  126. data/lib/chewy/type/witchcraft.rb +27 -13
  127. data/lib/chewy/type/wrapper.rb +28 -2
  128. data/lib/chewy/version.rb +1 -1
  129. data/lib/tasks/chewy.rake +84 -26
  130. data/spec/chewy/config_spec.rb +82 -1
  131. data/spec/chewy/fields/base_spec.rb +147 -112
  132. data/spec/chewy/fields/root_spec.rb +75 -18
  133. data/spec/chewy/fields/time_fields_spec.rb +2 -3
  134. data/spec/chewy/index/actions_spec.rb +180 -50
  135. data/spec/chewy/index/aliases_spec.rb +2 -2
  136. data/spec/chewy/index/settings_spec.rb +67 -38
  137. data/spec/chewy/index/specification_spec.rb +160 -0
  138. data/spec/chewy/index_spec.rb +57 -66
  139. data/spec/chewy/journal_spec.rb +149 -54
  140. data/spec/chewy/minitest/helpers_spec.rb +4 -4
  141. data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
  142. data/spec/chewy/query/criteria_spec.rb +179 -179
  143. data/spec/chewy/query/filters_spec.rb +15 -15
  144. data/spec/chewy/query/loading_spec.rb +22 -20
  145. data/spec/chewy/query/nodes/and_spec.rb +2 -2
  146. data/spec/chewy/query/nodes/bool_spec.rb +4 -4
  147. data/spec/chewy/query/nodes/equal_spec.rb +19 -19
  148. data/spec/chewy/query/nodes/exists_spec.rb +6 -6
  149. data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
  150. data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
  151. data/spec/chewy/query/nodes/missing_spec.rb +5 -5
  152. data/spec/chewy/query/nodes/not_spec.rb +3 -2
  153. data/spec/chewy/query/nodes/or_spec.rb +2 -2
  154. data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
  155. data/spec/chewy/query/nodes/query_spec.rb +2 -2
  156. data/spec/chewy/query/nodes/range_spec.rb +18 -18
  157. data/spec/chewy/query/nodes/raw_spec.rb +1 -1
  158. data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
  159. data/spec/chewy/query/nodes/script_spec.rb +4 -4
  160. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  161. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  162. data/spec/chewy/query/pagination_spec.rb +25 -21
  163. data/spec/chewy/query_spec.rb +501 -560
  164. data/spec/chewy/rake_helper_spec.rb +368 -0
  165. data/spec/chewy/repository_spec.rb +4 -4
  166. data/spec/chewy/rspec/update_index_spec.rb +89 -56
  167. data/spec/chewy/runtime_spec.rb +2 -2
  168. data/spec/chewy/search/loader_spec.rb +117 -0
  169. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  170. data/spec/chewy/search/pagination/kaminari_spec.rb +17 -0
  171. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  172. data/spec/chewy/search/pagination/will_paginate_spec.rb +17 -0
  173. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  174. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  175. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  176. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  177. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  178. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  179. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  180. data/spec/chewy/search/parameters/indices_boost_spec.rb +83 -0
  181. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  182. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  183. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  184. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  185. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  186. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  187. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  188. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  189. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  190. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  191. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  192. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  193. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  194. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  195. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  196. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  197. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  198. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  199. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  200. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  201. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  202. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  203. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  204. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  205. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  206. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  207. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  208. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  209. data/spec/chewy/search/parameters_spec.rb +130 -0
  210. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  211. data/spec/chewy/search/request_spec.rb +669 -0
  212. data/spec/chewy/search/response_spec.rb +192 -0
  213. data/spec/chewy/search/scrolling_spec.rb +169 -0
  214. data/spec/chewy/search_spec.rb +13 -6
  215. data/spec/chewy/stash_spec.rb +95 -0
  216. data/spec/chewy/strategy/active_job_spec.rb +6 -0
  217. data/spec/chewy/strategy/resque_spec.rb +6 -0
  218. data/spec/chewy/strategy/shoryuken_spec.rb +64 -0
  219. data/spec/chewy/strategy/sidekiq_spec.rb +8 -0
  220. data/spec/chewy/strategy_spec.rb +6 -6
  221. data/spec/chewy/type/actions_spec.rb +29 -10
  222. data/spec/chewy/type/adapter/active_record_spec.rb +203 -91
  223. data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
  224. data/spec/chewy/type/adapter/object_spec.rb +101 -28
  225. data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
  226. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  227. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  228. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  229. data/spec/chewy/type/import/routine_spec.rb +110 -0
  230. data/spec/chewy/type/import_spec.rb +350 -271
  231. data/spec/chewy/type/mapping_spec.rb +54 -18
  232. data/spec/chewy/type/observe_spec.rb +5 -1
  233. data/spec/chewy/type/syncer_spec.rb +123 -0
  234. data/spec/chewy/type/witchcraft_spec.rb +45 -29
  235. data/spec/chewy/type/wrapper_spec.rb +63 -23
  236. data/spec/chewy/type_spec.rb +28 -7
  237. data/spec/chewy_spec.rb +75 -7
  238. data/spec/spec_helper.rb +5 -2
  239. data/spec/support/active_record.rb +5 -1
  240. data/spec/support/class_helpers.rb +0 -14
  241. data/spec/support/mongoid.rb +15 -3
  242. data/spec/support/sequel.rb +6 -1
  243. metadata +198 -37
  244. data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
  245. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
  246. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
  247. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
  248. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
  249. data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
  250. data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
  251. data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
  252. data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
  253. data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
  254. data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
  255. data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
  256. data/gemfiles/sequel.4.38.gemfile +0 -14
  257. data/lib/chewy/journal/apply.rb +0 -31
  258. data/lib/chewy/journal/clean.rb +0 -24
  259. data/lib/chewy/journal/entry.rb +0 -83
  260. data/lib/chewy/journal/query.rb +0 -87
  261. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  262. data/lib/chewy/query/scoping.rb +0 -20
  263. data/spec/chewy/journal/apply_spec.rb +0 -120
  264. data/spec/chewy/journal/entry_spec.rb +0 -237
  265. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
@@ -14,6 +14,7 @@ module Chewy
14
14
  @queue = :chewy
15
15
 
16
16
  def self.perform(type, ids, options = {})
17
+ options[:refresh] = !Chewy.disable_refresh_async if Chewy.disable_refresh_async
17
18
  type.constantize.import!(ids, options)
18
19
  end
19
20
  end
@@ -0,0 +1,40 @@
1
+ module Chewy
2
+ class Strategy
3
+ # The strategy works the same way as atomic, but performs
4
+ # async index update driven by shoryuken
5
+ #
6
+ # Chewy.strategy(:shoryuken) do
7
+ # User.all.map(&:save) # Does nothing here
8
+ # Post.all.map(&:save) # And here
9
+ # # It imports all the changed users and posts right here
10
+ # end
11
+ #
12
+ class Shoryuken < Atomic
13
+ class Worker
14
+ include ::Shoryuken::Worker
15
+
16
+ shoryuken_options auto_delete: true,
17
+ body_parser: :json
18
+
19
+ def perform(_sqs_msg, body)
20
+ options = body['options'] || {}
21
+ options[:refresh] = !Chewy.disable_refresh_async if Chewy.disable_refresh_async
22
+ body['type'].constantize.import!(body['ids'], options.deep_symbolize_keys!)
23
+ end
24
+ end
25
+
26
+ def leave
27
+ @stash.each do |type, ids|
28
+ next if ids.empty?
29
+ Shoryuken::Worker.perform_async({type: type.name, ids: ids}, queue: shoryuken_queue)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def shoryuken_queue
36
+ Chewy.settings.fetch(:shoryuken, {})[:queue] || 'chewy'
37
+ end
38
+ end
39
+ end
40
+ end
@@ -13,18 +13,28 @@ module Chewy
13
13
  class Worker
14
14
  include ::Sidekiq::Worker
15
15
 
16
- sidekiq_options queue: :chewy
17
-
18
16
  def perform(type, ids, options = {})
17
+ options[:refresh] = !Chewy.disable_refresh_async if Chewy.disable_refresh_async
19
18
  type.constantize.import!(ids, options)
20
19
  end
21
20
  end
22
21
 
23
22
  def leave
24
23
  @stash.each do |type, ids|
25
- Chewy::Strategy::Sidekiq::Worker.perform_async(type.name, ids) unless ids.empty?
24
+ next if ids.empty?
25
+ ::Sidekiq::Client.push(
26
+ 'queue' => sidekiq_queue,
27
+ 'class' => Chewy::Strategy::Sidekiq::Worker,
28
+ 'args' => [type.name, ids]
29
+ )
26
30
  end
27
31
  end
32
+
33
+ private
34
+
35
+ def sidekiq_queue
36
+ Chewy.settings.fetch(:sidekiq, {})[:queue] || 'chewy'
37
+ end
28
38
  end
29
39
  end
30
40
  end
@@ -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, :raw_import, :journal].freeze
17
+ IMPORT_OPTIONS_KEYS = %i[batch_size bulk_size refresh consistency replication raw_import journal].freeze
17
18
 
18
19
  include Search
19
20
  include Mapping
@@ -24,7 +25,7 @@ module Chewy
24
25
  include Witchcraft
25
26
  include Import
26
27
 
27
- singleton_class.delegate :index_name, :_index_name, :derivable_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 = {}
@@ -33,7 +34,7 @@ module Chewy
33
34
  # Chewy index current type belongs to. Defined inside `Chewy.create_type`
34
35
  #
35
36
  def index
36
- raise NotImplementedError
37
+ raise NotImplementedError, 'Looks like this type ws defined outside the index scope and `.index` method is undefined for it'
37
38
  end
38
39
 
39
40
  # Current type adapter. Defined inside `Chewy.create_type`, derived from
@@ -49,6 +50,27 @@ module Chewy
49
50
  adapter.type_name
50
51
  end
51
52
 
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
66
+
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
73
+
52
74
  # Returns list of public class methods defined in current type
53
75
  #
54
76
  def scopes
@@ -4,15 +4,39 @@ module Chewy
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  module ClassMethods
7
- # Delete all documents of a type and reimport them
8
- # Returns true or false depending on success.
7
+ # Deletes all documents of a type and reimports them
9
8
  #
9
+ # @example
10
10
  # UsersIndex::User.reset
11
11
  #
12
+ # @see Chewy::Type::Import::ClassMethods#import
13
+ # @see Chewy::Type::Import::ClassMethods#import
14
+ # @return [true, false] the result of import
12
15
  def reset
13
16
  delete_all
14
17
  import
15
18
  end
19
+
20
+ # Performs missing and outdated objects synchronization for the current type.
21
+ #
22
+ # @example
23
+ # UsersIndex::User.sync
24
+ #
25
+ # @see Chewy::Type::Syncer
26
+ # @param parallel [true, Integer, Hash] options for parallel execution or the number of processes
27
+ # @return [Hash{Symbol, Object}, nil] a number of missing and outdated documents reindexed and their ids, nil in case of errors
28
+ def sync(parallel: nil)
29
+ syncer = Syncer.new(self, parallel: parallel)
30
+ count = syncer.perform
31
+ {count: count, missing: syncer.missing_ids, outdated: syncer.outdated_ids} if count
32
+ end
33
+
34
+ # A {Chewy::Journal} instance for the particular type
35
+ #
36
+ # @return [Chewy::Journal] journal instance
37
+ def journal
38
+ @journal ||= Chewy::Journal.new(self)
39
+ end
16
40
  end
17
41
  end
18
42
  end
@@ -22,32 +22,58 @@ module Chewy
22
22
  end
23
23
 
24
24
  def import_scope(scope, options)
25
- scope = scope.reorder(target_id.asc).limit(options[:batch_size])
26
-
27
- ids = pluck_ids(scope)
28
- result = true
25
+ pluck_in_batches(scope, options.slice(:batch_size)).inject(true) do |result, ids|
26
+ objects = if options[:raw_import]
27
+ raw_default_scope_where_ids_in(ids, options[:raw_import])
28
+ else
29
+ default_scope_where_ids_in(ids)
30
+ end
29
31
 
30
- while ids.present?
31
- objects =
32
- if options[:raw_import]
33
- raw_default_scope_where_ids_in(ids, options[:raw_import])
34
- else
35
- default_scope_where_ids_in(ids)
36
- end
37
- result &= yield grouped_objects(objects)
38
- break if ids.size < options[:batch_size]
39
- ids = pluck_ids(scope.where(target_id.gt(ids.last)))
32
+ result & yield(grouped_objects(objects))
40
33
  end
34
+ end
41
35
 
42
- result
36
+ def primary_key
37
+ @primary_key ||= target.primary_key.to_sym
43
38
  end
44
39
 
45
40
  def target_id
46
- target.arel_table[target.primary_key]
41
+ target.arel_table[primary_key.to_s]
42
+ end
43
+
44
+ def pluck(scope, fields: [], typecast: true)
45
+ if typecast
46
+ scope.except(:includes).distinct.pluck(primary_key, *fields)
47
+ else
48
+ scope = scope.except(:includes).distinct
49
+ scope.select_values = [primary_key, *fields].map do |column|
50
+ target.columns_hash.key?(column) ? target.arel_table[column] : column
51
+ end
52
+ sql = scope.to_sql
53
+
54
+ if fields.present?
55
+ target.connection.select_rows(sql)
56
+ else
57
+ target.connection.select_values(sql)
58
+ end
59
+ end
47
60
  end
48
61
 
49
- def pluck_ids(scope)
50
- scope.except(:includes).uniq.pluck(target.primary_key.to_sym)
62
+ def pluck_in_batches(scope, fields: [], batch_size: nil, typecast: true)
63
+ return enum_for(:pluck_in_batches, scope, fields: fields, batch_size: batch_size, typecast: typecast) unless block_given?
64
+
65
+ scope = scope.reorder(target_id.asc).limit(batch_size)
66
+ ids = pluck(scope, fields: fields, typecast: typecast)
67
+ count = 0
68
+
69
+ while ids.present?
70
+ yield ids
71
+ break if ids.size < batch_size
72
+ last_id = ids.last.is_a?(Array) ? ids.last.first : ids.last
73
+ ids = pluck(scope.where(target_id.gt(last_id)), fields: fields, typecast: typecast)
74
+ end
75
+
76
+ count
51
77
  end
52
78
 
53
79
  def scope_where_ids_in(scope, ids)
@@ -67,17 +93,6 @@ module Chewy
67
93
  ::ActiveRecord::Base
68
94
  end
69
95
  end
70
-
71
- ActiveSupport.on_load(:active_record) do
72
- if ::ActiveRecord::VERSION::MAJOR >= 5
73
- module Rails5
74
- def pluck_ids(scope)
75
- scope.except(:includes).distinct.pluck(target.primary_key.to_sym)
76
- end
77
- end
78
- Chewy::Type::Adapter::ActiveRecord.prepend(Rails5)
79
- end
80
- end
81
96
  end
82
97
  end
83
98
  end
@@ -37,23 +37,43 @@ module Chewy
37
37
  end
38
38
 
39
39
  # Splits passed objects to groups according to `:batch_size` options.
40
- # For every group crates hash with action keys. Example:
40
+ # For every group creates hash with action keys. Example:
41
41
  #
42
- # { delete: [object1, object2], index: [object3, object4, object5] }
42
+ # { delete: [object_or_id1, object_or_id2], index: [object3, object4, object5] }
43
43
  #
44
- # Returns true id all the block call returns true and false otherwise
44
+ # @yield batch [Array<Object>] each batch of objects
45
+ # @return [true, false] returns true if all the block call returns true and false otherwise
46
+ def import(*_args, &_block)
47
+ raise NotImplementedError
48
+ end
49
+
50
+ # Unlike {#import} fetches only ids (references) to the imported objects,
51
+ # using the same procedures as {#import}.
52
+ #
53
+ # @param fields [Array<Symbol>] additional fields to fetch
54
+ # @param batch_size [Integer] batch size, defaults to 1000
55
+ # @yield batch [Array<Object>] each batch of objects
56
+ def import_fields(*_args, &_block)
57
+ raise NotImplementedError
58
+ end
59
+
60
+ # Uses the same strategy as import for the passed arguments, and returns
61
+ # an array of references to the passed objects. Returns ids if possible.
62
+ # Otherwise - and array of objects themselves.
45
63
  #
46
- def import(*_args)
64
+ # @param batch_size [Integer] batch size, defaults to 1000
65
+ # @yield batch [Array<Object>] each batch of objects
66
+ def import_references(*_args, &_block)
47
67
  raise NotImplementedError
48
68
  end
49
69
 
50
- # Returns array of loaded objects for passed objects array. If some object
70
+ # Returns array of loaded objects for passed ids array. If some object
51
71
  # was not loaded, it returns `nil` in the place of this object
52
72
  #
53
- # load(double(id: 1), double(id: 2), double(id: 3)) #=>
73
+ # load([1, 2, 3]) #=>
54
74
  # # [<Product id: 1>, nil, <Product id: 3>], assuming, #2 was not found
55
75
  #
56
- def load(*_args)
76
+ def load(_ids, **_options)
57
77
  raise NotImplementedError
58
78
  end
59
79
 
@@ -27,18 +27,29 @@ module Chewy
27
27
  end
28
28
 
29
29
  def import_scope(scope, options)
30
- scope.batch_size(options[:batch_size]).no_timeout.pluck(:_id)
31
- .each_slice(options[:batch_size]).map do |ids|
32
- yield grouped_objects(default_scope_where_ids_in(ids))
33
- end.all?
30
+ pluck_in_batches(scope, options.slice(:batch_size)).map do |ids|
31
+ yield grouped_objects(default_scope_where_ids_in(ids))
32
+ end.all?
34
33
  end
35
34
 
36
- def pluck_ids(scope)
37
- scope.pluck(:_id)
35
+ def primary_key
36
+ :_id
37
+ end
38
+
39
+ def pluck(scope, fields: [])
40
+ scope.pluck(primary_key, *fields)
41
+ end
42
+
43
+ def pluck_in_batches(scope, fields: [], batch_size: nil, **options)
44
+ return enum_for(:pluck_in_batches, scope, fields: fields, batch_size: batch_size, **options) unless block_given?
45
+
46
+ scope.batch_size(batch_size).no_timeout.pluck(primary_key, *fields).each_slice(batch_size) do |batch|
47
+ yield batch
48
+ end
38
49
  end
39
50
 
40
51
  def scope_where_ids_in(scope, ids)
41
- scope.where(:_id.in => ids)
52
+ scope.where(primary_key.in => ids)
42
53
  end
43
54
 
44
55
  def all_scope
@@ -3,60 +3,196 @@ require 'chewy/type/adapter/base'
3
3
  module Chewy
4
4
  class Type
5
5
  module Adapter
6
+ # This adapter provides an ability to import documents from any
7
+ # source. You can actually use any class or even a symbol as
8
+ # a target.
9
+ #
10
+ # In case if a class is used - some of the additional features
11
+ # are available: it is possible to provide the default import
12
+ # data (used on reset) and source objects loading logic.
13
+ #
14
+ # @see #import
15
+ # @see #load
6
16
  class Object < Base
7
- def initialize(*args)
8
- @options = args.extract_options!
9
- @target = args.first
17
+ # The signature of the type definition.
18
+ #
19
+ # @example
20
+ # define_type :geoname
21
+ # define_type Geoname
22
+ # define_type -> { Geoname.all_the_places }, name: 'geoname'
23
+ #
24
+ # @param target [Class, Symbol, String, Proc] a source of data and everything
25
+ # @option options [String, Symbol] :name redefines the inferred type name if necessary
26
+ # @option options [String, Symbol] :import_all_method redefines import method name
27
+ # @option options [String, Symbol] :load_all_method redefines batch load method name
28
+ # @option options [String, Symbol] :load_one_method redefines per-object load method name
29
+ def initialize(target, **options)
30
+ @target = target
31
+ @options = options
10
32
  end
11
33
 
34
+ # Name is used for the type class creation. Inferred from the target
35
+ # by default if possible.
36
+ #
37
+ # @example
38
+ # # defines MyIndex::Geoname
39
+ # define_type :geoname
40
+ # # still defines MyIndex::Geoname
41
+ # define_type -> { Geoname.all_the_places }, name: 'geoname'
42
+ #
43
+ # @return [String]
12
44
  def name
13
45
  @name ||= (options[:name] || @target).to_s.camelize.demodulize
14
46
  end
15
47
 
48
+ # While for ORM adapters it returns an array of ids for the passed
49
+ # collection, for the object adapter it returns the collection itself.
50
+ #
51
+ # @param collection [Array<Object>, Object] a collection or an object
52
+ # @return [Array<Object>]
16
53
  def identify(collection)
17
54
  Array.wrap(collection)
18
55
  end
19
56
 
20
- # Imports passed data with options
57
+ # This method is used internally by `Chewy::Type.import`.
21
58
  #
22
- # Import data types:
59
+ # The idea is that any object can be imported to ES if
60
+ # it responds to `#to_json` method.
23
61
  #
24
- # * Array ob objects
62
+ # If method `destroyed?` is defined for object (or, in case of hash object,
63
+ # it has `:_destroyed` or `'_destroyed'` key) and returns `true` or object
64
+ # satisfy `delete_if` type option then object will be deleted from index.
65
+ # But in order to be destroyable, objects need to respond to `id` method
66
+ # or have an `id` key so ElasticSearch could know which one to delete.
25
67
  #
26
- # Import options:
68
+ # If nothing is passed the method tries to call `import_all_method`,
69
+ # which is `call` by default, on target to get the default objects batch.
27
70
  #
28
- # <tt>:batch_size</tt> - import batch size, 1000 objects by default
71
+ # @example
72
+ # class Geoname
73
+ # self < class
74
+ # def self.call
75
+ # FancyGeoAPI.all_points_collection
76
+ # end
77
+ # alias_method :import_all, :call
78
+ # end
79
+ # end
29
80
  #
30
- # If method `destroyed?` is defined for object and returns true or object
31
- # satisfy `delete_if` type option then object will be deleted from index.
32
- # But to be destroyed objects need to respond to `id` method as well, so
33
- # ElasticSearch could know which one to delete.
81
+ # # All the folloving variants will work:
82
+ # define_type Geoname
83
+ # define_type Geoname, import_all_method: 'import_all'
84
+ # define_type -> { FancyGeoAPI.all_points_collection }, name: 'geoname'
34
85
  #
86
+ # @param args [Array<#to_json>]
87
+ # @option options [Integer] :batch_size import processing batch size
88
+ # @return [true, false]
35
89
  def import(*args, &block)
90
+ collection, options = import_args(*args)
91
+ import_objects(collection, options, &block)
92
+ end
93
+
94
+ # For the object adapter this method tries to fetch :id and requested
95
+ # fields from the passed collection or the target's `import_all_method`
96
+ # when defined. Otherwise it tries to call the target `pluck_method`,
97
+ # which is configurable and `pluck` by default. The `pluck_method` have
98
+ # to act exactly the same way as the AR one. It returns an empty array
99
+ # when none of the methods are found.
100
+ #
101
+ # @example
102
+ # class Geoname
103
+ # self < class
104
+ # def self.pluck(*fields)
105
+ # if fields.one?
106
+ # whatever_source.map { |object| object.send(fields.first) }
107
+ # else
108
+ # whatever_source.map do |object|
109
+ # fields.map { |field| object.send(field) }
110
+ # end
111
+ # end
112
+ # end
113
+ # end
114
+ # end
115
+ #
116
+ # @see Chewy::Type::Adapter::Base#import_fields
117
+ def import_fields(*args)
118
+ return enum_for(:import_fields, *args) unless block_given?
36
119
  options = args.extract_options!
37
120
  options[:batch_size] ||= BATCH_SIZE
38
121
 
39
- objects = if args.empty? && @target.respond_to?(import_all_method)
40
- @target.send(import_all_method)
122
+ if args.empty? && @target.respond_to?(pluck_method)
123
+ @target.send(pluck_method, :id, *options[:fields]).each_slice(options[:batch_size]) do |batch|
124
+ yield batch
125
+ end
126
+ elsif options[:fields].blank?
127
+ import_references(*args, options) do |batch|
128
+ yield batch.map { |object| object_field(object, :id) || object }
129
+ end
41
130
  else
42
- args.flatten.compact
131
+ import_references(*args, options) do |batch|
132
+ batch = batch.map do |object|
133
+ options[:fields].map { |field| object_field(object, field) }
134
+ .unshift(object_field(object, :id) || object)
135
+ end
136
+ yield batch
137
+ end
43
138
  end
139
+ end
140
+
141
+ # For the Object adapter returns the objects themselves in batches.
142
+ #
143
+ # @see Chewy::Type::Adapter::Base#import_references
144
+ def import_references(*args)
145
+ return enum_for(:import_references, *args) unless block_given?
44
146
 
45
- import_objects(objects, options, &block)
147
+ collection, options = import_args(*args)
148
+ collection.each_slice(options[:batch_size]) do |batch|
149
+ yield batch
150
+ end
46
151
  end
47
152
 
48
- def load(*args)
49
- args.extract_options!
50
- objects = args.flatten
153
+ # This method is used internally by the request DSL when the
154
+ # collection of ORM/ODM objects is requested.
155
+ #
156
+ # Options usage is implemented by `load_all_method` and `load_one_method`.
157
+ #
158
+ # If none of the `load_all_method` or `load_one_method` is implemented
159
+ # for the target - the method will return nil. This means that the
160
+ # loader will return an array `Chewy::Type` objects that actually was passed.
161
+ #
162
+ # To use loading for objects it is obviously required to provide
163
+ # some meaningful ids for ES documents.
164
+ #
165
+ # @example
166
+ # class Geoname
167
+ # def self.load_all(wrappers, options)
168
+ # if options[:additional_data]
169
+ # wrappers.map do |wrapper|
170
+ # FancyGeoAPI.point_by_name(wrapper.name)
171
+ # end
172
+ # else
173
+ # wrappers
174
+ # end
175
+ # end
176
+ # end
177
+ #
178
+ # MyIndex::Geoname.load(additional_data: true).objects
179
+ #
180
+ # @param ids [Array<Hash>] an array of ids from ES hits
181
+ # @param options [Hash] any options passed here with the request DSL `load` method.
182
+ # @return [Array<Object>, nil]
183
+ def load(ids, **options)
51
184
  if target.respond_to?(load_all_method)
52
- target.send(load_all_method, objects)
185
+ if target.method(load_all_method).arity == 1
186
+ target.send(load_all_method, ids)
187
+ else
188
+ target.send(load_all_method, ids, options)
189
+ end
53
190
  elsif target.respond_to?(load_one_method)
54
- objects.map { |object| target.send(load_one_method, object) }
55
- elsif target.respond_to?(:wrap)
56
- ActiveSupport::Deprecation.warn('Loading with `wrap` method is deprecated. Rename it to `load_one` or pass `load_one_method: :my_load_method` option to `define_type`')
57
- objects.map { |object| target.wrap(object) }
58
- else
59
- objects
191
+ if target.method(load_one_method).arity == 1
192
+ ids.map { |hit| target.send(load_one_method, hit) }
193
+ else
194
+ ids.map { |hit| target.send(load_one_method, hit, options) }
195
+ end
60
196
  end
61
197
  end
62
198
 
@@ -75,10 +211,22 @@ module Chewy
75
211
  !!delete
76
212
  end
77
213
 
214
+ def object_field(object, name)
215
+ if object.respond_to?(name)
216
+ object.send(name)
217
+ elsif object.is_a?(Hash)
218
+ object[name.to_sym] || object[name.to_s]
219
+ end
220
+ end
221
+
78
222
  def import_all_method
79
223
  @import_all_method ||= options[:import_all_method] || :call
80
224
  end
81
225
 
226
+ def pluck_method
227
+ @pluck_method ||= options[:pluck_method] || :pluck
228
+ end
229
+
82
230
  def load_all_method
83
231
  @load_all_method ||= options[:load_all_method] || :load_all
84
232
  end
@@ -86,6 +234,19 @@ module Chewy
86
234
  def load_one_method
87
235
  @load_one_method ||= options[:load_one_method] || :load_one
88
236
  end
237
+
238
+ def import_args(*args)
239
+ options = args.extract_options!
240
+ options[:batch_size] ||= BATCH_SIZE
241
+
242
+ collection = if args.empty? && @target.respond_to?(import_all_method)
243
+ @target.send(import_all_method)
244
+ else
245
+ args.flatten(1).compact
246
+ end
247
+
248
+ [collection, options]
249
+ end
89
250
  end
90
251
  end
91
252
  end