chewy 0.10.1 → 7.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) 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/.rubocop.yml +28 -23
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +480 -298
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -5
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +571 -333
  16. data/chewy.gemspec +12 -15
  17. data/gemfiles/rails.5.2.activerecord.gemfile +11 -0
  18. data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
  19. data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
  20. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  21. data/lib/chewy/config.rb +48 -77
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +88 -16
  24. data/lib/chewy/fields/root.rb +15 -21
  25. data/lib/chewy/index/actions.rb +67 -38
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +11 -12
  28. data/lib/chewy/{type → index}/adapter/object.rb +28 -32
  29. data/lib/chewy/{type → index}/adapter/orm.rb +26 -24
  30. data/lib/chewy/index/aliases.rb +14 -5
  31. data/lib/chewy/index/crutch.rb +40 -0
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +10 -9
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +19 -18
  36. data/lib/chewy/{type → index}/import.rb +82 -36
  37. data/lib/chewy/{type → index}/mapping.rb +63 -62
  38. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  39. data/lib/chewy/index/observe/callback.rb +34 -0
  40. data/lib/chewy/index/observe.rb +17 -0
  41. data/lib/chewy/index/settings.rb +2 -0
  42. data/lib/chewy/index/specification.rb +13 -10
  43. data/lib/chewy/{type → index}/syncer.rb +62 -63
  44. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  45. data/lib/chewy/{type → index}/wrapper.rb +16 -6
  46. data/lib/chewy/index.rb +68 -93
  47. data/lib/chewy/journal.rb +25 -14
  48. data/lib/chewy/minitest/helpers.rb +91 -18
  49. data/lib/chewy/minitest/search_index_receiver.rb +29 -33
  50. data/lib/chewy/multi_search.rb +62 -0
  51. data/lib/chewy/railtie.rb +8 -24
  52. data/lib/chewy/rake_helper.rb +141 -112
  53. data/lib/chewy/rspec/build_query.rb +12 -0
  54. data/lib/chewy/rspec/helpers.rb +55 -0
  55. data/lib/chewy/rspec/update_index.rb +58 -49
  56. data/lib/chewy/rspec.rb +2 -0
  57. data/lib/chewy/runtime.rb +1 -1
  58. data/lib/chewy/search/loader.rb +19 -41
  59. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  60. data/lib/chewy/search/parameters/collapse.rb +16 -0
  61. data/lib/chewy/search/parameters/concerns/query_storage.rb +6 -5
  62. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  63. data/lib/chewy/search/parameters/indices.rb +78 -0
  64. data/lib/chewy/search/parameters/none.rb +1 -3
  65. data/lib/chewy/search/parameters/order.rb +6 -19
  66. data/lib/chewy/search/parameters/source.rb +5 -1
  67. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  68. data/lib/chewy/search/parameters.rb +28 -8
  69. data/lib/chewy/search/query_proxy.rb +9 -2
  70. data/lib/chewy/search/request.rb +207 -157
  71. data/lib/chewy/search/response.rb +5 -5
  72. data/lib/chewy/search/scoping.rb +7 -8
  73. data/lib/chewy/search/scrolling.rb +14 -13
  74. data/lib/chewy/search.rb +7 -26
  75. data/lib/chewy/stash.rb +27 -29
  76. data/lib/chewy/strategy/active_job.rb +2 -2
  77. data/lib/chewy/strategy/atomic.rb +1 -1
  78. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  79. data/lib/chewy/strategy/base.rb +10 -0
  80. data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +148 -0
  81. data/lib/chewy/strategy/delayed_sidekiq/worker.rb +52 -0
  82. data/lib/chewy/strategy/delayed_sidekiq.rb +17 -0
  83. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  84. data/lib/chewy/strategy/sidekiq.rb +3 -2
  85. data/lib/chewy/strategy.rb +6 -19
  86. data/lib/chewy/version.rb +1 -1
  87. data/lib/chewy.rb +37 -80
  88. data/lib/generators/chewy/install_generator.rb +1 -1
  89. data/lib/tasks/chewy.rake +26 -32
  90. data/migration_guide.md +56 -0
  91. data/spec/chewy/config_spec.rb +27 -57
  92. data/spec/chewy/fields/base_spec.rb +457 -174
  93. data/spec/chewy/fields/root_spec.rb +24 -32
  94. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  95. data/spec/chewy/index/actions_spec.rb +425 -60
  96. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  97. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  98. data/spec/chewy/index/aliases_spec.rb +3 -3
  99. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  100. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  101. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +22 -30
  102. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  103. data/spec/chewy/{type → index}/import_spec.rb +154 -95
  104. data/spec/chewy/index/mapping_spec.rb +135 -0
  105. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  106. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  107. data/spec/chewy/index/observe_spec.rb +143 -0
  108. data/spec/chewy/index/settings_spec.rb +3 -1
  109. data/spec/chewy/index/specification_spec.rb +32 -33
  110. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  111. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  112. data/spec/chewy/index/wrapper_spec.rb +100 -0
  113. data/spec/chewy/index_spec.rb +99 -114
  114. data/spec/chewy/journal_spec.rb +56 -101
  115. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  116. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  117. data/spec/chewy/multi_search_spec.rb +84 -0
  118. data/spec/chewy/rake_helper_spec.rb +325 -101
  119. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  120. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  121. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  122. data/spec/chewy/runtime_spec.rb +2 -2
  123. data/spec/chewy/search/loader_spec.rb +19 -53
  124. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  125. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  126. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  127. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  128. data/spec/chewy/search/parameters/indices_spec.rb +99 -0
  129. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  130. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  131. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  132. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  133. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  134. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  135. data/spec/chewy/search/parameters_spec.rb +39 -8
  136. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  137. data/spec/chewy/search/request_spec.rb +360 -149
  138. data/spec/chewy/search/response_spec.rb +35 -25
  139. data/spec/chewy/search/scrolling_spec.rb +28 -26
  140. data/spec/chewy/search_spec.rb +73 -53
  141. data/spec/chewy/stash_spec.rb +16 -26
  142. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  143. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  144. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  145. data/spec/chewy/strategy/delayed_sidekiq_spec.rb +190 -0
  146. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  147. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  148. data/spec/chewy/strategy_spec.rb +19 -15
  149. data/spec/chewy_spec.rb +17 -110
  150. data/spec/spec_helper.rb +7 -22
  151. data/spec/support/active_record.rb +43 -5
  152. metadata +123 -198
  153. data/.travis.yml +0 -53
  154. data/Appraisals +0 -79
  155. data/LEGACY_DSL.md +0 -497
  156. data/gemfiles/rails.4.0.activerecord.gemfile +0 -14
  157. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  158. data/gemfiles/rails.4.2.activerecord.gemfile +0 -15
  159. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -15
  160. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  161. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  162. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  163. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  164. data/gemfiles/sequel.4.45.gemfile +0 -11
  165. data/lib/chewy/backports/deep_dup.rb +0 -46
  166. data/lib/chewy/backports/duplicable.rb +0 -91
  167. data/lib/chewy/query/compose.rb +0 -68
  168. data/lib/chewy/query/criteria.rb +0 -191
  169. data/lib/chewy/query/filters.rb +0 -227
  170. data/lib/chewy/query/loading.rb +0 -111
  171. data/lib/chewy/query/nodes/and.rb +0 -25
  172. data/lib/chewy/query/nodes/base.rb +0 -17
  173. data/lib/chewy/query/nodes/bool.rb +0 -34
  174. data/lib/chewy/query/nodes/equal.rb +0 -34
  175. data/lib/chewy/query/nodes/exists.rb +0 -20
  176. data/lib/chewy/query/nodes/expr.rb +0 -28
  177. data/lib/chewy/query/nodes/field.rb +0 -110
  178. data/lib/chewy/query/nodes/has_child.rb +0 -15
  179. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  180. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  181. data/lib/chewy/query/nodes/match_all.rb +0 -11
  182. data/lib/chewy/query/nodes/missing.rb +0 -20
  183. data/lib/chewy/query/nodes/not.rb +0 -25
  184. data/lib/chewy/query/nodes/or.rb +0 -25
  185. data/lib/chewy/query/nodes/prefix.rb +0 -19
  186. data/lib/chewy/query/nodes/query.rb +0 -20
  187. data/lib/chewy/query/nodes/range.rb +0 -63
  188. data/lib/chewy/query/nodes/raw.rb +0 -15
  189. data/lib/chewy/query/nodes/regexp.rb +0 -35
  190. data/lib/chewy/query/nodes/script.rb +0 -20
  191. data/lib/chewy/query/pagination.rb +0 -25
  192. data/lib/chewy/query.rb +0 -1098
  193. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  194. data/lib/chewy/search/parameters/types.rb +0 -20
  195. data/lib/chewy/strategy/resque.rb +0 -27
  196. data/lib/chewy/strategy/shoryuken.rb +0 -40
  197. data/lib/chewy/type/actions.rb +0 -43
  198. data/lib/chewy/type/adapter/mongoid.rb +0 -69
  199. data/lib/chewy/type/adapter/sequel.rb +0 -95
  200. data/lib/chewy/type/crutch.rb +0 -32
  201. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  202. data/lib/chewy/type/observe.rb +0 -78
  203. data/lib/chewy/type.rb +0 -117
  204. data/lib/sequel/plugins/chewy_observe.rb +0 -78
  205. data/spec/chewy/query/criteria_spec.rb +0 -700
  206. data/spec/chewy/query/filters_spec.rb +0 -201
  207. data/spec/chewy/query/loading_spec.rb +0 -124
  208. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  209. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  210. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  211. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  212. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  213. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  214. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  215. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  216. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  217. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  218. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  219. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  220. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  221. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  222. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  223. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  224. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  225. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  226. data/spec/chewy/query/pagination_spec.rb +0 -39
  227. data/spec/chewy/query_spec.rb +0 -636
  228. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  229. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  230. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
  231. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  232. data/spec/chewy/strategy/resque_spec.rb +0 -46
  233. data/spec/chewy/strategy/shoryuken_spec.rb +0 -64
  234. data/spec/chewy/type/actions_spec.rb +0 -50
  235. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  236. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  237. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  238. data/spec/chewy/type/mapping_spec.rb +0 -142
  239. data/spec/chewy/type/observe_spec.rb +0 -137
  240. data/spec/chewy/type/wrapper_spec.rb +0 -98
  241. data/spec/chewy/type_spec.rb +0 -55
  242. data/spec/support/mongoid.rb +0 -93
  243. data/spec/support/sequel.rb +0 -80
