chewy 0.8.4 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +56 -0
  4. data/.rubocop_todo.yml +44 -0
  5. data/.travis.yml +36 -67
  6. data/.yardopts +5 -0
  7. data/Appraisals +63 -58
  8. data/CHANGELOG.md +168 -11
  9. data/Gemfile +16 -9
  10. data/Guardfile +5 -5
  11. data/LEGACY_DSL.md +497 -0
  12. data/README.md +403 -470
  13. data/Rakefile +11 -1
  14. data/chewy.gemspec +12 -15
  15. data/gemfiles/rails.4.0.activerecord.gemfile +9 -9
  16. data/gemfiles/rails.4.1.activerecord.gemfile +9 -9
  17. data/gemfiles/rails.4.2.activerecord.gemfile +8 -8
  18. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +16 -0
  19. data/gemfiles/rails.5.0.activerecord.gemfile +16 -0
  20. data/gemfiles/rails.5.0.mongoid.6.1.gemfile +16 -0
  21. data/gemfiles/rails.5.1.activerecord.gemfile +16 -0
  22. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +16 -0
  23. data/gemfiles/rails.5.2.activerecord.gemfile +16 -0
  24. data/gemfiles/sequel.4.45.gemfile +11 -0
  25. data/lib/chewy/backports/deep_dup.rb +1 -1
  26. data/lib/chewy/backports/duplicable.rb +1 -0
  27. data/lib/chewy/config.rb +53 -21
  28. data/lib/chewy/errors.rb +6 -6
  29. data/lib/chewy/fields/base.rb +59 -29
  30. data/lib/chewy/fields/root.rb +49 -14
  31. data/lib/chewy/index/actions.rb +95 -36
  32. data/lib/chewy/index/aliases.rb +2 -1
  33. data/lib/chewy/index/settings.rb +10 -5
  34. data/lib/chewy/index/specification.rb +60 -0
  35. data/lib/chewy/index.rb +239 -138
  36. data/lib/chewy/journal.rb +55 -0
  37. data/lib/chewy/log_subscriber.rb +8 -8
  38. data/lib/chewy/minitest/helpers.rb +77 -0
  39. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  40. data/lib/chewy/minitest.rb +1 -0
  41. data/lib/chewy/query/compose.rb +18 -19
  42. data/lib/chewy/query/criteria.rb +34 -24
  43. data/lib/chewy/query/filters.rb +28 -11
  44. data/lib/chewy/query/loading.rb +3 -4
  45. data/lib/chewy/query/nodes/and.rb +1 -1
  46. data/lib/chewy/query/nodes/base.rb +1 -1
  47. data/lib/chewy/query/nodes/bool.rb +6 -4
  48. data/lib/chewy/query/nodes/equal.rb +4 -4
  49. data/lib/chewy/query/nodes/exists.rb +1 -1
  50. data/lib/chewy/query/nodes/expr.rb +2 -2
  51. data/lib/chewy/query/nodes/field.rb +35 -31
  52. data/lib/chewy/query/nodes/has_child.rb +1 -0
  53. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  54. data/lib/chewy/query/nodes/has_relation.rb +10 -12
  55. data/lib/chewy/query/nodes/missing.rb +1 -1
  56. data/lib/chewy/query/nodes/not.rb +1 -1
  57. data/lib/chewy/query/nodes/or.rb +1 -1
  58. data/lib/chewy/query/nodes/prefix.rb +3 -2
  59. data/lib/chewy/query/nodes/query.rb +1 -1
  60. data/lib/chewy/query/nodes/range.rb +9 -9
  61. data/lib/chewy/query/nodes/raw.rb +1 -1
  62. data/lib/chewy/query/nodes/regexp.rb +13 -9
  63. data/lib/chewy/query/nodes/script.rb +4 -4
  64. data/lib/chewy/query/pagination.rb +10 -1
  65. data/lib/chewy/query.rb +286 -170
  66. data/lib/chewy/railtie.rb +7 -6
  67. data/lib/chewy/rake_helper.rb +275 -37
  68. data/lib/chewy/repository.rb +2 -2
  69. data/lib/chewy/rspec/update_index.rb +70 -65
  70. data/lib/chewy/rspec.rb +1 -1
  71. data/lib/chewy/runtime/version.rb +4 -4
  72. data/lib/chewy/search/loader.rb +83 -0
  73. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  74. data/lib/chewy/search/pagination/will_paginate.rb +43 -0
  75. data/lib/chewy/search/parameters/aggs.rb +16 -0
  76. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  77. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  78. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  79. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  80. data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
  81. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  82. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  83. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  84. data/lib/chewy/search/parameters/explain.rb +16 -0
  85. data/lib/chewy/search/parameters/filter.rb +47 -0
  86. data/lib/chewy/search/parameters/highlight.rb +16 -0
  87. data/lib/chewy/search/parameters/indices.rb +123 -0
  88. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  89. data/lib/chewy/search/parameters/limit.rb +17 -0
  90. data/lib/chewy/search/parameters/load.rb +32 -0
  91. data/lib/chewy/search/parameters/min_score.rb +16 -0
  92. data/lib/chewy/search/parameters/none.rb +27 -0
  93. data/lib/chewy/search/parameters/offset.rb +17 -0
  94. data/lib/chewy/search/parameters/order.rb +64 -0
  95. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  96. data/lib/chewy/search/parameters/preference.rb +16 -0
  97. data/lib/chewy/search/parameters/profile.rb +16 -0
  98. data/lib/chewy/search/parameters/query.rb +19 -0
  99. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  100. data/lib/chewy/search/parameters/rescore.rb +29 -0
  101. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  102. data/lib/chewy/search/parameters/search_after.rb +20 -0
  103. data/lib/chewy/search/parameters/search_type.rb +16 -0
  104. data/lib/chewy/search/parameters/source.rb +73 -0
  105. data/lib/chewy/search/parameters/storage.rb +95 -0
  106. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  107. data/lib/chewy/search/parameters/suggest.rb +16 -0
  108. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  109. data/lib/chewy/search/parameters/timeout.rb +16 -0
  110. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  111. data/lib/chewy/search/parameters/types.rb +20 -0
  112. data/lib/chewy/search/parameters/version.rb +16 -0
  113. data/lib/chewy/search/parameters.rb +167 -0
  114. data/lib/chewy/search/query_proxy.rb +257 -0
  115. data/lib/chewy/search/request.rb +1045 -0
  116. data/lib/chewy/search/response.rb +119 -0
  117. data/lib/chewy/search/scoping.rb +50 -0
  118. data/lib/chewy/search/scrolling.rb +134 -0
  119. data/lib/chewy/search.rb +81 -26
  120. data/lib/chewy/stash.rb +79 -0
  121. data/lib/chewy/strategy/active_job.rb +1 -0
  122. data/lib/chewy/strategy/atomic.rb +2 -4
  123. data/lib/chewy/strategy/base.rb +4 -4
  124. data/lib/chewy/strategy/bypass.rb +1 -2
  125. data/lib/chewy/strategy/resque.rb +1 -0
  126. data/lib/chewy/strategy/shoryuken.rb +40 -0
  127. data/lib/chewy/strategy/sidekiq.rb +13 -1
  128. data/lib/chewy/strategy/urgent.rb +1 -1
  129. data/lib/chewy/strategy.rb +19 -10
  130. data/lib/chewy/type/actions.rb +26 -2
  131. data/lib/chewy/type/adapter/active_record.rb +50 -24
  132. data/lib/chewy/type/adapter/base.rb +29 -9
  133. data/lib/chewy/type/adapter/mongoid.rb +19 -10
  134. data/lib/chewy/type/adapter/object.rb +195 -31
  135. data/lib/chewy/type/adapter/orm.rb +69 -33
  136. data/lib/chewy/type/adapter/sequel.rb +37 -19
  137. data/lib/chewy/type/crutch.rb +5 -4
  138. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  139. data/lib/chewy/type/import/bulk_request.rb +78 -0
  140. data/lib/chewy/type/import/journal_builder.rb +45 -0
  141. data/lib/chewy/type/import/routine.rb +138 -0
  142. data/lib/chewy/type/import.rb +150 -176
  143. data/lib/chewy/type/mapping.rb +58 -42
  144. data/lib/chewy/type/observe.rb +21 -15
  145. data/lib/chewy/type/syncer.rb +222 -0
  146. data/lib/chewy/type/witchcraft.rb +89 -34
  147. data/lib/chewy/type/wrapper.rb +48 -16
  148. data/lib/chewy/type.rb +77 -49
  149. data/lib/chewy/version.rb +1 -1
  150. data/lib/chewy.rb +95 -52
  151. data/lib/generators/chewy/install_generator.rb +3 -3
  152. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  153. data/lib/tasks/chewy.rake +91 -28
  154. data/spec/chewy/config_spec.rb +130 -12
  155. data/spec/chewy/fields/base_spec.rb +194 -172
  156. data/spec/chewy/fields/root_spec.rb +123 -17
  157. data/spec/chewy/fields/time_fields_spec.rb +10 -9
  158. data/spec/chewy/index/actions_spec.rb +228 -43
  159. data/spec/chewy/index/aliases_spec.rb +2 -2
  160. data/spec/chewy/index/settings_spec.rb +100 -49
  161. data/spec/chewy/index/specification_spec.rb +169 -0
  162. data/spec/chewy/index_spec.rb +159 -63
  163. data/spec/chewy/journal_spec.rb +268 -0
  164. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  165. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  166. data/spec/chewy/query/criteria_spec.rb +503 -236
  167. data/spec/chewy/query/filters_spec.rb +96 -68
  168. data/spec/chewy/query/loading_spec.rb +80 -42
  169. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  170. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  171. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  172. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  173. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  174. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  175. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  176. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  177. data/spec/chewy/query/nodes/not_spec.rb +5 -7
  178. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  179. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  180. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  181. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  182. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  183. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  184. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  185. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  186. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  187. data/spec/chewy/query/pagination_spec.rb +25 -22
  188. data/spec/chewy/query_spec.rb +510 -505
  189. data/spec/chewy/rake_helper_spec.rb +381 -0
  190. data/spec/chewy/repository_spec.rb +8 -8
  191. data/spec/chewy/rspec/update_index_spec.rb +215 -113
  192. data/spec/chewy/runtime_spec.rb +2 -2
  193. data/spec/chewy/search/loader_spec.rb +117 -0
  194. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  195. data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
  196. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  197. data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
  198. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  199. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  200. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  201. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  202. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  203. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  204. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  205. data/spec/chewy/search/parameters/indices_spec.rb +191 -0
  206. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  207. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  208. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  209. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  210. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  211. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  212. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  213. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  214. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  215. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  216. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  217. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  218. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  219. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  220. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  221. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  222. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  223. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  224. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  225. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  226. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  227. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  228. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  229. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  230. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  231. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  232. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  233. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  234. data/spec/chewy/search/parameters_spec.rb +145 -0
  235. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  236. data/spec/chewy/search/request_spec.rb +685 -0
  237. data/spec/chewy/search/response_spec.rb +192 -0
  238. data/spec/chewy/search/scrolling_spec.rb +169 -0
  239. data/spec/chewy/search_spec.rb +37 -20
  240. data/spec/chewy/stash_spec.rb +95 -0
  241. data/spec/chewy/strategy/active_job_spec.rb +8 -2
  242. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  243. data/spec/chewy/strategy/resque_spec.rb +8 -2
  244. data/spec/chewy/strategy/shoryuken_spec.rb +66 -0
  245. data/spec/chewy/strategy/sidekiq_spec.rb +10 -2
  246. data/spec/chewy/strategy_spec.rb +6 -6
  247. data/spec/chewy/type/actions_spec.rb +29 -10
  248. data/spec/chewy/type/adapter/active_record_spec.rb +357 -139
  249. data/spec/chewy/type/adapter/mongoid_spec.rb +220 -101
  250. data/spec/chewy/type/adapter/object_spec.rb +129 -40
  251. data/spec/chewy/type/adapter/sequel_spec.rb +304 -152
  252. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  253. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  254. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  255. data/spec/chewy/type/import/routine_spec.rb +110 -0
  256. data/spec/chewy/type/import_spec.rb +360 -244
  257. data/spec/chewy/type/mapping_spec.rb +96 -29
  258. data/spec/chewy/type/observe_spec.rb +25 -15
  259. data/spec/chewy/type/syncer_spec.rb +123 -0
  260. data/spec/chewy/type/witchcraft_spec.rb +122 -44
  261. data/spec/chewy/type/wrapper_spec.rb +63 -23
  262. data/spec/chewy/type_spec.rb +32 -10
  263. data/spec/chewy_spec.rb +82 -12
  264. data/spec/spec_helper.rb +16 -2
  265. data/spec/support/active_record.rb +6 -2
  266. data/spec/support/class_helpers.rb +4 -19
  267. data/spec/support/mongoid.rb +17 -5
  268. data/spec/support/sequel.rb +6 -1
  269. metadata +250 -57
  270. data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
  271. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
  272. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
  273. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  274. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  275. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  276. data/gemfiles/rails.4.0.mongoid.4.0.0.kaminari.gemfile +0 -14
  277. data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
  278. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  279. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  280. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  281. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  282. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  283. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  284. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  285. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  286. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  287. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  288. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  289. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -15
  290. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
  291. data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
  292. data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
  293. data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
  294. data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
  295. data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
  296. data/gemfiles/rails.4.2.mongoid.5.1.0.will_paginate.gemfile +0 -14
  297. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  298. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  299. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
  300. data/gemfiles/sequel.4.31.gemfile +0 -13
  301. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  302. data/lib/chewy/query/scoping.rb +0 -20
  303. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -60
