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
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'chewy/version'
@@ -6,8 +5,8 @@ require 'chewy/version'
6
5
  Gem::Specification.new do |spec| # rubocop:disable BlockLength
7
6
  spec.name = 'chewy'
8
7
  spec.version = Chewy::VERSION
9
- spec.authors = ['pyromaniac']
10
- spec.email = ['kinwizard@gmail.com']
8
+ spec.authors = ['Toptal, LLC', 'pyromaniac']
9
+ spec.email = ['open-source@toptal.com', 'kinwizard@gmail.com']
11
10
  spec.summary = 'Elasticsearch ODM client wrapper'
12
11
  spec.description = 'Chewy provides functionality for Elasticsearch index handling, documents import mappings and chainable query DSL'
13
12
  spec.homepage = 'https://github.com/toptal/chewy'
@@ -18,22 +17,22 @@ Gem::Specification.new do |spec| # rubocop:disable BlockLength
18
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
18
  spec.require_paths = ['lib']
20
19
 
21
- spec.add_development_dependency 'rake'
22
20
  spec.add_development_dependency 'appraisal'
23
- spec.add_development_dependency 'rspec'
24
- spec.add_development_dependency 'rspec-its'
25
- spec.add_development_dependency 'rspec-collection_matchers'
26
- spec.add_development_dependency 'sqlite3'
27
21
  spec.add_development_dependency 'database_cleaner'
28
22
  spec.add_development_dependency 'elasticsearch-extensions'
23
+ spec.add_development_dependency 'rake'
29
24
  spec.add_development_dependency 'resque_spec'
25
+ spec.add_development_dependency 'rspec', '>= 3.7.0'
26
+ spec.add_development_dependency 'rspec-collection_matchers'
27
+ spec.add_development_dependency 'rspec-its'
28
+ spec.add_development_dependency 'rubocop', '0.52.1'
29
+ spec.add_development_dependency 'sqlite3'
30
30
  spec.add_development_dependency 'timecop'
31
- spec.add_development_dependency 'rubocop'
32
- spec.add_development_dependency 'rubysl', '~> 2.0' if RUBY_ENGINE == 'rbx'
33
31
 
34
32
  spec.add_development_dependency 'method_source'
35
33
  spec.add_development_dependency 'unparser'
36
34
 
37
- spec.add_dependency 'activesupport', '>= 3.2'
38
- spec.add_dependency 'elasticsearch', '>= 1.0.0'
35
+ spec.add_dependency 'activesupport', '>= 5.2'
36
+ spec.add_dependency 'elasticsearch', '>= 2.0.0'
37
+ spec.add_dependency 'elasticsearch-dsl'
39
38
  end
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.2.0"
6
+ gem "activesupport", "~> 5.2.0"
7
+ gem "activejob", "~> 5.2.0"
8
+ gem "resque", require: false
9
+ gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
11
+ gem "sidekiq", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
13
+ gem "will_paginate", require: false
14
+ gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
16
+
17
+ gemspec path: "../"
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "mongoid", "~> 6.4.0"
6
+ gem "activesupport", "~> 5.2.0"
7
+ gem "activejob", "~> 5.2.0"
8
+ gem "resque", require: false
9
+ gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
11
+ gem "sidekiq", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
13
+ gem "will_paginate", require: false
14
+ gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
16
+
17
+ gemspec path: "../"
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 6.0.0"
6
+ gem "activesupport", "~> 6.0.0"
7
+ gem "activejob", "~> 6.0.0"
8
+ gem "resque", require: false
9
+ gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
11
+ gem "sidekiq", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
13
+ gem "will_paginate", require: false
14
+ gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
16
+
17
+ gemspec path: "../"
@@ -0,0 +1,19 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 6.1.0"
6
+ gem "activesupport", "~> 6.1.0"
7
+ gem "activejob", "~> 6.1.0"
8
+ gem "resque", require: false
9
+ gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
11
+ gem "sidekiq", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
13
+ gem "will_paginate", require: false
14
+ gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
16
+
17
+ eval(File.read('gemfiles/ruby3.gemfile'), nil, 'gemfiles/ruby3.gemfile') if RUBY_VERSION >= '3.0.0'
18
+
19
+ gemspec path: "../"
@@ -0,0 +1,10 @@
1
+ # kwargs support was added here:
2
+ # https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/method_double.rb#L66
3
+ # but this change was not released yet so for the time being use main branch
4
+ gem 'rspec', github: 'rspec/rspec', branch: 'main'
5
+ gem 'rspec-core', github: 'rspec/rspec-core', branch: 'main'
6
+ gem 'rspec-expectations', github: 'rspec/rspec-expectations', branch: 'main'
7
+ gem 'rspec-mocks', github: 'rspec/rspec-mocks', branch: 'main'
8
+ gem 'rspec-support', github: 'rspec/rspec-support', branch: 'main'
9
+ # extracted from stdlib
10
+ gem 'rexml'
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sequel", "~> 4.45.0"
6
+ gem "activesupport", "~> 5.1.0"
7
+ gem "kaminari-core", "~> 1.1.0", require: false
8
+ gem "will_paginate", require: false
9
+ gem "parallel", require: false
10
+
11
+ gemspec path: "../"
@@ -1,3 +1,4 @@
1
+ require 'active_support/version'
1
2
  require 'active_support/concern'
