chewy 5.1.0 → 7.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (234) hide show
  1. checksums.yaml +4 -4
  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 +73 -0
  7. data/.rubocop.yml +13 -8
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +449 -347
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -7
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +423 -311
  16. data/chewy.gemspec +8 -10
  17. data/gemfiles/rails.5.2.activerecord.gemfile +9 -14
  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 +42 -60
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +80 -20
  24. data/lib/chewy/fields/root.rb +7 -17
  25. data/lib/chewy/index/actions.rb +62 -35
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +2 -3
  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/{type → index}/crutch.rb +5 -5
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +6 -7
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +17 -16
  36. data/lib/chewy/{type → index}/import.rb +51 -33
  37. data/lib/chewy/{type → index}/mapping.rb +32 -37
  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/specification.rb +1 -0
  42. data/lib/chewy/{type → index}/syncer.rb +61 -62
  43. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  44. data/lib/chewy/{type → index}/wrapper.rb +13 -3
  45. data/lib/chewy/index.rb +46 -96
  46. data/lib/chewy/journal.rb +25 -14
  47. data/lib/chewy/minitest/helpers.rb +86 -13
  48. data/lib/chewy/minitest/search_index_receiver.rb +22 -26
  49. data/lib/chewy/multi_search.rb +62 -0
  50. data/lib/chewy/railtie.rb +6 -20
  51. data/lib/chewy/rake_helper.rb +136 -108
  52. data/lib/chewy/rspec/build_query.rb +12 -0
  53. data/lib/chewy/rspec/helpers.rb +55 -0
  54. data/lib/chewy/rspec/update_index.rb +55 -44
  55. data/lib/chewy/rspec.rb +2 -0
  56. data/lib/chewy/runtime.rb +1 -1
  57. data/lib/chewy/search/loader.rb +19 -41
  58. data/lib/chewy/search/parameters/collapse.rb +16 -0
  59. data/lib/chewy/search/parameters/concerns/query_storage.rb +2 -2
  60. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  61. data/lib/chewy/search/parameters/indices.rb +12 -57
  62. data/lib/chewy/search/parameters/none.rb +1 -3
  63. data/lib/chewy/search/parameters/order.rb +6 -19
  64. data/lib/chewy/search/parameters/source.rb +5 -1
  65. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  66. data/lib/chewy/search/parameters.rb +7 -4
  67. data/lib/chewy/search/query_proxy.rb +9 -2
  68. data/lib/chewy/search/request.rb +180 -154
  69. data/lib/chewy/search/response.rb +5 -5
  70. data/lib/chewy/search/scoping.rb +7 -8
  71. data/lib/chewy/search/scrolling.rb +16 -13
  72. data/lib/chewy/search.rb +7 -22
  73. data/lib/chewy/stash.rb +19 -30
  74. data/lib/chewy/strategy/active_job.rb +2 -2
  75. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  76. data/lib/chewy/strategy/base.rb +10 -0
  77. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  78. data/lib/chewy/strategy/sidekiq.rb +3 -2
  79. data/lib/chewy/strategy.rb +5 -19
  80. data/lib/chewy/version.rb +1 -1
  81. data/lib/chewy.rb +36 -80
  82. data/lib/generators/chewy/install_generator.rb +1 -1
  83. data/lib/tasks/chewy.rake +26 -32
  84. data/migration_guide.md +56 -0
  85. data/spec/chewy/config_spec.rb +15 -61
  86. data/spec/chewy/fields/base_spec.rb +432 -145
  87. data/spec/chewy/fields/root_spec.rb +20 -28
  88. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  89. data/spec/chewy/index/actions_spec.rb +388 -55
  90. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  91. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  92. data/spec/chewy/index/aliases_spec.rb +3 -3
  93. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  94. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  95. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +14 -22
  96. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  97. data/spec/chewy/{type → index}/import_spec.rb +149 -96
  98. data/spec/chewy/index/mapping_spec.rb +135 -0
  99. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  100. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  101. data/spec/chewy/index/observe_spec.rb +143 -0
  102. data/spec/chewy/index/settings_spec.rb +3 -1
  103. data/spec/chewy/index/specification_spec.rb +20 -30
  104. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  105. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  106. data/spec/chewy/index/wrapper_spec.rb +100 -0
  107. data/spec/chewy/index_spec.rb +69 -137
  108. data/spec/chewy/journal_spec.rb +46 -91
  109. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  110. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  111. data/spec/chewy/multi_search_spec.rb +84 -0
  112. data/spec/chewy/rake_helper_spec.rb +293 -101
  113. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  114. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  115. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  116. data/spec/chewy/runtime_spec.rb +2 -2
  117. data/spec/chewy/search/loader_spec.rb +19 -53
  118. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  119. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  120. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  121. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  122. data/spec/chewy/search/parameters/indices_spec.rb +26 -118
  123. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  124. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  125. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  126. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  127. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  128. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  129. data/spec/chewy/search/parameters_spec.rb +23 -7
  130. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  131. data/spec/chewy/search/request_spec.rb +344 -149
  132. data/spec/chewy/search/response_spec.rb +35 -25
  133. data/spec/chewy/search/scrolling_spec.rb +28 -26
  134. data/spec/chewy/search_spec.rb +69 -59
  135. data/spec/chewy/stash_spec.rb +16 -26
  136. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  137. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  138. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  139. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  140. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  141. data/spec/chewy/strategy_spec.rb +19 -15
  142. data/spec/chewy_spec.rb +17 -110
  143. data/spec/spec_helper.rb +6 -29
  144. data/spec/support/active_record.rb +43 -5
  145. metadata +102 -198
  146. data/.travis.yml +0 -45
  147. data/Appraisals +0 -81
  148. data/LEGACY_DSL.md +0 -497
  149. data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
  150. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  151. data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
  152. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +0 -16
  153. data/gemfiles/rails.5.0.activerecord.gemfile +0 -16
  154. data/gemfiles/rails.5.0.mongoid.6.1.gemfile +0 -16
  155. data/gemfiles/rails.5.1.activerecord.gemfile +0 -16
  156. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +0 -16
  157. data/gemfiles/sequel.4.45.gemfile +0 -11
  158. data/lib/chewy/backports/deep_dup.rb +0 -46
  159. data/lib/chewy/backports/duplicable.rb +0 -91
  160. data/lib/chewy/query/compose.rb +0 -68
  161. data/lib/chewy/query/criteria.rb +0 -191
  162. data/lib/chewy/query/filters.rb +0 -244
  163. data/lib/chewy/query/loading.rb +0 -110
  164. data/lib/chewy/query/nodes/and.rb +0 -25
  165. data/lib/chewy/query/nodes/base.rb +0 -17
  166. data/lib/chewy/query/nodes/bool.rb +0 -34
  167. data/lib/chewy/query/nodes/equal.rb +0 -34
  168. data/lib/chewy/query/nodes/exists.rb +0 -20
  169. data/lib/chewy/query/nodes/expr.rb +0 -28
  170. data/lib/chewy/query/nodes/field.rb +0 -110
  171. data/lib/chewy/query/nodes/has_child.rb +0 -15
  172. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  173. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  174. data/lib/chewy/query/nodes/match_all.rb +0 -11
  175. data/lib/chewy/query/nodes/missing.rb +0 -20
  176. data/lib/chewy/query/nodes/not.rb +0 -25
  177. data/lib/chewy/query/nodes/or.rb +0 -25
  178. data/lib/chewy/query/nodes/prefix.rb +0 -19
  179. data/lib/chewy/query/nodes/query.rb +0 -20
  180. data/lib/chewy/query/nodes/range.rb +0 -63
  181. data/lib/chewy/query/nodes/raw.rb +0 -15
  182. data/lib/chewy/query/nodes/regexp.rb +0 -35
  183. data/lib/chewy/query/nodes/script.rb +0 -20
  184. data/lib/chewy/query/pagination.rb +0 -25
  185. data/lib/chewy/query.rb +0 -1142
  186. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  187. data/lib/chewy/search/parameters/types.rb +0 -20
  188. data/lib/chewy/strategy/resque.rb +0 -27
  189. data/lib/chewy/strategy/shoryuken.rb +0 -40
  190. data/lib/chewy/type/actions.rb +0 -43
  191. data/lib/chewy/type/adapter/mongoid.rb +0 -67
  192. data/lib/chewy/type/adapter/sequel.rb +0 -93
  193. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  194. data/lib/chewy/type/observe.rb +0 -82
  195. data/lib/chewy/type.rb +0 -117
  196. data/lib/sequel/plugins/chewy_observe.rb +0 -63
  197. data/spec/chewy/query/criteria_spec.rb +0 -700
  198. data/spec/chewy/query/filters_spec.rb +0 -201
  199. data/spec/chewy/query/loading_spec.rb +0 -124
  200. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  201. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  202. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  203. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  204. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  205. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  206. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  207. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  208. data/spec/chewy/query/nodes/not_spec.rb +0 -14
  209. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  210. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  211. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  212. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  213. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  214. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  215. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  216. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  217. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  218. data/spec/chewy/query/pagination_spec.rb +0 -39
  219. data/spec/chewy/query_spec.rb +0 -637
  220. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  221. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  222. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  223. data/spec/chewy/strategy/resque_spec.rb +0 -46
  224. data/spec/chewy/strategy/shoryuken_spec.rb +0 -66
  225. data/spec/chewy/type/actions_spec.rb +0 -50
  226. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  227. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  228. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  229. data/spec/chewy/type/mapping_spec.rb +0 -173
  230. data/spec/chewy/type/observe_spec.rb +0 -137
  231. data/spec/chewy/type/wrapper_spec.rb +0 -98
  232. data/spec/chewy/type_spec.rb +0 -55
  233. data/spec/support/mongoid.rb +0 -93
  234. data/spec/support/sequel.rb +0 -80