data/lib/chewy/index.rb CHANGED
@@ -2,6 +2,7 @@ require 'chewy/search'
2
2
  require 'chewy/index/actions'
3
3
  require 'chewy/index/aliases'
4
4
  require 'chewy/index/settings'
5
+ require 'chewy/index/specification'
5
6
 
6
7
  module Chewy
7
8
  class Index
@@ -17,159 +18,259 @@ module Chewy
17
18
  class_attribute :_settings
18
19
  self._settings = Chewy::Index::Settings.new
19
20
 
20
- # Setups or returns ElasticSearch index name
21
- #
22
- # class UsersIndex < Chewy::Index
23
- # end
24
- # UsersIndex.index_name # => 'users'
25
- #
26
- # class UsersIndex < Chewy::Index
27
- # index_name 'dudes'
28
- # end
29
- # UsersIndex.index_name # => 'dudes'
30
- #
31
- def self.index_name(suggest = nil)
32
- if suggest
33
- @index_name = build_index_name(suggest, prefix: default_prefix)
34
- else
35
- @index_name ||= begin
36
- build_index_name(
37
- name.sub(/Index\Z/, '').demodulize.underscore,
38
- prefix: default_prefix
39
- ) if name
21
+ class << self
22
+ # @overload index_name(suggest)
23
+ # If suggested name is passed, it is set up as the new base name for
24
+ # the index. Used for the index base name redefinition.
25
+ #
26
+ # @example
27
+ # class UsersIndex < Chewy::Index
28
+ # index_name :legacy_users
29
+ # end
30
+ # UsersIndex.index_name # => 'legacy_users'
31
+ #
32
+ # @param suggest [String, Symbol] suggested base name
33
+ # @return [String] new base name
34
+ #
35
+ # @overload index_name(prefix: nil, suffix: nil)
36
+ # If suggested name is not passed, returns the base name accompanied
37
+ # with the prefix (if any) and suffix (if passed).
38
+ #
39
+ # @example
40
+ # class UsersIndex < Chewy::Index
41
+ # end
42
+ #
43
+ # Chewy.settings = {prefix: 'test'}
44
+ # UsersIndex.index_name # => 'test_users'
45
+ # UsersIndex.index_name(prefix: 'foobar') # => 'foobar_users'
46
+ # UsersIndex.index_name(suffix: '2017') # => 'test_users_2017'
47
+ # UsersIndex.index_name(prefix: '', suffix: '2017') # => 'users_2017'
48
+ #
49
+ # @param prefix [String] index name prefix, uses {.prefix} method by default
50
+ # @param suffix [String] index name suffix, used for creating several indexes for the same alias during the zero-downtime reset
51
+ # @raise [UndefinedIndex] if the base name is blank
52
+ # @return [String] result index name
53
+ def index_name(suggest = nil, prefix: nil, suffix: nil)
54
+ if suggest
55
+ @base_name = suggest.to_s.presence
56
+ else
57
+ [
58
+ prefix || prefix_with_deprecation,
59
+ base_name,
60
+ suffix
61
+ ].reject(&:blank?).join('_')
40
62
  end