2
3
  require 'active_support/deprecation'
3
4
  require 'active_support/json'
@@ -16,9 +17,32 @@ require 'active_support/core_ext/string/inflections'
16
17
  require 'i18n/core_ext/hash'
17
18
  require 'chewy/backports/deep_dup' unless Object.respond_to?(:deep_dup)
18
19
  require 'singleton'
20
+ require 'base64'
19
21
 
20
22
  require 'elasticsearch'
21
23
 
24
+ def try_require(path)
25
+ require path
26
+ rescue LoadError
27
+ nil
28
+ end
29
+
30
+ try_require 'kaminari'
31
+ try_require 'kaminari/core'
32
+ try_require 'will_paginate'
33
+ try_require 'will_paginate/collection'
34
+ try_require 'parallel'
35
+
36
+ ActiveSupport.on_load(:active_record) do
37
+ try_require 'will_paginate/active_record'
38
+ try_require 'kaminari/activerecord'
39
+ end
40
+
41
+ ActiveSupport.on_load(:mongoid) do
42
+ try_require 'will_paginate/mongoid'
43
+ try_require 'kaminari/mongoid'
44
+ end
45
+
22
46
  require 'chewy/version'
23
47
  require 'chewy/errors'
24
48
  require 'chewy/config'
@@ -34,29 +58,8 @@ require 'chewy/fields/root'
34
58
  require 'chewy/journal'
35
59
  require 'chewy/railtie' if defined?(::Rails::Railtie)
36
60
 
37
- begin
38
- require 'kaminari'
39
- require 'chewy/query/pagination/kaminari'
40
- rescue LoadError
41
- nil
42
- end
43
-
44
- begin
45
- require 'will_paginate'
46
- require 'will_paginate/collection'
47
- require 'chewy/query/pagination/will_paginate'
48
- rescue LoadError
49
- nil
50
- end
51
-
52
61
  ActiveSupport.on_load(:active_record) do
53
62
  extend Chewy::Type::Observe::ActiveRecordMethods
54
-
55
- begin
56
- require 'will_paginate/active_record'
57
- rescue LoadError
58
- nil
59
- end
60
63
  end
61
64
 
62
65
  ActiveSupport.on_load(:mongoid) do
@@ -67,13 +70,6 @@ ActiveSupport.on_load(:mongoid) do
67
70
  end
68
71
  end
69
72
  end
70
-
71
- begin
72
- require 'will_paginate/mongoid'
73
- require 'chewy/query/pagination/will_paginate'
74
- rescue LoadError
75
- nil
76
- end
77
73
  end
78
74
 
79
75
  module Chewy
@@ -87,29 +83,47 @@ module Chewy
87
83
  class << self
88
84
  attr_accessor :adapters
89
85
 
90
- # Derives type from string `index#type` representation:
91
- #
92
- # Chewy.derive_type('users#user') # => UsersIndex::User
93
- #
94
- # If index has only one type - it is possible to derive it without specification:
86
+ # Derives a single type for the passed string identifier if possible.
95
87
  #
96
- # Chewy.derive_type('users') # => UsersIndex::User
97
- #
98
- # If index has more then one type - it raises Chewy::UnderivableType.
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
99
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
100
97
  def derive_type(name)
101
98
  return name if name.is_a?(Class) && name < Chewy::Type
102
99
 