@@ -1,43 +1,42 @@
1
- require 'chewy/type/import/journal_builder'
2
- require 'chewy/type/import/bulk_builder'
3
- require 'chewy/type/import/bulk_request'
4
- require 'chewy/type/import/routine'
1
+ require 'chewy/index/import/journal_builder'
2
+ require 'chewy/index/import/bulk_builder'
3
+ require 'chewy/index/import/bulk_request'
4
+ require 'chewy/index/import/routine'
5
5
 
6
6
  module Chewy
7
- class Type
7
+ class Index
8
8
  module Import
9
9
  extend ActiveSupport::Concern
10
10
 
11
- IMPORT_WORKER = lambda do |type, options, total, ids, index|
12
- ::Process.setproctitle("chewy [#{type}]: import data (#{index + 1}/#{total})")
13
- routine = Routine.new(type, options)
14
- type.adapter.import(*ids, routine.options) do |action_objects|
11
+ IMPORT_WORKER = lambda do |index, options, total, ids, iteration|
12
+ ::Process.setproctitle("chewy [#{index}]: import data (#{iteration + 1}/#{total})")
13
+ routine = Routine.new(index, **options)
14
+ index.adapter.import(*ids, routine.options) do |action_objects|
15
15
  routine.process(**action_objects)
16
16
  end