data/lib/chewy.rb CHANGED
@@ -4,18 +4,18 @@ require 'active_support/deprecation'
4
4
  require 'active_support/json'
5
5
  require 'active_support/log_subscriber'
6
6
 
7
+ require 'active_support/isolated_execution_state' if ActiveSupport::VERSION::MAJOR >= 7
7
8
  require 'active_support/core_ext/array/access'
8
9
  require 'active_support/core_ext/array/wrap'
9
10
  require 'active_support/core_ext/enumerable'
10
11
  require 'active_support/core_ext/hash/reverse_merge'
12
+ require 'active_support/core_ext/hash/keys'
11
13
  require 'active_support/core_ext/numeric/time'
12
14
  require 'active_support/core_ext/numeric/bytes'
13
15
  require 'active_support/core_ext/object/blank'
14
16
  require 'active_support/core_ext/object/inclusion'
15
17
  require 'active_support/core_ext/string/inflections'
16
18
 
17
- require 'i18n/core_ext/hash'
18
- require 'chewy/backports/deep_dup' unless Object.respond_to?(:deep_dup)
19
19
  require 'singleton'
20
20
  require 'base64'
21
21
 
@@ -29,20 +29,12 @@ end
29
29
 
30
30
  try_require 'kaminari'
31
31
  try_require 'kaminari/core'
