chewy 0.9.0 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +214 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +41 -19
  5. data/.rubocop_todo.yml +2 -2
  6. data/.yardopts +5 -0
  7. data/Appraisals +58 -28
  8. data/CHANGELOG.md +153 -12
  9. data/Gemfile +20 -12
  10. data/LEGACY_DSL.md +497 -0
  11. data/LICENSE.txt +1 -1
  12. data/README.md +338 -528
  13. data/chewy.gemspec +11 -12
  14. data/gemfiles/rails.5.2.activerecord.gemfile +17 -0
  15. data/gemfiles/rails.5.2.mongoid.6.4.gemfile +17 -0
  16. data/gemfiles/rails.6.0.activerecord.gemfile +17 -0
  17. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  18. data/gemfiles/ruby3.gemfile +10 -0
  19. data/gemfiles/sequel.4.45.gemfile +11 -0
  20. data/lib/chewy.rb +79 -44
  21. data/lib/chewy/backports/duplicable.rb +1 -1
  22. data/lib/chewy/config.rb +43 -17
  23. data/lib/chewy/errors.rb +2 -2
  24. data/lib/chewy/fields/base.rb +56 -31
  25. data/lib/chewy/fields/root.rb +44 -11
  26. data/lib/chewy/index.rb +237 -149
  27. data/lib/chewy/index/actions.rb +100 -35
  28. data/lib/chewy/index/aliases.rb +2 -1
  29. data/lib/chewy/index/settings.rb +11 -5
  30. data/lib/chewy/index/specification.rb +60 -0
  31. data/lib/chewy/journal.rb +40 -92
  32. data/lib/chewy/minitest/helpers.rb +6 -6
  33. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  34. data/lib/chewy/query.rb +182 -122
  35. data/lib/chewy/query/compose.rb +13 -13
  36. data/lib/chewy/query/criteria.rb +13 -13
  37. data/lib/chewy/query/filters.rb +21 -4
  38. data/lib/chewy/query/loading.rb +1 -2
  39. data/lib/chewy/query/nodes/and.rb +2 -2
  40. data/lib/chewy/query/nodes/bool.rb +1 -1
  41. data/lib/chewy/query/nodes/equal.rb +2 -2
  42. data/lib/chewy/query/nodes/exists.rb +1 -1
  43. data/lib/chewy/query/nodes/field.rb +1 -1
  44. data/lib/chewy/query/nodes/has_relation.rb +2 -2
  45. data/lib/chewy/query/nodes/match_all.rb +1 -1
  46. data/lib/chewy/query/nodes/missing.rb +1 -1
  47. data/lib/chewy/query/nodes/not.rb +2 -2
  48. data/lib/chewy/query/nodes/or.rb +2 -2
  49. data/lib/chewy/query/nodes/prefix.rb +1 -1
  50. data/lib/chewy/query/nodes/query.rb +2 -2
  51. data/lib/chewy/query/nodes/range.rb +4 -4
  52. data/lib/chewy/query/nodes/regexp.rb +4 -4
  53. data/lib/chewy/query/nodes/script.rb +3 -3
  54. data/lib/chewy/query/pagination.rb +10 -1
  55. data/lib/chewy/railtie.rb +4 -3
  56. data/lib/chewy/rake_helper.rb +265 -48
  57. data/lib/chewy/rspec/update_index.rb +33 -27
  58. data/lib/chewy/search.rb +79 -26
  59. data/lib/chewy/search/loader.rb +83 -0
  60. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  61. data/lib/chewy/search/pagination/will_paginate.rb +43 -0
  62. data/lib/chewy/search/parameters.rb +168 -0
  63. data/lib/chewy/search/parameters/aggs.rb +16 -0
  64. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  65. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  66. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  67. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  68. data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
  69. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  70. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  71. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  72. data/lib/chewy/search/parameters/explain.rb +16 -0
  73. data/lib/chewy/search/parameters/filter.rb +47 -0
  74. data/lib/chewy/search/parameters/highlight.rb +16 -0
  75. data/lib/chewy/search/parameters/indices.rb +123 -0
  76. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  77. data/lib/chewy/search/parameters/limit.rb +17 -0
  78. data/lib/chewy/search/parameters/load.rb +32 -0
  79. data/lib/chewy/search/parameters/min_score.rb +16 -0
  80. data/lib/chewy/search/parameters/none.rb +27 -0
  81. data/lib/chewy/search/parameters/offset.rb +17 -0
  82. data/lib/chewy/search/parameters/order.rb +64 -0
  83. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  84. data/lib/chewy/search/parameters/preference.rb +16 -0
  85. data/lib/chewy/search/parameters/profile.rb +16 -0
  86. data/lib/chewy/search/parameters/query.rb +19 -0
  87. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  88. data/lib/chewy/search/parameters/rescore.rb +29 -0
  89. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  90. data/lib/chewy/search/parameters/search_after.rb +20 -0
  91. data/lib/chewy/search/parameters/search_type.rb +16 -0
  92. data/lib/chewy/search/parameters/source.rb +73 -0
  93. data/lib/chewy/search/parameters/storage.rb +95 -0
  94. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  95. data/lib/chewy/search/parameters/suggest.rb +16 -0
  96. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  97. data/lib/chewy/search/parameters/timeout.rb +16 -0
  98. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  99. data/lib/chewy/search/parameters/types.rb +20 -0
  100. data/lib/chewy/search/parameters/version.rb +16 -0
  101. data/lib/chewy/search/query_proxy.rb +257 -0
  102. data/lib/chewy/search/request.rb +1046 -0
  103. data/lib/chewy/search/response.rb +119 -0
  104. data/lib/chewy/search/scoping.rb +50 -0
  105. data/lib/chewy/search/scrolling.rb +134 -0
  106. data/lib/chewy/stash.rb +79 -0
  107. data/lib/chewy/strategy.rb +10 -3
  108. data/lib/chewy/strategy/active_job.rb +2 -1
  109. data/lib/chewy/strategy/atomic.rb +2 -4
  110. data/lib/chewy/strategy/bypass.rb +1 -1
  111. data/lib/chewy/strategy/resque.rb +1 -0
  112. data/lib/chewy/strategy/shoryuken.rb +40 -0
  113. data/lib/chewy/strategy/sidekiq.rb +13 -3
  114. data/lib/chewy/type.rb +29 -7
  115. data/lib/chewy/type/actions.rb +26 -2
  116. data/lib/chewy/type/adapter/active_record.rb +44 -29
  117. data/lib/chewy/type/adapter/base.rb +27 -7
  118. data/lib/chewy/type/adapter/mongoid.rb +19 -10
  119. data/lib/chewy/type/adapter/object.rb +187 -26
  120. data/lib/chewy/type/adapter/orm.rb +59 -32
  121. data/lib/chewy/type/adapter/sequel.rb +33 -19
  122. data/lib/chewy/type/crutch.rb +1 -1
  123. data/lib/chewy/type/import.rb +146 -191
  124. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  125. data/lib/chewy/type/import/bulk_request.rb +78 -0
  126. data/lib/chewy/type/import/journal_builder.rb +45 -0
  127. data/lib/chewy/type/import/routine.rb +138 -0
  128. data/lib/chewy/type/mapping.rb +51 -35
  129. data/lib/chewy/type/observe.rb +17 -13
  130. data/lib/chewy/type/syncer.rb +222 -0
  131. data/lib/chewy/type/witchcraft.rb +32 -16
  132. data/lib/chewy/type/wrapper.rb +30 -4
  133. data/lib/chewy/version.rb +1 -1
  134. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  135. data/lib/tasks/chewy.rake +84 -26
  136. data/spec/chewy/config_spec.rb +98 -1
  137. data/spec/chewy/fields/base_spec.rb +170 -135
  138. data/spec/chewy/fields/root_spec.rb +124 -20
  139. data/spec/chewy/fields/time_fields_spec.rb +2 -3
  140. data/spec/chewy/index/actions_spec.rb +214 -52
  141. data/spec/chewy/index/aliases_spec.rb +2 -2
  142. data/spec/chewy/index/settings_spec.rb +67 -38
  143. data/spec/chewy/index/specification_spec.rb +169 -0
  144. data/spec/chewy/index_spec.rb +108 -64
  145. data/spec/chewy/journal_spec.rb +150 -55
  146. data/spec/chewy/minitest/helpers_spec.rb +4 -4
  147. data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
  148. data/spec/chewy/query/criteria_spec.rb +179 -179
  149. data/spec/chewy/query/filters_spec.rb +16 -16
  150. data/spec/chewy/query/loading_spec.rb +22 -20
  151. data/spec/chewy/query/nodes/and_spec.rb +2 -2
  152. data/spec/chewy/query/nodes/bool_spec.rb +4 -4
  153. data/spec/chewy/query/nodes/equal_spec.rb +19 -19
  154. data/spec/chewy/query/nodes/exists_spec.rb +6 -6
  155. data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
  156. data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
  157. data/spec/chewy/query/nodes/missing_spec.rb +5 -5
  158. data/spec/chewy/query/nodes/not_spec.rb +4 -2
  159. data/spec/chewy/query/nodes/or_spec.rb +2 -2
  160. data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
  161. data/spec/chewy/query/nodes/query_spec.rb +2 -2
  162. data/spec/chewy/query/nodes/range_spec.rb +18 -18
  163. data/spec/chewy/query/nodes/raw_spec.rb +1 -1
  164. data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
  165. data/spec/chewy/query/nodes/script_spec.rb +4 -4
  166. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  167. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  168. data/spec/chewy/query/pagination_spec.rb +25 -21
  169. data/spec/chewy/query_spec.rb +503 -561
  170. data/spec/chewy/rake_helper_spec.rb +381 -0
  171. data/spec/chewy/repository_spec.rb +4 -4
  172. data/spec/chewy/rspec/update_index_spec.rb +89 -56
  173. data/spec/chewy/runtime_spec.rb +2 -2
  174. data/spec/chewy/search/loader_spec.rb +117 -0
  175. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  176. data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
  177. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  178. data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
  179. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  180. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  181. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  182. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  183. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  184. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  185. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  186. data/spec/chewy/search/parameters/indices_spec.rb +191 -0
  187. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  188. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  189. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  190. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  191. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  192. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  193. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  194. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  195. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  196. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  197. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  198. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  199. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  200. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  201. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  202. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  203. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  204. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  205. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  206. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  207. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  208. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  209. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  210. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  211. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  212. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  213. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  214. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  215. data/spec/chewy/search/parameters_spec.rb +147 -0
  216. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  217. data/spec/chewy/search/request_spec.rb +685 -0
  218. data/spec/chewy/search/response_spec.rb +198 -0
  219. data/spec/chewy/search/scrolling_spec.rb +169 -0
  220. data/spec/chewy/search_spec.rb +33 -16
  221. data/spec/chewy/stash_spec.rb +95 -0
  222. data/spec/chewy/strategy/active_job_spec.rb +21 -2
  223. data/spec/chewy/strategy/resque_spec.rb +6 -0
  224. data/spec/chewy/strategy/shoryuken_spec.rb +70 -0
  225. data/spec/chewy/strategy/sidekiq_spec.rb +13 -1
  226. data/spec/chewy/strategy_spec.rb +6 -6
  227. data/spec/chewy/type/actions_spec.rb +29 -10
  228. data/spec/chewy/type/adapter/active_record_spec.rb +203 -91
  229. data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
  230. data/spec/chewy/type/adapter/object_spec.rb +101 -28
  231. data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
  232. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  233. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  234. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  235. data/spec/chewy/type/import/routine_spec.rb +110 -0
  236. data/spec/chewy/type/import_spec.rb +356 -271
  237. data/spec/chewy/type/mapping_spec.rb +96 -29
  238. data/spec/chewy/type/observe_spec.rb +9 -5
  239. data/spec/chewy/type/syncer_spec.rb +123 -0
  240. data/spec/chewy/type/witchcraft_spec.rb +61 -29
  241. data/spec/chewy/type/wrapper_spec.rb +63 -23
  242. data/spec/chewy/type_spec.rb +28 -7
  243. data/spec/chewy_spec.rb +75 -7
  244. data/spec/spec_helper.rb +17 -3
  245. data/spec/support/active_record.rb +5 -1
  246. data/spec/support/class_helpers.rb +0 -14
  247. data/spec/support/mongoid.rb +15 -3
  248. data/spec/support/sequel.rb +6 -1
  249. metadata +219 -58
  250. data/.travis.yml +0 -36
  251. data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
  252. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
  253. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
  254. data/gemfiles/rails.4.2.activerecord.gemfile +0 -17
  255. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
  256. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
  257. data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
  258. data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
  259. data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
  260. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -16
  261. data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
  262. data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
  263. data/gemfiles/rails.5.0.activerecord.gemfile +0 -17
  264. data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
  265. data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
  266. data/gemfiles/sequel.4.38.gemfile +0 -14
  267. data/lib/chewy/journal/apply.rb +0 -31
  268. data/lib/chewy/journal/clean.rb +0 -24
  269. data/lib/chewy/journal/entry.rb +0 -83
  270. data/lib/chewy/journal/query.rb +0 -87
  271. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  272. data/lib/chewy/query/scoping.rb +0 -20
  273. data/spec/chewy/journal/apply_spec.rb +0 -120
  274. data/spec/chewy/journal/entry_spec.rb +0 -237
  275. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