17
17
  {errors: routine.errors, import: routine.stats, leftovers: routine.leftovers}
18
18
  end
19
19
 
20
- LEFTOVERS_WORKER = lambda do |type, options, total, body, index|
21
- ::Process.setproctitle("chewy [#{type}]: import leftovers (#{index + 1}/#{total})")
22
- routine = Routine.new(type, options)
20
+ LEFTOVERS_WORKER = lambda do |index, options, total, body, iteration|
21
+ ::Process.setproctitle("chewy [#{index}]: import leftovers (#{iteration + 1}/#{total})")
22
+ routine = Routine.new(index, **options)
23
23
  routine.perform_bulk(body)
24
24
  routine.errors
25
25
  end
26
26
 
27
27
  module ClassMethods
28
28
  # @!method import(*collection, **options)
29
- # Basically, one of the main methods for type. Performs any objects import
30
- # to the index for a specified type. Does all the objects handling routines.
29
+ # Basically, one of the main methods for an index. Performs any objects import
30
+ # to the index. Does all the objects handling routines.
31
31
  # Performs document import by utilizing bulk API. Bulk size and objects batch
32
32
  # size are controlled by the corresponding options.
33
33
  #
34
34
  # It accepts ORM/ODM objects, PORO, hashes, ids which are used by adapter to
35
- # fetch objects from the source depenting on the used adapter. It destroys
36
- # passed objects from the index if they are not in the default type scope
35
+ # fetch objects from the source depending on the used adapter. It destroys
36
+ # passed objects from the index if they are not in the default scope
37
37
  # or marked for destruction.
38
38
  #
39
- # It handles parent-child relationships: if the object parent_id has been
40
- # changed it destroys the object and recreates it from scratch.
39
+ # It handles parent-child relationships with a join field reindexing children when the parent is reindexed.
41
40
  #
42
41
  # Performs journaling if enabled: it stores all the ids of the imported
43
42
  # objects to a specialized index. It is possible to replay particular import
@@ -51,15 +50,15 @@ module Chewy
51
50
  #
52
51
  # Utilizes `ActiveSupport::Notifications`, so it is possible to get imported
53
52
  # objects later by listening to the `import_objects.chewy` queue. It is also