32
- try_require 'will_paginate'
33
- try_require 'will_paginate/collection'
34
32
  try_require 'parallel'
35
33
 
36
34
  ActiveSupport.on_load(:active_record) do
37
- try_require 'will_paginate/active_record'
38
35
  try_require 'kaminari/activerecord'
39
36
  end
40
37
 
41
- ActiveSupport.on_load(:mongoid) do
42
- try_require 'will_paginate/mongoid'
43
- try_require 'kaminari/mongoid'
44
- end
45
-
46
38
  require 'chewy/version'
47
39
  require 'chewy/errors'
48
40
  require 'chewy/config'
@@ -52,100 +44,59 @@ require 'chewy/runtime'
52
44
  require 'chewy/log_subscriber'
53
45
  require 'chewy/strategy'
54
46
  require 'chewy/index'
55
- require 'chewy/type'
56
47
  require 'chewy/fields/base'
57
48
  require 'chewy/fields/root'
58
49
  require 'chewy/journal'
59
50
  require 'chewy/railtie' if defined?(::Rails::Railtie)
60
51
 
61
52
  ActiveSupport.on_load(:active_record) do
62
- extend Chewy::Type::Observe::ActiveRecordMethods
63
- end
64
-
65
- ActiveSupport.on_load(:mongoid) do
66
- module Mongoid
67
- module Document
68
- module ClassMethods
69
- include Chewy::Type::Observe::MongoidMethods
70
- end
71
- end
72
- end
53
+ include Chewy::Index::Observe::ActiveRecordMethods
73
54
  end