@@ -30,8 +30,8 @@ module Chewy
30
30
  # Suffixed index names might be used for zero-downtime mapping change, for example.
31
31
  # Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
32
32
  #
33
- def create(*args)
34
- create!(*args)
33
+ def create(*args, **kwargs)
34
+ create!(*args, **kwargs)
35
35
  rescue Elasticsearch::Transport::Transport::Errors::BadRequest
36
36
  false
37
37
  end
@@ -52,18 +52,14 @@ module Chewy
52
52
  # Suffixed index names might be used for zero-downtime mapping change, for example.
53
53
  # Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
54
54
  #
55
- def create!(*args)
56
- options = args.extract_options!.reverse_merge!(alias: true)
57
- name = build_index_name(suffix: args.first)
55
+ def create!(suffix = nil, **options)
56
+ options.reverse_merge!(alias: true)
57
+ general_name = index_name
58
+ suffixed_name = index_name(suffix: suffix)
58
59
 
59
- if Chewy::Runtime.version >= 1.1
60
- body = index_params
61
- body[:aliases] = { index_name => {} } if options[:alias] && name != index_name
62
- result = client.indices.create(index: name, body: body)
63
- else
64
- result = client.indices.create(index: name, body: index_params)
65
- result &&= client.indices.put_alias(index: name, name: index_name) if options[:alias] && name != index_name
66
- end
60
+ body = specification_hash
61
+ body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
62
+ result = client.indices.create(index: suffixed_name, body: body)
67
63
 