103
- index_name, type_name = name.split('#', 2)
104
- class_name = "#{index_name.camelize}Index"
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
103
+ end
104
+
105
+ # Derives all the types for the passed string identifier if possible.
106
+ #
107
+ # @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]
111
+ #
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)
117
+
118
+ index_name, type_name = from.split('#', 2)
119
+ class_name = "#{index_name.camelize.gsub(/Index\z/, '')}Index"
105
120
  index = class_name.safe_constantize
106
121
  raise Chewy::UnderivableType, "Can not find index named `#{class_name}`" unless index && index < Chewy::Index
107
122
  if type_name.present?
108
- index.type_hash[type_name] or raise Chewy::UnderivableType, "Index `#{class_name}` doesn`t have type named `#{type_name}`"
109
- elsif index.types.one?
110
- index.types.first
123
+ type = index.type_hash[type_name] or raise Chewy::UnderivableType, "Index `#{class_name}` doesn`t have type named `#{type_name}`"
124
+ [type]
111
125
  else
112
- raise Chewy::UnderivableType, "Index `#{class_name}` has more than one type, please specify type via `#{index_name}#type_name`"
126
+ index.types
113
127
  end
114
128
  end
115
129
 
@@ -118,7 +132,7 @@ module Chewy
118
132
  def create_type(index, target, options = {}, &block)
119
133
  type = Class.new(Chewy::Type)
120
134
 
121
- adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
135
+ adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, **options)
122
136
 
123
137
  index.const_set(adapter.name, type)
124
138
  type.send(:define_singleton_method, :index) { index }
@@ -152,7 +166,7 @@ module Chewy
152
166
  # Be careful, if current prefix is blank, this will destroy all the indexes.
153
167
  #
154
168
  def massacre
155
- Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].delete_if(&:blank?).join('_'))
169
+ Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].reject(&:blank?).join('_'))
156
170
  Chewy.wait_for_status
157
171
  end
158
172
  alias_method :delete_all, :massacre
@@ -202,5 +216,26 @@ module Chewy
202
216
  Chewy::Repository.instance
203
217
  end
204
218
  delegate(*Chewy::Repository.delegated, to: :repository)
219
+
220
+ def create_indices
221
+ Chewy::Index.descendants.each(&:create)
222
+ end
223
+
224
+ def create_indices!
225
+ Chewy::Index.descendants.each(&:create!)
226
+ end
227
+
228
+ def eager_load!
229
+ return unless defined?(Chewy::Railtie)
230
+ dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[Chewy.configuration[:indices_path]] }.compact.map(&:existent).flatten.uniq
231
+
232
+ dirs.each do |dir|
233
+ Dir.glob(File.join(dir, '**/*.rb')).each do |file|
234
+ require_dependency file
235
+ end
236
+ end
237
+ end
205
238
  end
206
239
  end
240
+
241
+ require 'chewy/stash'
@@ -79,7 +79,7 @@ end
79
79
  require 'bigdecimal'
80
80
  class BigDecimal
81
81
  begin
82
- BigDecimal.new('4.56').dup
82
+ BigDecimal('4.56').dup
83
83
 
84
84
  def duplicable?
85
85
  true
@@ -2,45 +2,56 @@ module Chewy
2
2
  class Config
3
3
  include Singleton
4
4
 
5
- attr_accessor :settings, :transport_logger, :transport_tracer, :logger,
6
-
5
+ attr_accessor :settings, :logger,
7
6
  # Default query compilation mode. `:must` by default.
8
7
  # See Chewy::Query#query_mode for details
9
8
  #
10
9
  :query_mode,
11
-
12
10
  # Default filters compilation mode. `:and` by default.
13
11
  # See Chewy::Query#filter_mode for details
14
12
  #
15
13
  :filter_mode,
16
-
17
14
  # Default post_filters compilation mode. `nil` by default.
18
15
  # See Chewy::Query#post_filter_mode for details
19
16
  #
20
17
  :post_filter_mode,
21
-
22
18
  # The first strategy in stack. `:base` by default.
23
19
  # If you need to return to the previous chewy behavior -
24
20
  # just set it to `:bypass`
25
21
  #
26
22
  :root_strategy,
27
-
28
23
  # Default request strategy middleware, used in e.g
29
24
  # Rails controllers. See Chewy::Railtie::RequestStrategy
30
25
  # for more info.
31
26
  #
32
27
  :request_strategy,