74
55
 
75
56
  module Chewy
76
57
  @adapters = [
77
- Chewy::Type::Adapter::ActiveRecord,
78
- Chewy::Type::Adapter::Mongoid,
79
- Chewy::Type::Adapter::Sequel,
80
- Chewy::Type::Adapter::Object
58
+ Chewy::Index::Adapter::ActiveRecord,
59
+ Chewy::Index::Adapter::Object
81
60
  ]
82
61
 
83
62
  class << self
84
63
  attr_accessor :adapters
85
64
 
86
- # Derives a single type for the passed string identifier if possible.
87
- #
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
93
- #
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)
98
- return name if name.is_a?(Class) && name < Chewy::Type
99
-
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
65
+ # A thread-local variables accessor
66
+ # @return [Hash]
67
+ def current
68
+ unless Thread.current.thread_variable?(:chewy)
69
+ Thread.current.thread_variable_set(:chewy, {})
70
+ end
71
+
72
+ Thread.current.thread_variable_get(:chewy)
103
73
  end
104
74
 
105
- # Derives all the types for the passed string identifier if possible.
75
+ # Derives an index for the passed string identifier if possible.
106
76
  #
107
77
  # @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]
78
+ # Chewy.derive_name(UsersIndex) # => UsersIndex
79
+ # Chewy.derive_name('namespace/users') # => Namespace::UsersIndex
80
+ # Chewy.derive_name('missing') # => raises Chewy::UndefinedIndex
111
81
  #
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)
82
+ # @param index_name [String, Chewy::Index] index identifier or class
83
+ # @raise [Chewy::UndefinedIndex] in cases when it is impossible to find index
84
+ # @return [Chewy::Index]
85
+ def derive_name(index_name)
86
+ return index_name if index_name.is_a?(Class) && index_name < Chewy::Index
117
87
 
118
- index_name, type_name = from.split('#', 2)
119
88
  class_name = "#{index_name.camelize.gsub(/Index\z/, '')}Index"
120
89
  index = class_name.safe_constantize
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]
125
- else
126
- index.types
127
- end
128
- end
129
90
 
130
- # Creates Chewy::Type ancestor defining index and adapter methods.
131
- #
132
- def create_type(index, target, options = {}, &block)
133
- type = Class.new(Chewy::Type)
134
-
135
- adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
136
-
137
- index.const_set(adapter.name, type)
138
- type.send(:define_singleton_method, :index) { index }
139
- type.send(:define_singleton_method, :adapter) { adapter }
91
+ return index if index && index < Chewy::Index
140
92
 
141
- type.class_eval(&block) if block
142
- type
93
+ raise Chewy::UndefinedIndex, "Can not find index named `#{class_name}`"
143
94
  end
144
95
 
145
96
  # Main elasticsearch-ruby client instance
146
97
  #
147
98
  def client
148
- Thread.current[:chewy_client] ||= begin
99
+ Chewy.current[:chewy_client] ||= begin
149
100
  client_configuration = configuration.deep_dup
150
101
  client_configuration.delete(:prefix) # used by Chewy, not relevant to Elasticsearch::Client
151
102
  block = client_configuration[:transport_options].try(:delete, :proc)
@@ -159,7 +110,9 @@ module Chewy
159
110
  # Does nothing in case of config `wait_for_status` is undefined.
160
111
  #
161
112
  def wait_for_status
162
- client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status] if Chewy.configuration[:wait_for_status].present?
113
+ if Chewy.configuration[:wait_for_status].present?
114
+ client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status]
115
+ end
163
116
  end
164
117
 
165
118
  # Deletes all corresponding indexes with current prefix from ElasticSearch.
@@ -195,15 +148,15 @@ module Chewy
195
148
  # city3.do_update! # index updated again