68
64
  Chewy.wait_for_status if result
69
65
  result
@@ -78,7 +74,13 @@ module Chewy
78
74
  # UsersIndex.delete '01-2014' # deletes `users_01-2014` index
79
75
  #
80
76
  def delete(suffix = nil)
81
- result = client.indices.delete index: build_index_name(suffix: suffix)
77
+ # Verify that the index_name is really the index_name and not an alias.
78
+ #
79
+ # "The index parameter in the delete index API no longer accepts alias names.
80
+ # Instead, it accepts only index names (or wildcards which will expand to matching indices)."
81
+ # https://www.elastic.co/guide/en/elasticsearch/reference/6.8/breaking-changes-6.0.html#_delete_index_api_resolves_indices_expressions_only_against_indices
82
+ index_names = client.indices.get_alias(index: index_name(suffix: suffix)).keys
83
+ result = client.indices.delete index: index_names.join(',')
82
84
  Chewy.wait_for_status if result
83
85
  result
84
86
  # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
@@ -137,10 +139,17 @@ module Chewy
137
139
  #
138
140
  # See [import.rb](lib/chewy/type/import.rb) for more details.
139
141
  #
140
- [:import, :import!].each do |method|
142
+ %i[import import!].each do |method|
141
143
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
142
- def #{method} options = {}
143
- objects = options.reject { |k, v| !type_names.map(&:to_sym).include?(k) }
144
+ def #{method}(*args)
145
+ options = args.extract_options!
146
+ if args.one? && type_names.one?
147
+ objects = {type_names.first.to_sym => args.first}
148
+ elsif args.one?
149
+ fail ArgumentError, "Please pass objects for `#{method}` as a hash with type names"
150
+ else
151
+ objects = options.reject { |k, v| !type_names.map(&:to_sym).include?(k) }
152
+ end
144
153
  types.map do |type|