54
- # possible to get the list of occured errors from the payload if something
53
+ # possible to get the list of occurred errors from the payload if something
55
54
  # went wrong.
56
55
  #
57
56
  # Import can also be run in parallel using the Parallel gem functionality.
58
57
  #
59
58
  # @example
60
- # UsersIndex::User.import(parallel: true) # imports everything in parallel with automatic workers number
61
- # UsersIndex::User.import(parallel: 3) # using 3 workers
62
- # UsersIndex::User.import(parallel: {in_threads: 10}) # in 10 threads
59
+ # UsersIndex.import(parallel: true) # imports everything in parallel with automatic workers number
60
+ # UsersIndex.import(parallel: 3) # using 3 workers
61
+ # UsersIndex.import(parallel: {in_threads: 10}) # in 10 threads
63
62
  #
64
63
  # @see https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
65
64
  # @param collection [Array<Object>] and array or anything to import
@@ -67,13 +66,14 @@ module Chewy
67
66
  # @option options [String] suffix an index name suffix, used for zero-downtime reset mostly, no suffix by default
68
67
  # @option options [Integer] bulk_size bulk API chunk size in bytes; if passed, the request is performed several times for each chunk, empty by default
69
68
  # @option options [Integer] batch_size passed to the adapter import method, used to split imported objects in chunks, 1000 by default
69
+ # @option options [Boolean] direct_import skips object reloading in ORM adapter, `false` by default
70
70
  # @option options [true, false] journal enables imported objects journaling, false by default
71
71
  # @option options [Array<Symbol, String>] update_fields list of fields for the partial import, empty by default
72
72
  # @option options [true, false] update_failover enables full objects reimport in cases of partial update errors, `true` by default
73
73
  # @option options [true, Integer, Hash] parallel enables parallel import processing with the Parallel gem, accepts the number of workers or any Parallel gem acceptable options
74
74
  # @return [true, false] false in case of errors
75
- def import(*args)
76
- import_routine(*args).blank?
75
+ ruby2_keywords def import(*args)
76
+ intercept_import_using_strategy(*args).blank?
77
77
  end
78
78
 
79
79
  # @!method import!(*collection, **options)
@@ -83,9 +83,11 @@ module Chewy
83
83
  # in case of any import errors.
84
84
  #
85
85
  # @raise [Chewy::ImportFailed] in case of errors
86
- def import!(*args)
87
- errors = import_routine(*args)
86
+ ruby2_keywords def import!(*args)
87
+ errors = intercept_import_using_strategy(*args)
88
+
88
89
  raise Chewy::ImportFailed.new(self, errors) if errors.present?
90
+
89
91
  true
90
92
  end
91
93
 
@@ -93,7 +95,7 @@ module Chewy
93
95
  # `bulk_size` and `suffix`.
94
96
  #
95
97
  # @see https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
96
- # @see Chewy::Type::Import::Bulk
98
+ # @see Chewy::Index::Import::BulkRequest
97
99
  # @param options [Hash{Symbol => Object}] besides specific import options, it accepts all the options suitable for the bulk API call like `refresh` or `timeout`
98
100
  # @option options [String] suffix bulk API chunk size in bytes; if passed, the request is performed several times for each chunk, empty by default
99
101
  # @option options [Integer] bulk_size bulk API chunk size in bytes; if passed, the request is performed several times for each chunk, empty by default
@@ -110,23 +112,51 @@ module Chewy
110
112
  # or normal composing under the hood.
111
113
  #
112
114
  # @param object [Object] a data source object
113
- # @param crutches [Object] optional crutches object; if ommited - a crutch for the single passed object is created as a fallback
115
+ # @param crutches [Object] optional crutches object; if omitted - a crutch for the single passed object is created as a fallback
114
116
  # @param fields [Array<Symbol>] and array of fields to restrict the generated document
115
117
  # @return [Hash] a JSON-ready hash
116
118
  def compose(object, crutches = nil, fields: [])
117
- crutches ||= Chewy::Type::Crutch::Crutches.new self, [object]
119
+ crutches ||= Chewy::Index::Crutch::Crutches.new self, [object]
118
120
 
119
- if witchcraft? && build_root.children.present?
121
+ if witchcraft? && root.children.present?
120
122
  cauldron(fields: fields).brew(object, crutches)
121
123
  else
122
- build_root.compose(object, crutches, fields: fields)
124
+ root.compose(object, crutches, fields: fields)
123
125
  end
124
126
  end
125
127
 
126
128
  private
127
129
 