196
149
  #
197
150
  def strategy(name = nil, &block)
198
- Thread.current[:chewy_strategy] ||= Chewy::Strategy.new
151
+ Chewy.current[:chewy_strategy] ||= Chewy::Strategy.new
199
152
  if name
200
153
  if block
201
- Thread.current[:chewy_strategy].wrap name, &block
154
+ Chewy.current[:chewy_strategy].wrap name, &block
202
155
  else
203
- Thread.current[:chewy_strategy].push name
156
+ Chewy.current[:chewy_strategy].push name
204
157
  end
205
158
  else
206
- Thread.current[:chewy_strategy]
159
+ Chewy.current[:chewy_strategy]
207
160
  end
208
161
  end
209
162
 
@@ -227,7 +180,10 @@ module Chewy
227
180
 
228
181
  def eager_load!
229
182
  return unless defined?(Chewy::Railtie)
230
- dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[Chewy.configuration[:indices_path]] }.compact.map(&:existent).flatten.uniq
183
+
184
+ dirs = Chewy::Railtie.all_engines.map do |engine|
185
+ engine.paths[Chewy.configuration[:indices_path]]
186
+ end.compact.map(&:existent).flatten.uniq
231
187
 
232
188
  dirs.each do |dir|
233
189
  Dir.glob(File.join(dir, '**/*.rb')).each do |file|
@@ -1,7 +1,7 @@
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', __dir__)
5
5
 
6
6
  def copy_configuration
7
7
  template 'chewy.yml', 'config/chewy.yml'
data/lib/tasks/chewy.rake CHANGED
@@ -23,22 +23,22 @@ end
23
23
  namespace :chewy do
24
24
  desc 'Destroys, recreates and imports data for the specified indexes or all of them'
25
25
  task reset: :environment do |_task, args|
26
- Chewy::RakeHelper.reset(parse_classes(args.extras))
26
+ Chewy::RakeHelper.reset(**parse_classes(args.extras))
27
27
  end
28
28
 
29
29
  desc 'Resets data for the specified indexes or all of them only if the index specification is changed'
30
30
  task upgrade: :environment do |_task, args|
31
- Chewy::RakeHelper.upgrade(parse_classes(args.extras))
31
+ Chewy::RakeHelper.upgrade(**parse_classes(args.extras))
32
32
  end
33
33
 
34
34
  desc 'Updates data for the specified indexes/types or all of them'
35
35
  task update: :environment do |_task, args|
36
- Chewy::RakeHelper.update(parse_classes(args.extras))
36
+ Chewy::RakeHelper.update(**parse_classes(args.extras))
37
37
  end
38
38
 
39
39
  desc 'Synchronizes data for the specified indexes/types or all of them'
40
40
  task sync: :environment do |_task, args|
41
- Chewy::RakeHelper.sync(parse_classes(args.extras))
41
+ Chewy::RakeHelper.sync(**parse_classes(args.extras))
42
42
  end
43
43
 
44
44
  desc 'Resets all the indexes with the specification changed and synchronizes the rest of them'
@@ -47,25 +47,35 @@ namespace :chewy do
47
47
  Chewy::RakeHelper.sync(except: processed)
48
48
  end
49
49
 
50
+ desc 'Reindex data from source index to destination index'
51
+ task :reindex, %i[source dest] => :environment do |_task, args|
52
+ Chewy::RakeHelper.reindex(source: args[:source], dest: args[:dest])
53
+ end
54
+
55
+ desc 'Update mapping of exising index with body hash'
56
+ task :update_mapping, %i[index_name] => :environment do |_task, args|
57
+ Chewy::RakeHelper.update_mapping(name: args[:index_name])
58
+ end
59
+
50
60
  namespace :parallel do
51
61
  desc 'Parallel version of `rake chewy:reset`'
52
62
  task reset: :environment do |_task, args|
53
- Chewy::RakeHelper.reset(parse_parallel_args(args.extras))
63
+ Chewy::RakeHelper.reset(**parse_parallel_args(args.extras))
54
64
  end
55
65
 
56
66
  desc 'Parallel version of `rake chewy:upgrade`'