145
154
  args = [objects[type.type_name.to_sym], options.dup].reject(&:blank?)
146
155
  type.#{method} *args
@@ -149,33 +158,89 @@ module Chewy
149
158
  METHOD
150
159
  end
151
160
 
152
- # Deletes, creates and imports data to the index.
153
- # Returns import result
154
- #
155
- # UsersIndex.reset!
161
+ # Deletes, creates and imports data to the index. Returns the
162
+ # import result. If index name suffix is passed as the first
163
+ # argument - performs zero-downtime index resetting.
156
164
  #
157
- # If index name suffix passed as the first argument - performs
158
- # zero-downtime index resetting (described here:
159
- # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
165
+ # It also applies journal if anything was journaled during the
166
+ # reset.
160
167
  #
161
- # UsersIndex.reset! Time.now.to_i, journal: true
162
- #
163
- def reset!(suffix = nil, journal: false)
164
- if suffix.present? && (indexes = self.indexes).present?
168
+ # @example
169
+ # UsersIndex.reset!
170
+ # UsersIndex.reset! Time.now.to_i
171
+ #
172
+ # @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime
173
+ # @param suffix [String] a suffix for the newly created index
174
+ # @param apply_journal [true, false] if true, journal is applied after the import is completed
175
+ # @param journal [true, false] journaling is switched off for import during reset by default
176
+ # @param import_options [Hash] options, passed to the import call
177
+ # @return [true, false] false in case of errors
178
+ def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
179
+ result = if suffix.present?
180
+ start_time = Time.now
181
+ indexes = self.indexes
165
182
  create! suffix, alias: false