41
63
  end
42
- @index_name or raise UndefinedIndex
43
- end
44
64
 
45
- # Prefix to use
46
- #
47
- def self.default_prefix
48
- Chewy.configuration[:prefix]
49
- end
65
+ # Base name for the index. Uses the default value inferred from the
66
+ # class name unless redefined.
67
+ #
68
+ # @example
69
+ # class Namespace::UsersIndex < Chewy::Index
70
+ # end
71
+ # UsersIndex.index_name # => 'users'
72
+ #
73
+ # Class.new(Chewy::Index).base_name # => raises UndefinedIndex
74
+ #
75
+ # @raise [UndefinedIndex] when the base name is blank
76
+ # @return [String] current base name
77
+ def base_name
78
+ @base_name ||= name.sub(/Index\z/, '').demodulize.underscore if name
79
+ raise UndefinedIndex if @base_name.blank?
80
+ @base_name
81
+ end
50
82
 
51
- # Defines type for the index. Arguments depends on adapter used. For
52
- # ActiveRecord you can pass model or scope and options
53
- #
54
- # class CarsIndex < Chewy::Index
55
- # define_type Car do
56
- # ...
57
- # end # defines VehiclesIndex::Car type
58
- # end
59
- #
60
- # Type name might be passed in complicated cases:
61
- #
62
- # class VehiclesIndex < Chewy::Index
63
- # define_type Vehicle.cars.includes(:manufacturer), name: 'cars' do
64
- # ...
65
- # end # defines VehiclesIndex::Cars type
66
- #
67
- # define_type Vehicle.motocycles.includes(:manufacturer), name: 'motocycles' do
68
- # ...
69
- # end # defines VehiclesIndex::Motocycles type
70
- # end
71
- #
72
- # For plain objects:
73
- #
74
- # class PlanesIndex < Chewy::Index
75
- # define_type :plane do
76
- # ...
77
- # end # defines PlanesIndex::Plane type
78
- # end
79
- #
80
- # The main difference between using plain objects or ActiveRecord models for indexing
81
- # is import. If you will call `CarsIndex::Car.import` - it will import all the cars
82
- # automatically, while `PlanesIndex::Plane.import(my_planes)` requires import data to be
83
- # passed.
84
- #
85
- def self.define_type(target, options = {}, &block)
86
- type_class = Chewy.create_type(self, target, options, &block)
87
- self.type_hash = type_hash.merge(type_class.type_name => type_class)
88
-
89
- unless respond_to?(type_class.type_name)
90
- class_eval <<-METHOD, __FILE__, __LINE__ + 1
91
- def self.#{type_class.type_name}
92
- ActiveSupport::Deprecation.warn("`#{self}.#{type_class.type_name}` accessor is deprecated and will be removed soon. Use `#{type_class}` directly instead.")
93
- type_hash['#{type_class.type_name}']
94
- end
95
- METHOD
83
+ # Similar to the {.base_name} but respects the class namespace, also,
84
+ # can't be redefined. Used to reference index with the string identifier
85
+ #
86
+ # @example
87
+ # class Namespace::UsersIndex < Chewy::Index
88
+ # end
89
+ # UsersIndex.derivable_name # => 'namespace/users'
90
+ #
91
+ # Class.new(Chewy::Index).derivable_name # => nil
92
+ #
93
+ # @return [String, nil] derivable name or nil when it is impossible to calculate
94
+ def derivable_name
95
+ @derivable_name ||= name.sub(/Index\z/, '').underscore if name
96
96
  end