130
+ def intercept_import_using_strategy(*args)
131
+ args_clone = args.deep_dup
132
+ options = args_clone.extract_options!
133
+ strategy = options.delete(:strategy)
134
+
135
+ return import_routine(*args) if strategy.blank?
136
+
137
+ ids = args_clone.flatten
138
+ return {} if ids.blank?
139
+ return {argument: {"#{strategy} supports ids only!" => ids}} unless ids.all? do |id|
140
+ id.respond_to?(:to_i)
141
+ end
142
+
143
+ case strategy
144
+ when :delayed_sidekiq
145
+ begin
146
+ Chewy::Strategy::DelayedSidekiq::Scheduler.new(self, ids, options).postpone
147
+ {} # success. errors handling convention
148
+ rescue StandardError => e
149
+ {scheduler: {e.message => ids}}
150
+ end
151
+ else
152
+ {argument: {"unsupported strategy: '#{strategy}'" => ids}}
153
+ end
154
+ end
155
+
128
156
  def import_routine(*args)
129
- routine = Routine.new(self, args.extract_options!)
157
+ return if !args.first.nil? && empty_objects_or_scope?(args.first)
158
+
159
+ routine = Routine.new(self, **args.extract_options!)
130
160
  routine.create_indexes!
131
161
 
132
162
  if routine.parallel_options
@@ -136,8 +166,16 @@ module Chewy
136
166
  end
137
167
  end
138
168
 
169
+ def empty_objects_or_scope?(objects_or_scope)
170
+ if objects_or_scope.respond_to?(:empty?)
171
+ objects_or_scope.empty?
172
+ else
173
+ objects_or_scope.blank?
174
+ end
175
+ end
176
+
139
177
  def import_linear(objects, routine)
140
- ActiveSupport::Notifications.instrument 'import_objects.chewy', type: self do |payload|
178
+ ActiveSupport::Notifications.instrument 'import_objects.chewy', index: self do |payload|
141
179
  adapter.import(*objects, routine.options) do |action_objects|
142
180
  routine.process(**action_objects)
143
181
  end
@@ -151,17 +189,25 @@ module Chewy
151
189
  def import_parallel(objects, routine)
152
190
  raise "The `parallel` gem is required for parallel import, please add `gem 'parallel'` to your Gemfile" unless '::Parallel'.safe_constantize
153
191
 
154
- ActiveSupport::Notifications.instrument 'import_objects.chewy', type: self do |payload|
192
+ ActiveSupport::Notifications.instrument 'import_objects.chewy', index: self do |payload|
155
193
  batches = adapter.import_references(*objects, routine.options.slice(:batch_size)).to_a
156
194
 
157
195
  ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
158
- results = ::Parallel.map_with_index(batches, routine.parallel_options, &IMPORT_WORKER.curry[self, routine.options, batches.size])
196
+ results = ::Parallel.map_with_index(
197
+ batches,
198
+ routine.parallel_options,
199
+ &IMPORT_WORKER.curry[self, routine.options, batches.size]
200
+ )
159
201
  ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
160
202
  errors, import, leftovers = process_parallel_import_results(results)
161
203
 
162
204
  if leftovers.present?
163
205
  batches = leftovers.each_slice(routine.options[:batch_size])
164
- results = ::Parallel.map_with_index(batches, routine.parallel_options, &LEFTOVERS_WORKER.curry[self, routine.options, batches.size])
206
+ results = ::Parallel.map_with_index(
207
+ batches,
208
+ routine.parallel_options,
209
+ &LEFTOVERS_WORKER.curry[self, routine.options, batches.size]
210
+ )
165
211
  errors.concat(results.flatten(1))
166
212
  end
167
213
 
@@ -1,5 +1,5 @@
1
1
  module Chewy
2
- class Type
2
+ class Index
3
3
  module Mapping
4
4
  extend ActiveSupport::Concern
5
5
 
@@ -13,47 +13,50 @@ module Chewy
13
13
  end
14
14
 
15
15
  module ClassMethods
16
- # Defines root object for mapping and is optional for type
16
+ # Defines root object for mapping and is optional for index
17
17
  # definition. Use it only if you need to pass options for root
18
18
  # object mapping, such as `date_detection` or `dynamic_date_formats`
19
19
  #
20
+ # @example
20
21
  # class UsersIndex < Chewy::Index
21
- # define_type User do
22
- # # root object defined implicitly and optionless for current type
23
- # field :full_name, type: 'string'
24
- # end
22
+ # index_scope User
23
+ # # root object defined implicitly and optionless for current type
24
+ # field :full_name, type: 'keyword'
25
25
  # end
26
26
  #
27
27
  # class CarsIndex < Chewy::Index
28
- # define_type Car do
29
- # # explicit root definition with additional options
30
- # root dynamic_date_formats: ['yyyy-MM-dd'] do
31
- # field :model_name, type: 'string'
32
- # end
28
+ # index_scope Car
29
+ # # explicit root definition with additional options
30
+ # root dynamic_date_formats: ['yyyy-MM-dd'] do
31
+ # field :model_name, type: 'keyword'
33
32
  # end