166
- result = import suffix: suffix, journal: journal
167
- client.indices.update_aliases body: { actions: [
183
+
184
+ general_name = index_name
185
+ suffixed_name = index_name(suffix: suffix)
186
+
187
+ optimize_index_settings suffixed_name
188
+ result = import import_options.merge(suffix: suffix, journal: journal, refresh: !Chewy.reset_disable_refresh_interval)
189
+ original_index_settings suffixed_name
190
+
191
+ delete if indexes.blank?
192
+ client.indices.update_aliases body: {actions: [
168
193
  *indexes.map do |index|
169
- { remove: { index: index, alias: index_name } }
194
+ {remove: {index: index, alias: general_name}}
170
195
  end,
171
- { add: { index: build_index_name(suffix: suffix), alias: index_name } }
172
- ] }
196
+ {add: {index: suffixed_name, alias: general_name}}
197
+ ]}
173
198
  client.indices.delete index: indexes if indexes.present?
199
+
200
+ self.journal.apply(start_time, **import_options) if apply_journal
174
201
  result
175
202
  else
176
- purge! suffix
177
- import journal: journal
203
+ purge!
204
+ import import_options.merge(journal: journal)
178
205
  end
206
+
207
+ specification.lock!
208
+ result
209
+ end
210
+
211
+ # A {Chewy::Journal} instance for the particular index
212
+ #
213
+ # @return [Chewy::Journal] journal instance
214
+ def journal
215
+ @journal ||= Chewy::Journal.new(self)
216
+ end
217
+
218
+ private
219
+
220
+ def optimize_index_settings(index_name)
221
+ settings = {}
222
+ settings[:refresh_interval] = -1 if Chewy.reset_disable_refresh_interval
223
+ settings[:number_of_replicas] = 0 if Chewy.reset_no_replicas
224
+ update_settings index_name, settings: settings if settings.any?
225
+ end
226
+
227
+ def original_index_settings(index_name)
228
+ settings = {}
229
+ if Chewy.reset_disable_refresh_interval
230
+ settings.merge! index_settings(:refresh_interval)
231
+ settings[:refresh_interval] = '1s' if settings.empty?
232
+ end
233
+ settings.merge! index_settings(:number_of_replicas) if Chewy.reset_no_replicas
234
+ update_settings index_name, settings: settings if settings.any?
235
+ end
236
+
237
+ def update_settings(index_name, **options)
238
+ client.indices.put_settings index: index_name, body: {index: options[:settings]}
239
+ end
240
+
241
+ def index_settings(setting_name)
242
+ return {} unless settings_hash.key?(:settings) && settings_hash[:settings].key?(:index)
243
+ settings_hash[:settings][:index].slice(setting_name)
179
244
  end