97
- end
98
97
 
99
- # Types method has double usage.
100
- # If no arguments are passed - it returns array of defined types:
101
- #
102
- # UsersIndex.types # => [UsersIndex::Admin, UsersIndex::Manager, UsersIndex::User]
103
- #
104
- # If arguments are passed it treats like a part of chainable query DSL and
105
- # adds types array for index to select.
106
- #
107
- # UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager)
108
- # UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
109
- #
110
- def self.types *args
111
- if args.present?
112
- all.types *args
113
- else
114
- type_hash.values
98
+ # Used as a default value for {.index_name}. Return prefix from the configuration
99
+ # but can be redefined per-index to be more dynamic.
100
+ #
101
+ # @example
102
+ # class UsersIndex < Chewy::Index
103
+ # def self.prefix
104
+ # 'foobar'
105
+ # end
106
+ # end
107
+ # UsersIndex.index_name # => 'foobar_users'
108
+ #
109
+ # @return [String] prefix
110
+ def prefix
111
+ Chewy.configuration[:prefix]
115
112
  end
116
- end
117
113
 
118
- # Returns defined types names:
119
- #
120
- # UsersIndex.type_names # => ['admin', 'manager', 'user']
121
- #
122
- def self.type_names
123
- type_hash.keys
124
- end
114
+ # Defines type for the index. Arguments depends on adapter used. For
115
+ # ActiveRecord you can pass model or scope and options
116
+ #
117
+ # class CarsIndex < Chewy::Index
118
+ # define_type Car do
119
+ # ...
120
+ # end # defines VehiclesIndex::Car type
121
+ # end
122
+ #
123
+ # Type name might be passed in complicated cases:
124
+ #
125
+ # class VehiclesIndex < Chewy::Index
126
+ # define_type Vehicle.cars.includes(:manufacturer), name: 'cars' do
127
+ # ...
128
+ # end # defines VehiclesIndex::Cars type
129
+ #
130
+ # define_type Vehicle.motocycles.includes(:manufacturer), name: 'motocycles' do
131
+ # ...
132
+ # end # defines VehiclesIndex::Motocycles type
133
+ # end
134
+ #
135
+ # For plain objects:
136
+ #
137
+ # class PlanesIndex < Chewy::Index
138
+ # define_type :plane do
139
+ # ...
140
+ # end # defines PlanesIndex::Plane type
141
+ # end
142
+ #
143
+ # The main difference between using plain objects or ActiveRecord models for indexing
144
+ # is import. If you will call `CarsIndex::Car.import` - it will import all the cars
145
+ # automatically, while `PlanesIndex::Plane.import(my_planes)` requires import data to be
146
+ # passed.
147
+ #
148
+ def define_type(target, options = {}, &block)
149
+ type_class = Chewy.create_type(self, target, options, &block)
150
+ self.type_hash = type_hash.merge(type_class.type_name => type_class)
151
+ end
125
152
 