34
33
  # end
35
34
  #
36
- def root(options = {}, &block)
37
- raise 'Root is already defined' if root_object
38
- build_root(options, &block)
35
+ def root(**options)
36
+ self.root_object ||= Chewy::Fields::Root.new(:root, **Chewy.default_root_options.merge(options))
37
+ root_object.update_options!(**options)
38
+ yield if block_given?
39
+ root_object
39
40
  end
40
41
 
41
- # Defines mapping field for current type
42
+ # Defines mapping field for index
42
43
  #
44
+ # @example
43
45
  # class UsersIndex < Chewy::Index
44
- # define_type User do
45
- # # passing all the options to field definition:
46
- # field :full_name, type: 'string', analyzer: 'special'
47
- # end
46
+ # index_scope User
47
+ # # passing all the options to field definition:
48
+ # field :full_name, analyzer: 'special'
48
49
  # end
49
50
  #
50
51
  # The `type` is optional and defaults to `string` if not defined:
51
52
  #
53
+ # @example
52
54
  # field :full_name
53
55
  #
54
56
  # Also, multiple fields might be defined with one call and
55
57
  # with the same options:
56
58
  #
59
+ # @example
57
60
  # field :first_name, :last_name, analyzer: 'special'
58
61
  #
59
62
  # The only special option in the field definition
@@ -61,31 +64,35 @@ module Chewy
61
64
  # method will be called for the indexed object. Also
62
65
  # `:value` might be a proc or indexed object method name:
63
66
  #
67
+ # @example
64
68
  # class User < ActiveRecord::Base
65
69
  # def user_full_name
66
70
  # [first_name, last_name].join(' ')
67
71
  # end
68
72
  # end
69
73
  #
70
- # field :full_name, type: 'string', value: :user_full_name
74
+ # field :full_name, type: 'keyword', value: :user_full_name
71
75
  #
72
76
  # The proc evaluates inside the indexed object context if
73
77
  # its arity is 0 and in present contexts if there is an argument:
74
78
  #
75
- # field :full_name, type: 'string', value: -> { [first_name, last_name].join(' ') }
79
+ # @example
80
+ # field :full_name, type: 'keyword', value: -> { [first_name, last_name].join(' ') }
76
81
  #
77
82
  # separator = ' '
78
- # field :full_name, type: 'string', value: ->(user) { [user.first_name, user.last_name].join(separator) }
83
+ # field :full_name, type: 'keyword', value: ->(user) { [user.first_name, user.last_name].join(separator) }
79
84
  #
80
85
  # If array was returned as value - it will be put in index as well.
81
86
  #
82
- # field :tags, type: 'string', value: -> { tags.map(&:name) }
87
+ # @example
88
+ # field :tags, type: 'keyword', value: -> { tags.map(&:name) }
83
89
  #
84
90
  # Fields supports nesting in case of `object` field type. If
85
91
  # `user.quiz` will return an array of objects, then result index content
86
92
  # will be an array of hashes, if `user.quiz` is not a collection association
87
93
  # then just values hash will be put in the index.
88
94
  #
95
+ # @example
89
96
  # field :quiz do
90
97
  # field :question, :answer
91
98
  # field :score, type: 'integer'
@@ -93,6 +100,7 @@ module Chewy
93
100
  #
94
101
  # Nested fields are composed from nested objects:
95
102
  #
103
+ # @example
96
104
  # field :name, value: -> { name_translations } do
97
105
  # field :ru, value: ->(name) { name['ru'] }
98
106
  # field :en, value: ->(name) { name['en'] }
@@ -101,110 +109,103 @@ module Chewy
101
109
  # Of course it is possible to define object fields contents dynamically
102
110
  # but make sure evaluation proc returns hash:
103
111
  #
112
+ # @example
104
113
  # field :name, type: 'object', value: -> { name_translations }
105
114
  #
106
115
  # The special case is multi_field. If type options and block are
107
116
  # both present field is treated as a multi-field. In that case field
108
117
  # composition changes satisfy elasticsearch rules:
109
118
  #
110
- # field :full_name, type: 'string', analyzer: 'name', value: ->{ full_name.try(:strip) } do
119
+ # @example
120
+ # field :full_name, type: 'text', analyzer: 'name', value: ->{ full_name.try(:strip) } do
111
121
  # field :sorted, analyzer: 'sorted'
112
122
  # end
113
123
  #
114
- def field(*args, &block)
115
- options = args.extract_options!
116
- build_root
117
-
124
+ def field(*args, **options, &block)
118
125
  if args.size > 1