57
67
  task upgrade: :environment do |_task, args|
58
- Chewy::RakeHelper.upgrade(parse_parallel_args(args.extras))
68
+ Chewy::RakeHelper.upgrade(**parse_parallel_args(args.extras))
59
69
  end
60
70
 
61
71
  desc 'Parallel version of `rake chewy:update`'
62
72
  task update: :environment do |_task, args|
63
- Chewy::RakeHelper.update(parse_parallel_args(args.extras))
73
+ Chewy::RakeHelper.update(**parse_parallel_args(args.extras))
64
74
  end
65
75
 
66
76
  desc 'Parallel version of `rake chewy:sync`'
67
77
  task sync: :environment do |_task, args|
68
- Chewy::RakeHelper.sync(parse_parallel_args(args.extras))
78
+ Chewy::RakeHelper.sync(**parse_parallel_args(args.extras))
69
79
  end
70
80
 
71
81
  desc 'Parallel version of `rake chewy:deploy`'
@@ -79,34 +89,18 @@ namespace :chewy do
79
89
  namespace :journal do
80
90
  desc 'Applies changes that were done after the specified time for the specified indexes/types or all of them'
81
91
  task apply: :environment do |_task, args|
82
- Chewy::RakeHelper.journal_apply(parse_journal_args(args.extras))
92
+ Chewy::RakeHelper.journal_apply(**parse_journal_args(args.extras))
83
93
  end
84
94
 
85
95
  desc 'Removes journal records created before the specified timestamp for the specified indexes/types or all of them'
86
96
  task clean: :environment do |_task, args|
87
- Chewy::RakeHelper.journal_clean(parse_journal_args(args.extras))
88
- end
89
- end
90
-
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
-
94
- Chewy::RakeHelper.subscribed_task_stats do
95
- params = args.extras
96
-
97
- if params.empty?
98
- puts 'Please specify a timestamp like chewy:apply_changes_from[1469528705]'
99
- else
100
- timestamp, retries = params
101
- time = Time.at(timestamp.to_i)
102
- Chewy::Journal.new.apply(time, retries: (retries.to_i if retries))
103
- end
97
+ delete_options = Chewy::RakeHelper.delete_by_query_options_from_env(ENV)
98
+ Chewy::RakeHelper.journal_clean(
99
+ [
100
+ parse_journal_args(args.extras),
101
+ {delete_by_query_options: delete_options}
102
+ ].reduce({}, :merge)
103
+ )
104
104
  end
105
105
  end
106
-
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'
109
-
110
- Chewy::Journal.new.clean(args.extras.first)
111
- end
112
106
  end