126
- # Used as a part of index definition DSL. Defines settings:
127
- #
128
- # class UsersIndex < Chewy::Index
129
- # settings analysis: {
130
- # analyzer: {
131
- # name: {
132
- # tokenizer: 'standard',
133
- # filter: ['lowercase', 'icu_folding', 'names_nysiis']
134
- # }
135
- # }
136
- # }
137
- # end
138
- #
139
- # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
140
- # for more details
141
- #
142
- # It is possible to store analyzers settings in Chewy repositories
143
- # and link them form index class. See `Chewy::Index::Settings` for details.
144
- #
145
- def self.settings(params)
146
- self._settings = Chewy::Index::Settings.new params
147
- end
153
+ # Types method has double usage.
154
+ # If no arguments are passed - it returns array of defined types:
155
+ #
156
+ # UsersIndex.types # => [UsersIndex::Admin, UsersIndex::Manager, UsersIndex::User]
157
+ #
158
+ # If arguments are passed it treats like a part of chainable query DSL and
159
+ # adds types array for index to select.
160
+ #
161
+ # UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager)
162
+ # UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
163
+ #
164
+ def types(*args)
165
+ if args.present?
166
+ all.types(*args)
167
+ else
168
+ type_hash.values
169
+ end
170
+ end
148
171
 