119
- args.map { |name| field(name, options) }
126
+ args.map { |name| field(name, **options) }
120
127
  else
121
- expand_nested(Chewy::Fields::Base.new(args.first, options), &block)
128
+ expand_nested(Chewy::Fields::Base.new(args.first, **options), &block)
122
129
  end
123
130
  end
124
131
 
125
132
  # Defines an aggregation that can be bound to a query or filter
126
133
  #
127
- # Suppose that a user has posts and each post has ratings
128
- # avg_post_rating is the mean of all ratings
129
- #
134
+ # @example
135
+ # # Suppose that a user has posts and each post has ratings
136
+ # # avg_post_rating is the mean of all ratings
130
137
  # class UsersIndex < Chewy::Index
131
- # define_type User do
132
- # field :posts do
133
- # field :rating
134
- # end
135
- #
136
- # agg :avg_rating do
137
- # { avg: { field: 'posts.rating' } }
138
- # end
138
+ # index_scope User
139
+ # field :posts do
140
+ # field :rating
141
+ # end
142
+ #
143
+ # agg :avg_rating do
144
+ # { avg: { field: 'posts.rating' } }
139
145
  # end
140
146
  # end
141
147
  def agg(name, &block)
142
- build_root
143
148
  self._agg_defs = _agg_defs.merge(name => block)
144
149
  end
145
150
  alias_method :aggregation, :agg
146
151
 
147
152
  # Defines dynamic template in mapping root objects
148
153
  #
154
+ # @example
149
155
  # class CarsIndex < Chewy::Index
150
- # define_type Car do
151
- # template 'model.*', type: 'string', analyzer: 'special'
152
- # field 'model', type: 'object' # here we can put { de: 'Der Mercedes', en: 'Mercedes' }
153
- # # and template will be applyed to this field
154
- # end
156
+ # index_scope Car
157
+ # template 'model.*', type: 'text', analyzer: 'special'
158
+ # field 'model', type: 'object' # here we can put { de: 'Der Mercedes', en: 'Mercedes' }
159
+ # # and template will be applied to this field
155
160
  # end
156
161
  #
157
162
  # Name for each template is generated with the following
158
- # rule: "template_#{dynamic_templates.size + 1}".
163
+ # rule: `template_#!{dynamic_templates.size + 1}`.
159
164
  #
165
+ # @example Templates
160
166
  # template 'tit*', mapping_hash
161
167
  # template 'title.*', mapping_hash # dot in template causes "path_match" using
162
168
  # template /tit.+/, mapping_hash # using "match_pattern": "regexp"
163
169
  # template /title\..+/, mapping_hash # "\." - escaped dot causes "path_match" using
164
- # template /tit.+/, 'string', mapping_hash # "match_mapping_type" as the optionsl second argument
170
+ # template /tit.+/, type: 'text', mapping_hash # "match_mapping_type" as an optional second argument
165
171
  # template template42: {match: 'hello*', mapping: {type: 'object'}} # or even pass a template as is
166
172
  #
167
- def template(*args)
168
- build_root.dynamic_template(*args)
173
+ def template(*args, **options)
174
+ root.dynamic_template(*args, **options)
169
175
  end
170
176
  alias_method :dynamic_template, :template
171
177
 
172
178
  # Returns compiled mappings hash for current type
173
179
  #
174
180
  def mappings_hash
175
- root_object ? root_object.mappings_hash : {}
181
+ root.mappings_hash
176
182
  end
177
183
 
178
184
  # Check whether the type has outdated_sync_field defined with a simple value.
179
185
  #
180
186
  # @return [true, false]
181
187
  def supports_outdated_sync?
182
- updated_at_field = root_object.child_hash[outdated_sync_field] if root_object && outdated_sync_field
188
+ updated_at_field = root.child_hash[outdated_sync_field] if outdated_sync_field
183
189
  !!updated_at_field && updated_at_field.value.nil?
184
190
  end
185
191
 
186
192
  private
187
193
 
188
- def expand_nested(field, &block)
194
+ def expand_nested(field)
195
+ @_current_field ||= root
196
+
189
197
  if @_current_field
190
198
  field.parent = @_current_field
191
199
  @_current_field.children.push(field)
192
200
  end
193
201
 
194
- return unless block
202
+ return unless block_given?
195
203
 
196
204
  previous_field = @_current_field
197
205
  @_current_field = field
198
206
  yield
199
207
  @_current_field = previous_field
200
208
  end
201
-
202
- def build_root(options = {}, &block)
203
- return root_object if root_object
204
- self.root_object = Chewy::Fields::Root.new(type_name, Chewy.default_root_options.merge(options))
205
- expand_nested(root_object, &block)
206
- @_current_field = root_object
207
- end
208
209
  end