180
245
  end
181
246
  end
@@ -11,7 +11,8 @@ module Chewy
11
11
  end
12
12
 
13
13
  def aliases
14
- client.indices.get_alias(index: index_name, name: '*')[index_name].try(:[], 'aliases').try(:keys) || []
14
+ name = index_name
15
+ client.indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || []
15
16
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
16
17
  []
17
18
  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: [
@@ -23,20 +25,23 @@ module Chewy
23
25
  # might be used as well.
24
26
  #
25
27
  class Settings
26
- def initialize(params = {})
28
+ def initialize(params = {}, &block)
27
29
  @params = params
30
+ @proc_params = block
28
31
  end
29
32
 
30
33
  def to_hash
31
34
  settings = @params.deep_symbolize_keys
35
+ settings.merge!((@proc_params.call || {}).deep_symbolize_keys) if @proc_params
32
36
 
33
37
  settings[:analysis] = resolve_analysis(settings[:analysis]) if settings[:analysis]
38
+
34
39
  if settings[:index] || Chewy.configuration[:index]
35
40
  settings[:index] = (Chewy.configuration[:index] || {})
36
41
  .deep_merge((settings[:index] || {}).deep_symbolize_keys)
37
42
  end
38
43
 
39
- settings.present? ? { settings: settings } : {}
44
+ settings.present? ? {settings: settings} : {}
40
45
  end
41
46
 
42
47
  private
@@ -44,14 +49,15 @@ module Chewy
44
49
  def resolve_analysis(analysis)
45
50
  analyzer = resolve(analysis[:analyzer], Chewy.analyzers)
46
51
 
47
- options = [:tokenizer, :filter, :char_filter].each.with_object({}) do |type, result|
52
+ options = %i[tokenizer filter char_filter].each.with_object({}) do |type, result|
48
53
  dependencies = collect_dependencies(type, analyzer)
49
54
  resolved = resolve(dependencies.push(analysis[type]), Chewy.send(type.to_s.pluralize))
50
55
  result.merge!(type => resolved) if resolved.present?
51
56
  end
52
57
 
53
58
  options[:analyzer] = analyzer if analyzer.present?
54
- options
59
+ analysis = analysis.except(:analyzer, :tokenizer, :filter, :char_filter)
60
+ analysis.merge(options)
55
61
  end
56
62
 
57
63
  def collect_dependencies(type, analyzer)
@@ -66,7 +72,7 @@ module Chewy
66
72
  else
67
73
  name_or_hash = name_or_hash.to_sym
68
74
  resolved = repository[name_or_hash]
69
- resolved ? { name_or_hash => resolved } : {}
75
+ resolved ? {name_or_hash => resolved} : {}
70
76
  end
71
77
  result.merge!(options)
72
78
  end
@@ -0,0 +1,60 @@
1
+ module Chewy
2
+ class Index
3
+ # Index specification is a combination of index settings and
4
+ # mappings. The idea behind this class is that specification
5
+ # can be locked in the `Chewy::Stash::Specification` between
6
+ # resets, so it is possible to track changes. In the future
7
+ # it is planned to be way smarter but right now `rake chewy:deploy`
8
+ # checks if there were changes and resets the index only if
9
+ # anything was changed. Otherwise, the index reset is skipped.
10
+ #
11
+ # @see Chewy::Stash::Specification
12
+ class Specification
13
+ # @see Chewy::Index::Specification
14
+ # @param index [Chewy::Index] Just a chewy index
15
+ def initialize(index)
16
+ @index = index
17
+ end
18
+
19
+ # Stores the current index specification to the `Chewy::Stash::Specification`
20
+ # as json.
21
+ #
22
+ # @raise [Chewy::ImportFailed] if something went wrong
23
+ # @return [true] if everything is fine
24
+ def lock!
25
+ Chewy::Stash::Specification.import!([
26
+ id: @index.derivable_name,
27
+ specification: Base64.encode64(current.to_json)
28
+ ], journal: false)
29
+ end
30
+
31
+ # Returns the last locked specification as ruby hash. Returns
32
+ # empty hash if nothing is stored yet.
33
+ #
34
+ # @return [Hash] hash produced with JSON parser
35
+ def locked
36
+ filter = {ids: {values: [@index.derivable_name]}}
37
+ document = Chewy::Stash::Specification.filter(filter).first
38
+ return {} unless document
39
+ JSON.load(Base64.decode64(document.specification)) # rubocop:disable Security/JSONLoad
40
+ end
41
+
42
+ # Simply returns `Chewy::Index.specification_hash`, but
43
+ # prepared for JSON with `as_json` method. This means all the
44
+ # keys are strings and there are only values of types handled in JSON.
45
+ #
46
+ # @see Chewy::Index.specification_hash
47
+ # @return [Hash] a JSON-ready hash
48
+ def current
49
+ @index.specification_hash.as_json
50
+ end
51
+
52
+ # Compares previously locked and current specifications.
53
+ #
54
+ # @return [true, false] the result of comparison
55
+ def changed?
56
+ current != locked
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,107 +1,55 @@
1
- require 'chewy/journal/entry'
2
- require 'chewy/journal/query'
3
- require 'chewy/journal/apply'
4
- require 'chewy/journal/clean'
5
-
6
1
  module Chewy
2
+ # A class to perform journal-related actions for the specified indexes/types.
3
+ #
4
+ # @example
5
+ # journal = Chewy::Journal.new('places#city', UsersIndex)
6
+ # journal.apply(20.minutes.ago)
7
+ # journal.clean
8
+ #
7
9
  class Journal
8
- JOURNAL_MAPPING = {
9
- journal: {
10
- properties: {
11
- index_name: { type: 'string', index: 'not_analyzed' },
12
- type_name: { type: 'string', index: 'not_analyzed' },
13
- action: { type: 'string', index: 'not_analyzed' },
14
- object_ids: { type: 'string', index: 'not_analyzed' },
15
- created_at: { type: 'date' }
16
- }
17
- }
18
- }.freeze
19
-
20
- def initialize(index)
21
- @records = []
22
- @index = index
10
+ # @param only [Array<String, Chewy::Index, Chewy::Type>] indexes/types or even string references to perform actions on
11
+ def initialize(*only)
12
+ @only = only
23
13
  end
24
14
 
25
- def add(action_objects)
26
- @records +=
27
- action_objects.map do |action, objects|
28
- {
29
- index_name: @index.derivable_index_name,
30
- type_name: @index.type_name,
31
- action: action,
32
- object_ids: identify(objects),
33
- created_at: Time.now.to_i
34
- }
15
+ # Applies all changes that were done since the specified time to the
16
+ # specified indexes/types.
17
+ #
18
+ # @param since_time [Time, DateTime] timestamp from which changes will be applied
19
+ # @param retries [Integer] maximum number of attempts to make journal empty, 10 by default
20
+ # @return [Integer] the amount of journal entries found
21
+ def apply(since_time, retries: 10, **import_options)
22
+ stage = 1
23
+ since_time -= 1
24
+ count = 0
25
+ while stage <= retries
26
+ entries = Chewy::Stash::Journal.entries(since_time, only: @only).to_a.presence or break
27
+ count += entries.size
28
+ groups = reference_groups(entries)
29
+ ActiveSupport::Notifications.instrument 'apply_journal.chewy', stage: stage, groups: groups
30
+ groups.each do |type, references|
31
+ type.import(references, import_options.merge(journal: false))
35
32
  end
36
- end
37
-
38
- def bulk_body
39
- @records.map do |record|
40
- {
41
- create: {
42
- _index: self.class.index_name,
43
- _type: self.class.type_name,
44
- data: record
45
- }
46
- }
33
+ stage += 1
34
+ since_time = entries.map(&:created_at).max
47
35
  end
36
+ count
48
37
  end
49
38
 
50
- def any_records?
51
- @records.any?
39
+ # Cleans journal for the specified indexes/types.
40
+ #
41
+ # @param until_time [Time, DateTime] time to clean up until it
42
+ # @return [Hash] delete_by_query ES API call result
43
+ def clean(until_time = nil)
44
+ Chewy::Stash::Journal.clean(until_time, only: @only)
52
45
  end
53
46
 
54
47
  private
55
48
 
56
- def identify(objects)
57
- @index.adapter.identify(objects)
58
- end
59
-
60
- class << self
61
- def exists?
62
- Chewy.client.indices.exists? index: index_name
63
- end
64
-
65
- def index_name
66
- [
67
- Chewy.configuration[:prefix],
68
- Chewy.configuration[:journal_name] || 'chewy_journal'
69
- ].reject(&:blank?).join('_')
70
- end
71
-
72
- def type_name
73
- JOURNAL_MAPPING.keys.first
74
- end
75
-
76
- def create
77
- return if exists?
78
- Chewy.client.indices.create index: index_name, body: { settings: { index: Chewy.configuration[:index] }, mappings: JOURNAL_MAPPING }
79
- Chewy.wait_for_status
80
- end
81
-
82
- def delete!
83
- delete or raise Elasticsearch::Transport::Transport::Errors::NotFound
84
- end
85
-
86
- def delete
87
- result = Chewy.client.indices.delete index: index_name
88
- Chewy.wait_for_status if result
89
- result
90
- rescue Elasticsearch::Transport::Transport::Errors::NotFound
91
- false
92
- end
93
-
94
- def apply_changes_from(*args)
95
- Apply.since(*args)
96
- end
97
-
98
- def entries_from(*args)
99
- Entry.since(*args)
100
- end
101
-
102
- def clean_until(*args)
103
- Clean.until(*args)
104
- end
49
+ def reference_groups(entries)
50
+ entries.group_by(&:type).map do |type, grouped_entries|
51
+ [type, grouped_entries.map(&:references).inject(:|)]
52
+ end.to_h
105
53
  end
106
54
  end
107
55
  end