149
- # Returns list of public class methods defined in current index
150
- #
151
- def self.scopes
152
- public_methods - Chewy::Index.public_methods - type_names.map(&:to_sym)
153
- end
172
+ # Returns defined types names:
173
+ #
174
+ # UsersIndex.type_names # => ['admin', 'manager', 'user']
175
+ #
176
+ def type_names
177
+ type_hash.keys
178
+ end
154
179
 
155
- private
180
+ # Returns named type:
181
+ #
182
+ # UserIndex.type('admin') # => UsersIndex::Admin
183
+ #
184
+ def type(type_name)
185
+ type_hash.fetch(type_name) { raise UndefinedType, "Unknown type in #{name}: #{type_name}" }
186
+ end
156
187
 
157
- def self.build_index_name *args
158
- options = args.extract_options!
159
- [options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join(?_)
160
- end
188
+ # Used as a part of index definition DSL. Defines settings:
189
+ #
190
+ # class UsersIndex < Chewy::Index
191
+ # settings analysis: {
192
+ # analyzer: {
193
+ # name: {
194
+ # tokenizer: 'standard',
195
+ # filter: ['lowercase', 'icu_folding', 'names_nysiis']
196
+ # }
197
+ # }
198
+ # }
199
+ # end
200
+ #
201
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
202
+ # for more details
203
+ #
204
+ # It is possible to store analyzers settings in Chewy repositories
205
+ # and link them form index class. See `Chewy::Index::Settings` for details.
206
+ #
207
+ def settings(params = {}, &block)
208
+ self._settings = Chewy::Index::Settings.new(params, &block)
209
+ end
161
210
 
162
- def self.settings_hash
163
- _settings.to_hash
164
- end
211
+ # Returns list of public class methods defined in current index
212
+ #
213
+ def scopes
214
+ public_methods - Chewy::Index.public_methods
215
+ end
165
216
 
166
- def self.mappings_hash
167
- mappings = types.map(&:mappings_hash).inject(:merge)
168
- mappings.present? ? {mappings: mappings} : {}
169
- end
217
+ def settings_hash
218
+ _settings.to_hash
219
+ end
220
+
221
+ def mappings_hash
222
+ mappings = types.map(&:mappings_hash).inject(:merge)
223
+ mappings.present? ? {mappings: mappings} : {}
224
+ end
225
+
226
+ # Returns a hash containing the index settings and mappings
227
+ # Used for the ES index creation as body.
228
+ #
229
+ # @see Chewy::Index::Specification
230
+ # @return [Hash] specification as a hash
231
+ def specification_hash
232
+ [settings_hash, mappings_hash].inject(:merge)
233
+ end
234
+
235
+ def index_params
236
+ ActiveSupport::Deprecation.warn '`Chewy::Index.index_params` is deprecated and will be removed soon, use `Chewy::Index.specification_hash`'
237
+ specification_hash
238
+ end
239
+
240
+ # @see Chewy::Index::Specification
241
+ # @return [Chewy::Index::Specification] a specification object instance for this particular index
242
+ def specification
243
+ @specification ||= Specification.new(self)
244
+ end
170
245
 
171
- def self.index_params
172
- [settings_hash, mappings_hash].inject(:merge)
246
+ def derivable_index_name
247
+ ActiveSupport::Deprecation.warn '`Chewy::Index.derivable_index_name` is deprecated and will be removed soon, use `Chewy::Index.derivable_name` instead'
248
+ derivable_name
249
+ end
250
+
251
+ # Handling old default_prefix if it is not defined.
252
+ def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
253
+ if name == :default_prefix
254
+ ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, use `Chewy::Index.prefix` instead'
255
+ prefix
256
+ else
257
+ super
258
+ end
259
+ end
260
+
261
+ def prefix_with_deprecation
262
+ if respond_to?(:default_prefix)
263
+ ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, define `Chewy::Index.prefix` method instead'
264
+ default_prefix
265
+ else
266
+ prefix
267
+ end
268
+ end
269
+
270
+ def build_index_name(*args)
271
+ ActiveSupport::Deprecation.warn '`Chewy::Index.build_index_name` is deprecated and will be removed soon, use `Chewy::Index.index_name` instead'
272
+ index_name(args.extract_options!)
273
+ end
173
274
  end
174
275
  end
175
276
  end
@@ -0,0 +1,55 @@
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
+ #
9
+ class Journal
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
13
+ end
14
+
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))
32
+ end
33
+ stage += 1
34
+ since_time = entries.map(&:created_at).max
35
+ end
36
+ count
37
+ end
38
+
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)
45
+ end
46
+
47
+ private
48
+
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
53
+ end
54
+ end
55
+ end
@@ -16,17 +16,17 @@ module Chewy
16
16
  render_action('Delete by Query', event) { |payload| payload[:request] }