209
210
  end
210
211
  end
@@ -0,0 +1,87 @@
1
+ module Chewy
2
+ class Index
3
+ module Observe
4
+ module Helpers
5
+ def update_proc(index_name, *args, &block)
6
+ options = args.extract_options!
7
+ method = args.first
8
+
9
+ proc do
10
+ reference = if index_name.is_a?(Proc)
11
+ if index_name.arity.zero?
12
+ instance_exec(&index_name)
13
+ else
14
+ index_name.call(self)
15
+ end
16
+ else
17
+ index_name
18
+ end
19
+
20
+ index = Chewy.derive_name(reference)
21
+
22
+ next if Chewy.strategy.current.name == :bypass
23
+
24
+ backreference = if method && method.to_s == 'self'
25
+ self
26
+ elsif method
27
+ send(method)
28
+ else
29
+ instance_eval(&block)
30
+ end
31
+
32
+ index.update_index(backreference, options)
33
+ end
34
+ end
35
+
36
+ def extract_callback_options!(args)
37
+ options = args.extract_options!
38
+ result = options.each_key.with_object({}) do |key, hash|
39
+ hash[key] = options.delete(key) if %i[if unless].include?(key)
40
+ end
41
+ args.push(options) unless options.empty?
42
+ result
43
+ end
44
+ end
45
+
46
+ extend Helpers
47
+
48
+ module ActiveRecordMethods
49
+ extend ActiveSupport::Concern
50
+
51
+ def run_chewy_callbacks
52
+ chewy_callbacks.each { |callback| callback.call(self) }
53
+ end
54
+
55
+ def update_chewy_indices
56
+ Chewy.strategy.current.update_chewy_indices(self)
57
+ end
58
+
59
+ included do
60
+ class_attribute :chewy_callbacks, default: []
61
+ end
62
+
63
+ class_methods do
64
+ def initialize_chewy_callbacks
65
+ if Chewy.use_after_commit_callbacks
66
+ after_commit :update_chewy_indices, on: %i[create update]
67
+ after_commit :run_chewy_callbacks, on: :destroy
68
+ else
69
+ after_save :update_chewy_indices
70
+ after_destroy :run_chewy_callbacks
71
+ end
72
+ end
73
+
74
+ ruby2_keywords def update_index(type_name, *args, &block)
75
+ callback_options = Observe.extract_callback_options!(args)
76
+ update_proc = Observe.update_proc(type_name, *args, &block)
77
+ callback = Chewy::Index::Observe::Callback.new(update_proc, callback_options)
78
+
79
+ initialize_chewy_callbacks if chewy_callbacks.empty?
80
+
81
+ self.chewy_callbacks = chewy_callbacks.dup << callback
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,34 @@
1
+ module Chewy
2
+ class Index
3
+ module Observe
4
+ class Callback
5
+ def initialize(executable, filters = {})
6
+ @executable = executable
7
+ @if_filter = filters[:if]
8
+ @unless_filter = filters[:unless]
9
+ end
10
+
11
+ def call(context)
12
+ return if !@if_filter.nil? && !eval_filter(@if_filter, context)
13
+ return if !@unless_filter.nil? && eval_filter(@unless_filter, context)
14
+
15
+ eval_proc(@executable, context)
16
+ end
17
+
18
+ private
19
+
20
+ def eval_filter(filter, context)
21
+ case filter
22
+ when Symbol then context.send(filter)
23
+ when Proc then eval_proc(filter, context)
24
+ else filter
25
+ end
26
+ end
27
+
28
+ def eval_proc(executable, context)
29
+ executable.arity.zero? ? context.instance_exec(&executable) : executable.call(context)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,17 @@
1
+ require 'chewy/index/observe/callback'
2
+ require 'chewy/index/observe/active_record_methods'
3
+
4
+ module Chewy
5
+ class Index
6
+ module Observe
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def update_index(objects, options = {})
11
+ Chewy.strategy.current.update(self, objects, options)
12
+ true
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -4,12 +4,14 @@ module Chewy
4
4
  # hash. At first, you need to store some analyzers or other
5
5
  # analysis options to the corresponding repository:
6
6
  #
7
+ # @example
7
8
  # Chewy.analyzer :title_analyzer, type: 'custom', filter: %w(lowercase icu_folding title_nysiis)
8
9
  # Chewy.filter :title_nysiis, type: 'phonetic', encoder: 'nysiis', replace: false
9
10
  #
10
11
  # `title_nysiis` filter here will be expanded automatically when
11
12
  # `title_analyzer` analyser will be used in index settings:
12
13
  #
14
+ # @example
13
15
  # class ProductsIndex < Chewy::Index
14
16
  # settings analysis: {
15
17
  # analyzer: [