@@ -0,0 +1,56 @@
1
+ # Migration guide
2
+
3
+ This document outlines the steps you need to take when migrating between major versions of
4
+ Chewy and Elasticsearch. For simplicity's sake the guide will assume that you're using
5
+ Chewy alongside a matching Elasticsearch version.
6
+
7
+ ## Chewy 6/Elasticsearch 6 to Chewy 7/Elasticsearch 7
8
+
9
+ In order to upgrade Chewy 6/Elasticsearch 6 to Chewy 7/Elasticsearch 7 in the most seamless manner you have to:
10
+
11
+ * Upgrade to the latest 6.x stable releases, namely Chewy 6.0, Elasticsearch 6.8
12
+ * Study carefully [Breaking changes in 7.0](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/breaking-changes-7.0.html), make sure your application conforms.
13
+ * Run your test suite on Chewy 7.0 / Elasticsearch 7
14
+ * Run manual tests on Chewy 7.0 / Elasticsearch 7
15
+ * Upgrade to Chewy 7.0
16
+ * The “total hits” counter is an integer for ES versions < 7 and an object (hash) for the versions starting from 7.0.0. Elasticsearch added a special option, `rest_total_hits_as_int`, to ease the upgrade, that could be appended to any request and results in the old “total hits” format. Unfortunately, this option is not recognized by ES versions prior to 7.0.0, which means that we have to check the version to decide if we need this option.
17
+ Normally Chewy does memoization of the current ES version, but this might be inappropriate for the upgrade, as the version changes live.
18
+ To handle that we have 2 versions of Chewy for this stage of the upgrade: 7.0.0 and 7.0.1. Version 7.0.0 does the memoization and version 7.0.1 requests the current version on every search request.
19
+ * You can use the 7.0.0 version if it's fine for you to have an application restart immediately after ES cluster upgrade.
20
+ * If you're using the 7.0.1 version you might be interested in keeping the timeframe between this step and updating to Chewy 7.1 as small as possible, as version 7.0.1 skips ES version memoization for search requests to help dynamically detect ES version. This leads to an extra version request on each search request, i.e. could affect the overall performance/latency of the search and a load of ES cluster.
21
+ * Perform a [rolling upgrade](https://www.elastic.co/guide/en/elasticsearch/reference//rolling-upgrades.html) of Elasticsearch
22
+ * Run your test suite on Chewy 7.1 / Elasticsearch 7
23
+ * Run manual tests on Chewy 7.1 / Elasticsearch 7
24
+ * Upgrade to Chewy 7.1
25
+ * Upgrade to Chewy 7.2:
26
+ * Remove all the the `Chewy::Type` class usages, e.g. remove `CitiesIndex::City` / `CitiesIndex.city`
27
+ * `CitiesIndex::City.import! ...` becomes `CitiesIndex.import! ...`
28
+ * Update indexes with simplified DSL:
29
+ * `define_type` block -> `index_scope` clause
30
+ * it can be omitted completely, if you don't need to specify the scope or options, e.g. `name`
31
+ * Remove type names from string representations:
32
+ * in `update_index` ActiveRecord helper and RSpec matcher, e.g.
33
+ * `update_index('cities#city')` -> `update_index('cities')`
34
+ * `update_index(UsersIndex::User)` -> `update_index(UsersIndex)`
35
+ * in rake tasks (e.g. `rake chewy:update[cities#city]` -> `rake chewy:update[cities]`)
36
+ * rake tasks output is also changed (e.g. `Imported CitiesIndex::City in 1s, stats: index 3` -> `Imported CitiesIndex in 1s, stats: index 3`)
37
+ * Use index name instead of type name in loader additional scope
38
+ * e.g. `CitiesIndex.filter(...).load(city: {scope: City.where(...)})` -> `CitiesIndex.filter(...).load(cities: {scope: City.where(...)})`
39
+
40
+ ## Chewy 5/Elasticsearch 5 to Chewy 6/Elasticsearch 6
41
+
42
+ In order to upgrade Chewy 5/Elasticsearch 5 to Chewy 6/Elasticsearch 6 in the most seamless manner you have to:
43
+
44
+ * Upgrade to the latest 5.x stable releases, namely Chewy 5.2, Elasticsearch 5.6
45
+ * [Migrate any multi-typed indexes into single-typed](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/removal-of-types.html)
46
+ * Using [multi-index queries](https://github.com/toptal/chewy/pull/657) could be helpful
47
+ * Parent/Child [relationship is deprecated](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/removal-of-types.html#parent-child-mapping-types) in favor of the [join field](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/parent-join.html)
48
+ * Handle deprecation of `string` type & `not_analyzed` value for the `index` mapping parameter:
49
+ * replace fields with `{ type: 'string', index: 'not_analyzed'}` by `{type: 'keyword'}`
50
+ * replace fields with `{ type: 'string', index: 'analyzed'}` by `{type: 'text'}`
51
+ * `PathHierarchy` tokenizer' param `delimiter` now accepts only one argument, [others should be replaced by character filter ](https://discuss.elastic.co/t/multichar-delimiter-in-path-hierarchy-tokenizer/16203)
52
+ * Make sure you don't use any other of the [deprecated Elasticsearch 5 features](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/breaking-changes-6.0.html)
53
+ * Run your test suite on Chewy 6 / Elasticsearch 6
54
+ * Run manual tests on Chewy 6 / Elasticsearch 6
55
+ * Upgrade to Chewy 6
56
+ * Perform a [rolling upgrade](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/rolling-upgrades.html) of Elasticsearch
@@ -6,11 +6,9 @@ describe Chewy::Config do
6
6
  its(:logger) { should be_nil }
7
7
  its(:transport_logger) { should be_nil }
8
8
  its(:transport_logger) { should be_nil }
9
- its(:query_mode) { should == :must }
10
- its(:filter_mode) { should == :and }
11
- its(:post_filter_mode) { should be_nil }
12
9
  its(:root_strategy) { should == :base }
13
10
  its(:request_strategy) { should == :atomic }
11
+ its(:console_strategy) { should == :urgent }
14
12
  its(:use_after_commit_callbacks) { should == true }
15
13
  its(:indices_path) { should == 'app/chewy' }
16
14
  its(:reset_disable_refresh_interval) { should == false }
@@ -54,80 +52,23 @@ describe Chewy::Config do
54
52
  end
55
53
  end
56
54
 
57
- describe '#search_class=' do
58
- specify do
59
- expect { subject.search_class = Chewy::Query }
60
- .to change { subject.search_class }
61
- .from(be < Chewy::Search::Request)
62
- .to(be < Chewy::Query)
63
- end
64
-
65
- context do
66
- before { hide_const('Kaminari') }
67
-
68
- specify do
69
- expect(subject.search_class.included_modules)
70
- .to include(Chewy::Search::Pagination::WillPaginate)
71
- end
72
- end
73
- end
74
-
75
55
  describe '#search_class' do
76
56
  context 'nothing is defined' do
77
57
  before do
78
58
  hide_const('Kaminari')
79
- hide_const('WillPaginate')
80
59
  end
81
60
 
82
61
  specify do
83
62
  expect(subject.search_class.included_modules)
84
63
  .not_to include(Chewy::Search::Pagination::Kaminari)
85
64
  end
86
-
87
- specify do
88
- expect(subject.search_class.included_modules)
89
- .not_to include(Chewy::Search::Pagination::WillPaginate)
90
- end
91
65
  end
92
66
 
93
67
  context 'kaminari' do
94
- before { hide_const('WillPaginate') }
95
-
96
- specify do
97
- expect(subject.search_class.included_modules)
98
- .to include(Chewy::Search::Pagination::Kaminari)
99
- end
100
-
101
- specify do
102
- expect(subject.search_class.included_modules)
103
- .not_to include(Chewy::Search::Pagination::WillPaginate)
104
- end
105
- end
106
-
107
- context 'will_paginate' do
108
- before { hide_const('Kaminari') }
109
-
110
- specify do
111
- expect(subject.search_class.included_modules)
112
- .not_to include(Chewy::Search::Pagination::Kaminari)
113
- end
114
-
115
- specify do
116
- expect(subject.search_class.included_modules)
117
- .to include(Chewy::Search::Pagination::WillPaginate)
118
- end
119
- end
120
-
121
- context 'both are defined' do
122
68
  specify do
123
69
  expect(subject.search_class.included_modules)
124
70
  .to include(Chewy::Search::Pagination::Kaminari)
125
71
  end
126
-
127
- specify do
128
- expect(subject.search_class.included_modules)
129
- .not_to include(Chewy::Search::Pagination::WillPaginate)
130
- end
131
72
  end
132
73
  end
133
74
 
@@ -141,7 +82,7 @@ describe Chewy::Config do
141
82
  context 'when Rails::VERSION constant is defined' do
142
83
  it 'looks for configuration in "config/chewy.yml"' do
143
84
  module Rails
144
- VERSION = '5.1.0'.freeze
85
+ VERSION = '5.1.1'.freeze
145
86
 
146
87
  def self.root
147
88
  Pathname.new(__dir__)
@@ -154,4 +95,17 @@ describe Chewy::Config do
154
95
  end
155
96
  end
156
97
  end
98
+
99
+ describe '.console_strategy' do
100
+ context 'sets .console_strategy' do
101
+ let(:default_strategy) { subject.console_strategy }
102
+ let(:new_strategy) { :atomic }
103
+ after { subject.console_strategy = default_strategy }
104
+
105
+ specify do
106
+ expect { subject.console_strategy = new_strategy }
107
+ .to change { subject.console_strategy }.to(new_strategy)
108
+ end
109
+ end
110
+ end
157
111
  end