17
17
  end
18
18
 
19
- def render_action action, event, &block
19
+ def render_action(action, event)
20
20
  payload = event.payload
21
- description = block.call(payload)
21
+ description = yield(payload)
22
22
 
23
- if description.present?
24
- subject = payload[:type].presence || payload[:index]
25
- action = "#{subject} #{action} (#{event.duration.round(1)}ms)"
26
- action = color(action, GREEN, true)
23
+ return if description.blank?
27
24
 
28
- debug(" #{action} #{description}")
29
- end
25
+ subject = payload[:type].presence || payload[:index]
26
+ action = "#{subject} #{action} (#{event.duration.round(1)}ms)"
27
+ action = color(action, GREEN, true)
28
+
29
+ debug(" #{action} #{description}")
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,77 @@
1
+ require_relative 'search_index_receiver'
2
+
3
+ module Chewy
4
+ module Minitest
5
+ module Helpers
6
+ extend ActiveSupport::Concern
7
+
8
+ # Assert that an index *changes* during a block.
9
+ # @param index [Chewy::Type] the index / type to watch, eg EntitiesIndex::Entity.
10
+ # @param strategy [Symbol] the Chewy strategy to use around the block. See Chewy docs.
11
+ # @param bypass_actual_index [true, false]
12
+ # True to preempt the http call to Elastic, false otherwise.
13
+ # Should be set to true unless actually testing search functionality.
14
+ #
15
+ # @return [SearchIndexReceiver] for optional further assertions on the nature of the index changes.
16
+ #
17
+ def assert_indexes(index, strategy: :atomic, bypass_actual_index: true)
18
+ type = Chewy.derive_type index
19
+ receiver = SearchIndexReceiver.new
20
+
21
+ bulk_method = type.method :bulk
22
+ # Manually mocking #bulk because we need to properly capture `self`
23
+ bulk_mock = lambda do |*bulk_args|
24
+ receiver.catch bulk_args, self
25
+
26
+ bulk_method.call(*bulk_args) unless bypass_actual_index
27
+
28
+ {}
29
+ end
30
+
31
+ type.define_singleton_method :bulk, bulk_mock
32
+
33
+ Chewy.strategy(strategy) do
34
+ yield
35
+ end
36
+
37
+ type.define_singleton_method :bulk, bulk_method
38
+
39
+ assert_includes receiver.updated_indexes, index, "Expected #{index} to be updated but it wasn't"
40
+
41
+ receiver
42
+ end
43
+
44
+ # Run indexing for the database changes during the block provided.
45
+ # By default, indexing is run at the end of the block.
46
+ # @param strategy [Symbol] the Chewy index update strategy see Chewy docs.
47
+ def run_indexing(strategy: :atomic)
48
+ Chewy.strategy strategy do
49
+ yield
50
+ end
51
+ end
52
+
53
+ module ClassMethods
54
+ # Declare that all tests in this file require real indexing, always.
55
+ # In my completely unscientific experiments, this roughly doubled test runtime.
56
+ # Use with trepidation.
57
+ def index_everything!
58
+ setup do
59
+ Chewy.strategy :urgent
60
+ end
61
+
62
+ teardown do
63
+ Chewy.strategy.pop
64
+ end
65
+ end
66
+ end
67
+
68
+ included do
69
+ teardown do
70
+ # always destroy indexes between tests
71
+ # Prevent croll pollution of test cases due to indexing
72
+ Chewy.massacre
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,80 @@
1
+ # Test helper class to provide minitest hooks for Chewy::Index testing.
2
+ #
3
+ # @note Intended to be used in conjunction with a test helper which mocks over the #bulk
4
+ # method on a {Chewy::Type} class. (See SearchTestHelper)
5
+ #
6
+ # The class will capture the data from the *param on the Chewy::Type.bulk method and
7
+ # aggregate the data for test analysis.
8
+ class SearchIndexReceiver
9
+ def initialize
10
+ @mutations = {}
11
+ end
12
+
13
+ # @param bulk_params [Hash] the bulk_params that should be sent to the Chewy::Type.bulk method.
14
+ # @param type [Chewy::Type] the type executing this query.
15
+ def catch(bulk_params, type)
16
+ Array.wrap(bulk_params).map { |y| y[:body] }.flatten.each do |update|
17
+ if update[:delete]
18
+ mutation_for(type).deletes << update[:delete][:_id]
19
+ elsif update[:index]
20
+ mutation_for(type).indexes << update[:index]
21
+ end
22
+ end
23
+ end
24
+
25
+ # @param index [Chewy::Index] return only index requests to the specified {Chewy::Type} index.
26
+ # @return [Hash] the index changes captured by the mock.
27
+ def indexes_for(index = nil)
28
+ if index
29
+ mutation_for(index).indexes
30
+ else
31
+ Hash[
32
+ @mutations.map { |a, b| [a, b.indexes] }
33
+ ]
34
+ end
35
+ end
36
+ alias_method :indexes, :indexes_for
37
+
38
+ # @param index [Chewy::Index] return only delete requests to the specified {Chewy::Type} index.
39
+ # @return [Hash] the index deletes captured by the mock.
40
+ def deletes_for(index = nil)
41
+ if index
42
+ mutation_for(index).deletes
43
+ else
44
+ Hash[
45
+ @mutations.map { |a, b| [a, b.deletes] }
46
+ ]
47
+ end
48
+ end
49
+ alias_method :deletes, :deletes_for
50
+
51
+ # Check to see if a given object has been indexed.
52
+ # @param obj [#id] obj the object to look for.
53
+ # @param type [Chewy::Type] what type the object should be indexed as.
54
+ # @return [true, false] if the object was indexed.
55
+ def indexed?(obj, type)
56
+ indexes_for(type).map { |i| i[:_id] }.include? obj.id
57
+ end
58
+
59
+ # Check to see if a given object has been deleted.
60
+ # @param obj [#id] obj the object to look for.
61
+ # @param type [Chewy::Type] what type the object should have been deleted from.
62
+ # @return [true, false] if the object was deleted.
63
+ def deleted?(obj, type)
64
+ deletes_for(type).include? obj.id
65
+ end
66
+
67
+ # @return [Array<Chewy::Type>] a list of types indexes changed.
68
+ def updated_indexes
69
+ @mutations.keys
70
+ end
71
+
72
+ private
73
+
74
+ # Get the mutation object for a given type.
75
+ # @param type [Chewy::Type] the index type to fetch.
76
+ # @return [#indexes, #deletes] an object with a list of indexes and a list of deletes.
77
+ def mutation_for(type)
78
+ @mutations[type] ||= OpenStruct.new(indexes: [], deletes: [])
79
+ end
80
+ end
@@ -0,0 +1 @@
1
+ require 'chewy/minitest/helpers'