33
-
34
28
  # Use after_commit callbacks for RDBMS instead of
35
29
  # after_save and after_destroy. True by default. Useful
36
30
  # in tests with transactional fixtures or transactional
37
31
  # DatabaseCleaner strategy.
38
32
  #
39
33
  :use_after_commit_callbacks,
40
-
41
34
  # Where Chewy expects to find index definitions
42
35
  # within a Rails app folder.
43
- :indices_path
36
+ :indices_path,
37
+ # Set index refresh_interval setting to -1 before reset and put the original value after.
38
+ # If setting not present, put back to default 1s
39
+ # https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
40
+ :reset_disable_refresh_interval,
41
+ # Set number_of_replicas to 0 before reset and put the original value after
42
+ # https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
43
+ :reset_no_replicas,
44
+ # Refresh or not when import async (sidekiq, resque, activejob)
45
+ :disable_refresh_async,
46
+ # Default options for root of Chewy type. Allows to set default options
47
+ # for type mappings like `_all`.
48
+ :default_root_options,
49
+ # Default field type for any field in any Chewy type. Defaults to 'string'.
50
+ :default_field_type
51
+
52
+ attr_reader :transport_logger, :transport_tracer,
53
+ # Chewy search request DSL base class, used by every index.
54
+ :search_class
44
55
 
45
56
  def self.delegated
46
57
  public_instance_methods - superclass.public_instance_methods - Singleton.public_instance_methods
@@ -53,7 +64,13 @@ module Chewy
53
64
  @root_strategy = :base
54
65
  @request_strategy = :atomic
55
66
  @use_after_commit_callbacks = true
67
+ @reset_disable_refresh_interval = false
68
+ @reset_no_replicas = false
69
+ @disable_refresh_async = false
56
70
  @indices_path = 'app/chewy'
71
+ @default_root_options = {}
72
+ @default_field_type = 'text'.freeze
73
+ self.search_class = Chewy::Search::Request
57
74
  end
58
75
 
59
76
  def transport_logger=(logger)
@@ -66,6 +83,10 @@ module Chewy
66
83
  @transport_tracer = tracer
67
84
  end
68
85
 
86
+ def search_class=(base)
87
+ @search_class = build_search_class(base)
88
+ end
89
+
69
90
  # Chewy core configurations. There is two ways to set it up:
70
91
  # use `Chewy.settings=` method or, for Rails application,
71
92
  # create `config/chewy.yml` file. Btw, `config/chewy.yml` supports
@@ -92,7 +113,7 @@ module Chewy
92
113
  #
93
114
  # :wait_for_status - if this option set - chewy actions such
94
115
  # as creating or deleting index, importing data will wait for
95
- # the status specified. Extremely useful for tests under havy
116
+ # the status specified. Extremely useful for tests under heavy
96
117
  # indexes manipulations.
97
118
  #
98
119
  # test:
@@ -117,25 +138,30 @@ module Chewy
117
138
  end
118
139
  end
119
140
 
120
- def configuration=(options)
121
- ActiveSupport::Deprecation.warn("`Chewy.configuration = {foo: 'bar'}` method is deprecated and will be removed soon, use `Chewy.settings = {foo: 'bar'}` method instead")
122
- self.settings = options
123
- end
124
-
125
141
  private
126
142
 
127
143
  def yaml_settings
128
144
  @yaml_settings ||= begin
129
- if defined?(Rails)
145
+ if defined?(Rails::VERSION)
130
146
  file = Rails.root.join('config', 'chewy.yml')
131
147
 
132
148
  if File.exist?(file)
133
149
  yaml = ERB.new(File.read(file)).result
134
- hash = YAML.load(yaml)
150
+ hash = YAML.load(yaml) # rubocop:disable Security/YAMLLoad
135
151
  hash[Rails.env].try(:deep_symbolize_keys) if hash
136
152
  end
137
153
  end || {}
138
154
  end
139
155
  end
156
+
157
+ def build_search_class(base)
158
+ Class.new(base).tap do |search_class|
159
+ if defined?(::Kaminari)
160
+ search_class.send :include, Chewy::Search::Pagination::Kaminari
161
+ elsif defined?(::WillPaginate)
162
+ search_class.send :include, Chewy::Search::Pagination::WillPaginate
163
+ end
164
+ end
165
+ end
140
166
  end
141
167
  end