chewy 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (265) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +24 -2
  4. data/.rubocop_todo.yml +2 -2
  5. data/.travis.yml +38 -21
  6. data/.yardopts +5 -0
  7. data/Appraisals +55 -27
  8. data/CHANGELOG.md +57 -12
  9. data/Gemfile +14 -10
  10. data/LEGACY_DSL.md +497 -0
  11. data/README.md +249 -515
  12. data/chewy.gemspec +5 -4
  13. data/gemfiles/rails.4.0.activerecord.gemfile +14 -0
  14. data/gemfiles/rails.4.1.activerecord.gemfile +14 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +8 -10
  16. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +9 -10
  17. data/gemfiles/rails.5.0.activerecord.gemfile +8 -10
  18. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +15 -0
  19. data/gemfiles/rails.5.1.activerecord.gemfile +15 -0
  20. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +15 -0
  21. data/gemfiles/sequel.4.45.gemfile +11 -0
  22. data/lib/chewy.rb +77 -43
  23. data/lib/chewy/config.rb +44 -7
  24. data/lib/chewy/errors.rb +2 -2
  25. data/lib/chewy/fields/base.rb +39 -32
  26. data/lib/chewy/fields/root.rb +33 -7
  27. data/lib/chewy/index.rb +237 -149
  28. data/lib/chewy/index/actions.rb +85 -28
  29. data/lib/chewy/index/aliases.rb +2 -1
  30. data/lib/chewy/index/settings.rb +9 -5
  31. data/lib/chewy/index/specification.rb +58 -0
  32. data/lib/chewy/journal.rb +40 -92
  33. data/lib/chewy/query.rb +43 -27
  34. data/lib/chewy/query/compose.rb +13 -13
  35. data/lib/chewy/query/criteria.rb +13 -13
  36. data/lib/chewy/query/filters.rb +1 -1
  37. data/lib/chewy/query/loading.rb +1 -1
  38. data/lib/chewy/query/nodes/and.rb +2 -2
  39. data/lib/chewy/query/nodes/bool.rb +1 -1
  40. data/lib/chewy/query/nodes/equal.rb +2 -2
  41. data/lib/chewy/query/nodes/exists.rb +1 -1
  42. data/lib/chewy/query/nodes/has_relation.rb +2 -2
  43. data/lib/chewy/query/nodes/match_all.rb +1 -1
  44. data/lib/chewy/query/nodes/missing.rb +1 -1
  45. data/lib/chewy/query/nodes/not.rb +2 -2
  46. data/lib/chewy/query/nodes/or.rb +2 -2
  47. data/lib/chewy/query/nodes/prefix.rb +1 -1
  48. data/lib/chewy/query/nodes/query.rb +2 -2
  49. data/lib/chewy/query/nodes/range.rb +4 -4
  50. data/lib/chewy/query/nodes/regexp.rb +4 -4
  51. data/lib/chewy/query/nodes/script.rb +3 -3
  52. data/lib/chewy/query/pagination.rb +10 -1
  53. data/lib/chewy/railtie.rb +1 -0
  54. data/lib/chewy/rake_helper.rb +265 -48
  55. data/lib/chewy/rspec/update_index.rb +30 -22
  56. data/lib/chewy/search.rb +78 -21
  57. data/lib/chewy/search/loader.rb +83 -0
  58. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  59. data/lib/chewy/search/pagination/will_paginate.rb +41 -0
  60. data/lib/chewy/search/parameters.rb +150 -0
  61. data/lib/chewy/search/parameters/aggs.rb +16 -0
  62. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  63. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  64. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  65. data/lib/chewy/search/parameters/concerns/query_storage.rb +237 -0
  66. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  67. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  68. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  69. data/lib/chewy/search/parameters/explain.rb +16 -0
  70. data/lib/chewy/search/parameters/filter.rb +47 -0
  71. data/lib/chewy/search/parameters/highlight.rb +16 -0
  72. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  73. data/lib/chewy/search/parameters/limit.rb +17 -0
  74. data/lib/chewy/search/parameters/load.rb +32 -0
  75. data/lib/chewy/search/parameters/min_score.rb +16 -0
  76. data/lib/chewy/search/parameters/none.rb +27 -0
  77. data/lib/chewy/search/parameters/offset.rb +17 -0
  78. data/lib/chewy/search/parameters/order.rb +64 -0
  79. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  80. data/lib/chewy/search/parameters/preference.rb +16 -0
  81. data/lib/chewy/search/parameters/profile.rb +16 -0
  82. data/lib/chewy/search/parameters/query.rb +19 -0
  83. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  84. data/lib/chewy/search/parameters/rescore.rb +29 -0
  85. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  86. data/lib/chewy/search/parameters/search_after.rb +20 -0
  87. data/lib/chewy/search/parameters/search_type.rb +16 -0
  88. data/lib/chewy/search/parameters/source.rb +73 -0
  89. data/lib/chewy/search/parameters/storage.rb +95 -0
  90. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  91. data/lib/chewy/search/parameters/suggest.rb +16 -0
  92. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  93. data/lib/chewy/search/parameters/timeout.rb +16 -0
  94. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  95. data/lib/chewy/search/parameters/types.rb +20 -0
  96. data/lib/chewy/search/parameters/version.rb +16 -0
  97. data/lib/chewy/search/query_proxy.rb +257 -0
  98. data/lib/chewy/search/request.rb +1021 -0
  99. data/lib/chewy/search/response.rb +119 -0
  100. data/lib/chewy/search/scoping.rb +50 -0
  101. data/lib/chewy/search/scrolling.rb +136 -0
  102. data/lib/chewy/stash.rb +70 -0
  103. data/lib/chewy/strategy.rb +10 -3
  104. data/lib/chewy/strategy/active_job.rb +1 -0
  105. data/lib/chewy/strategy/atomic.rb +1 -3
  106. data/lib/chewy/strategy/bypass.rb +1 -1
  107. data/lib/chewy/strategy/resque.rb +1 -0
  108. data/lib/chewy/strategy/shoryuken.rb +40 -0
  109. data/lib/chewy/strategy/sidekiq.rb +13 -3
  110. data/lib/chewy/type.rb +29 -7
  111. data/lib/chewy/type/actions.rb +26 -2
  112. data/lib/chewy/type/adapter/active_record.rb +44 -29
  113. data/lib/chewy/type/adapter/base.rb +27 -7
  114. data/lib/chewy/type/adapter/mongoid.rb +18 -7
  115. data/lib/chewy/type/adapter/object.rb +187 -26
  116. data/lib/chewy/type/adapter/orm.rb +59 -32
  117. data/lib/chewy/type/adapter/sequel.rb +32 -16
  118. data/lib/chewy/type/import.rb +145 -191
  119. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  120. data/lib/chewy/type/import/bulk_request.rb +76 -0
  121. data/lib/chewy/type/import/journal_builder.rb +45 -0
  122. data/lib/chewy/type/import/routine.rb +138 -0
  123. data/lib/chewy/type/mapping.rb +11 -1
  124. data/lib/chewy/type/observe.rb +1 -1
  125. data/lib/chewy/type/syncer.rb +220 -0
  126. data/lib/chewy/type/witchcraft.rb +27 -13
  127. data/lib/chewy/type/wrapper.rb +28 -2
  128. data/lib/chewy/version.rb +1 -1
  129. data/lib/tasks/chewy.rake +84 -26
  130. data/spec/chewy/config_spec.rb +82 -1
  131. data/spec/chewy/fields/base_spec.rb +147 -112
  132. data/spec/chewy/fields/root_spec.rb +75 -18
  133. data/spec/chewy/fields/time_fields_spec.rb +2 -3
  134. data/spec/chewy/index/actions_spec.rb +180 -50
  135. data/spec/chewy/index/aliases_spec.rb +2 -2
  136. data/spec/chewy/index/settings_spec.rb +67 -38
  137. data/spec/chewy/index/specification_spec.rb +160 -0
  138. data/spec/chewy/index_spec.rb +57 -66
  139. data/spec/chewy/journal_spec.rb +149 -54
  140. data/spec/chewy/minitest/helpers_spec.rb +4 -4
  141. data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
  142. data/spec/chewy/query/criteria_spec.rb +179 -179
  143. data/spec/chewy/query/filters_spec.rb +15 -15
  144. data/spec/chewy/query/loading_spec.rb +22 -20
  145. data/spec/chewy/query/nodes/and_spec.rb +2 -2
  146. data/spec/chewy/query/nodes/bool_spec.rb +4 -4
  147. data/spec/chewy/query/nodes/equal_spec.rb +19 -19
  148. data/spec/chewy/query/nodes/exists_spec.rb +6 -6
  149. data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
  150. data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
  151. data/spec/chewy/query/nodes/missing_spec.rb +5 -5
  152. data/spec/chewy/query/nodes/not_spec.rb +3 -2
  153. data/spec/chewy/query/nodes/or_spec.rb +2 -2
  154. data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
  155. data/spec/chewy/query/nodes/query_spec.rb +2 -2
  156. data/spec/chewy/query/nodes/range_spec.rb +18 -18
  157. data/spec/chewy/query/nodes/raw_spec.rb +1 -1
  158. data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
  159. data/spec/chewy/query/nodes/script_spec.rb +4 -4
  160. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  161. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  162. data/spec/chewy/query/pagination_spec.rb +25 -21
  163. data/spec/chewy/query_spec.rb +501 -560
  164. data/spec/chewy/rake_helper_spec.rb +368 -0
  165. data/spec/chewy/repository_spec.rb +4 -4
  166. data/spec/chewy/rspec/update_index_spec.rb +89 -56
  167. data/spec/chewy/runtime_spec.rb +2 -2
  168. data/spec/chewy/search/loader_spec.rb +117 -0
  169. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  170. data/spec/chewy/search/pagination/kaminari_spec.rb +17 -0
  171. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  172. data/spec/chewy/search/pagination/will_paginate_spec.rb +17 -0
  173. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  174. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  175. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  176. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  177. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  178. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  179. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  180. data/spec/chewy/search/parameters/indices_boost_spec.rb +83 -0
  181. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  182. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  183. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  184. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  185. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  186. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  187. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  188. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  189. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  190. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  191. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  192. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  193. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  194. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  195. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  196. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  197. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  198. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  199. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  200. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  201. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  202. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  203. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  204. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  205. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  206. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  207. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  208. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  209. data/spec/chewy/search/parameters_spec.rb +130 -0
  210. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  211. data/spec/chewy/search/request_spec.rb +669 -0
  212. data/spec/chewy/search/response_spec.rb +192 -0
  213. data/spec/chewy/search/scrolling_spec.rb +169 -0
  214. data/spec/chewy/search_spec.rb +13 -6
  215. data/spec/chewy/stash_spec.rb +95 -0
  216. data/spec/chewy/strategy/active_job_spec.rb +6 -0
  217. data/spec/chewy/strategy/resque_spec.rb +6 -0
  218. data/spec/chewy/strategy/shoryuken_spec.rb +64 -0
  219. data/spec/chewy/strategy/sidekiq_spec.rb +8 -0
  220. data/spec/chewy/strategy_spec.rb +6 -6
  221. data/spec/chewy/type/actions_spec.rb +29 -10
  222. data/spec/chewy/type/adapter/active_record_spec.rb +203 -91
  223. data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
  224. data/spec/chewy/type/adapter/object_spec.rb +101 -28
  225. data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
  226. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  227. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  228. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  229. data/spec/chewy/type/import/routine_spec.rb +110 -0
  230. data/spec/chewy/type/import_spec.rb +350 -271
  231. data/spec/chewy/type/mapping_spec.rb +54 -18
  232. data/spec/chewy/type/observe_spec.rb +5 -1
  233. data/spec/chewy/type/syncer_spec.rb +123 -0
  234. data/spec/chewy/type/witchcraft_spec.rb +45 -29
  235. data/spec/chewy/type/wrapper_spec.rb +63 -23
  236. data/spec/chewy/type_spec.rb +28 -7
  237. data/spec/chewy_spec.rb +75 -7
  238. data/spec/spec_helper.rb +5 -2
  239. data/spec/support/active_record.rb +5 -1
  240. data/spec/support/class_helpers.rb +0 -14
  241. data/spec/support/mongoid.rb +15 -3
  242. data/spec/support/sequel.rb +6 -1
  243. metadata +198 -37
  244. data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
  245. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
  246. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
  247. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
  248. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
  249. data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
  250. data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
  251. data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
  252. data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
  253. data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
  254. data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
  255. data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
  256. data/gemfiles/sequel.4.38.gemfile +0 -14
  257. data/lib/chewy/journal/apply.rb +0 -31
  258. data/lib/chewy/journal/clean.rb +0 -24
  259. data/lib/chewy/journal/entry.rb +0 -83
  260. data/lib/chewy/journal/query.rb +0 -87
  261. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  262. data/lib/chewy/query/scoping.rb +0 -20
  263. data/spec/chewy/journal/apply_spec.rb +0 -120
  264. data/spec/chewy/journal/entry_spec.rb +0 -237
  265. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
@@ -1,9 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Chewy::Fields::Root do
4
- specify { expect(described_class.new('name').value).to be_a(Proc) }
5
- # TODO: add 'should_behave_like base_field'
6
-
7
4
  subject(:field) { described_class.new('product') }
8
5
 
9
6
  describe '#dynamic_template' do
@@ -13,34 +10,94 @@ describe Chewy::Fields::Root do
13
10
  field.dynamic_template 'hello.*'
14
11
  field.dynamic_template(/hello/)
15
12
  field.dynamic_template(/hello.*/)
16
- field.dynamic_template template_42: { mapping: {}, match: '' }
13
+ field.dynamic_template template_42: {mapping: {}, match: ''}
17
14
  field.dynamic_template(/hello\..*/)
18
15
 
19
- expect(field.mappings_hash).to eq(product: { dynamic_templates: [
20
- { template_1: { mapping: { type: 'string' }, match: 'hello' } },
21
- { template_2: { mapping: {}, match_mapping_type: 'integer', match: 'hello*' } },
22
- { template_3: { mapping: {}, path_match: 'hello.*' } },
23
- { template_4: { mapping: {}, match: 'hello', match_pattern: 'regexp' } },
24
- { template_5: { mapping: {}, match: 'hello.*', match_pattern: 'regexp' } },
25
- { template_42: { mapping: {}, match: '' } },
26
- { template_7: { mapping: {}, path_match: 'hello\..*', match_pattern: 'regexp' } }
27
- ] })
16
+ expect(field.mappings_hash).to eq(product: {dynamic_templates: [
17
+ {template_1: {mapping: {type: 'string'}, match: 'hello'}},
18
+ {template_2: {mapping: {}, match_mapping_type: 'integer', match: 'hello*'}},
19
+ {template_3: {mapping: {}, path_match: 'hello.*'}},
20
+ {template_4: {mapping: {}, match: 'hello', match_pattern: 'regexp'}},
21
+ {template_5: {mapping: {}, match: 'hello.*', match_pattern: 'regexp'}},
22
+ {template_42: {mapping: {}, match: ''}},
23
+ {template_7: {mapping: {}, path_match: 'hello\..*', match_pattern: 'regexp'}}
24
+ ]})
28
25
  end
29
26
 
30
27
  context do
31
28
  subject(:field) do
32
29
  described_class.new('product', dynamic_templates: [
33
- { template_42: { mapping: {}, match: '' } }
30
+ {template_42: {mapping: {}, match: ''}}
34
31
  ])
35
32
  end
36
33
 
37
34
  specify do
38
35
  field.dynamic_template 'hello', type: 'string'
39
- expect(field.mappings_hash).to eq(product: { dynamic_templates: [
40
- { template_42: { mapping: {}, match: '' } },
41
- { template_1: { mapping: { type: 'string' }, match: 'hello' } }
42
- ] })
36
+ expect(field.mappings_hash).to eq(product: {dynamic_templates: [
37
+ {template_42: {mapping: {}, match: ''}},
38
+ {template_1: {mapping: {type: 'string'}, match: 'hello'}}
39
+ ]})
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#compose' do
45
+ context 'empty children', :orm do
46
+ before do
47
+ stub_model(:city)
48
+ stub_index(:places) do
49
+ define_type City
50
+ end
51
+ end
52
+
53
+ let(:city) { City.new(name: 'London', rating: 100) }
54
+
55
+ specify do
56
+ expect(PlacesIndex::City.send(:build_root).compose(city))
57
+ .to match(hash_including('name' => 'London', 'rating' => 100))
58
+ end
59
+ specify do
60
+ expect(PlacesIndex::City.send(:build_root).compose(city, fields: %i[name borogoves]))
61
+ .to eq('name' => 'London')
62
+ end
63
+ end
64
+
65
+ context 'has children' do
66
+ before do
67
+ stub_index(:places) do
68
+ define_type :city do
69
+ field :name, :rating
70
+ end
71
+ end
43
72
  end
73
+
74
+ let(:city) { double(name: 'London', rating: 100) }
75
+
76
+ specify do
77
+ expect(PlacesIndex::City.send(:build_root).compose(city))
78
+ .to eq('name' => 'London', 'rating' => 100)
79
+ end
80
+ specify do
81
+ expect(PlacesIndex::City.send(:build_root).compose(city, fields: %i[name borogoves]))
82
+ .to eq('name' => 'London')
83
+ end
84
+ end
85
+ end
86
+
87
+ describe '#child_hash' do
88
+ before do
89
+ stub_index(:places) do
90
+ define_type :city do
91
+ field :name, :rating
92
+ end
93
+ end
94
+ end
95
+
96
+ specify do
97
+ expect(PlacesIndex::City.send(:build_root).child_hash).to match(
98
+ name: an_instance_of(Chewy::Fields::Base).and(have_attributes(name: :name)),
99
+ rating: an_instance_of(Chewy::Fields::Base).and(have_attributes(name: :rating))
100
+ )
44
101
  end
45
102
  end
46
103
  end
@@ -23,7 +23,6 @@ describe 'Time fields' do
23
23
  let(:range) { (time - 1.minute)..(time + 1.minute) }
24
24
 
25
25
  specify { expect(PostsIndex.total).to eq(3) }
26
- specify { expect(PostsIndex.filter { published_at == o { range } }.count).to eq(1) }
27
- specify { expect(PostsIndex.filter { published_at == o { range.min.utc..(range.max + 1.hour).utc } }.count).to eq(2) }
28
- specify { expect(PostsIndex.filter { published_at == o { [range.min..range.max] } }.count).to eq(1) }
26
+ specify { expect(PostsIndex.filter(range: {published_at: {gte: range.min, lte: range.max}}).size).to eq(1) }
27
+ specify { expect(PostsIndex.filter(range: {published_at: {gt: range.min.utc, lt: (range.max + 1.hour).utc}}).size).to eq(2) }
29
28
  end
@@ -35,7 +35,7 @@ describe Chewy::Index::Actions do
35
35
 
36
36
  context do
37
37
  before { DummiesIndex.create '2014' }
38
- specify { expect(DummiesIndex.indexes).to match_array(%w(dummies_2013 dummies_2014)) }
38
+ specify { expect(DummiesIndex.indexes).to match_array(%w[dummies_2013 dummies_2014]) }
39
39
  end
40
40
  end
41
41
 
@@ -55,11 +55,6 @@ describe Chewy::Index::Actions do
55
55
  context do
56
56
  before { DummiesIndex.create }
57
57
  specify do
58
- skip_on_version_gte('2.0', 'format of exception changed in 2.x')
59
- expect { DummiesIndex.create! }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/\[\[dummies\] already exists\]/)
60
- end
61
- specify do
62
- skip_on_version_lt('2.0', 'format of exception was changed')
63
58
  expect { DummiesIndex.create! }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/index_already_exists_exception.*dummies/)
64
59
  end
65
60
  specify { expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/Invalid alias name \[dummies\]/) }
@@ -72,18 +67,13 @@ describe Chewy::Index::Actions do
72
67
  specify { expect(DummiesIndex.aliases).to eq([]) }
73
68
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
74
69
  specify do
75
- skip_on_version_gte('2.0', 'format of exception changed in 2.x')
76
- expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/\[\[dummies_2013\] already exists\]/)
77
- end
78
- specify do
79
- skip_on_version_lt('2.0', 'format of exception was changed')
80
70
  expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/index_already_exists_exception.*dummies_2013/)
81
71
  end
82
72
  specify { expect(DummiesIndex.create!('2014')['acknowledged']).to eq(true) }
83
73
 
84
74
  context do
85
75
  before { DummiesIndex.create! '2014' }
86
- specify { expect(DummiesIndex.indexes).to match_array(%w(dummies_2013 dummies_2014)) }
76
+ specify { expect(DummiesIndex.indexes).to match_array(%w[dummies_2013 dummies_2014]) }
87
77
  end
88
78
  end
89
79
 
@@ -336,82 +326,222 @@ describe Chewy::Index::Actions do
336
326
  end
337
327
  end
338
328
 
339
- before { City.create!(id: 1, name: 'Moscow') }
340
-
341
- specify { expect(CitiesIndex.reset!).to eq(true) }
342
- specify { expect(CitiesIndex.reset!('2013')).to eq(true) }
343
-
344
329
  context do
345
- before { CitiesIndex.reset! }
330
+ before { City.create!(id: 1, name: 'Moscow') }
346
331
 
347
- specify { expect(CitiesIndex.all).to have(1).item }
348
- specify { expect(CitiesIndex.aliases).to eq([]) }
349
- specify { expect(CitiesIndex.indexes).to eq([]) }
332
+ specify { expect(CitiesIndex.reset!).to eq(true) }
333
+ specify { expect(CitiesIndex.reset!('2013')).to eq(true) }
350
334
 
351
335
  context do
352
- before { CitiesIndex.reset!('2013') }
336
+ before { CitiesIndex.reset! }
353
337
 
354
338
  specify { expect(CitiesIndex.all).to have(1).item }
355
339
  specify { expect(CitiesIndex.aliases).to eq([]) }
356
- specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
340
+ specify { expect(CitiesIndex.indexes).to eq([]) }
341
+
342
+ context do
343
+ before { CitiesIndex.reset!('2013') }
344
+
345
+ specify { expect(CitiesIndex.all).to have(1).item }
346
+ specify { expect(CitiesIndex.aliases).to eq([]) }
347
+ specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
348
+ end
349
+
350
+ context do
351
+ before { CitiesIndex.reset! }
352
+
353
+ specify { expect(CitiesIndex.all).to have(1).item }
354
+ specify { expect(CitiesIndex.aliases).to eq([]) }
355
+ specify { expect(CitiesIndex.indexes).to eq([]) }
356
+ end
357
357
  end
358
358
 
359
359
  context do
360
- before { CitiesIndex.reset! }
360
+ before { CitiesIndex.reset!('2013') }
361
361
 
362
362
  specify { expect(CitiesIndex.all).to have(1).item }
363
363
  specify { expect(CitiesIndex.aliases).to eq([]) }
364
- specify { expect(CitiesIndex.indexes).to eq([]) }
364
+ specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
365
+
366
+ context do
367
+ before { CitiesIndex.reset!('2014') }
368
+
369
+ specify { expect(CitiesIndex.all).to have(1).item }
370
+ specify { expect(CitiesIndex.aliases).to eq([]) }
371
+ specify { expect(CitiesIndex.indexes).to eq(['cities_2014']) }
372
+ specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
373
+ end
374
+
375
+ context do
376
+ before { CitiesIndex.reset! }
377
+
378
+ specify { expect(CitiesIndex.all).to have(1).item }
379
+ specify { expect(CitiesIndex.aliases).to eq([]) }
380
+ specify { expect(CitiesIndex.indexes).to eq([]) }
381
+ specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
382
+ end
365
383
  end
366
384
  end
367
385
 
368
- context do
386
+ context 'reset_disable_refresh_interval' do
387
+ let(:suffix) { Time.now.to_i }
388
+ let(:name) { CitiesIndex.index_name(suffix: suffix) }
389
+ let(:before_import_body) do
390
+ {
391
+ index: {refresh_interval: -1}
392
+ }
393
+ end
394
+ let(:after_import_body) do
395
+ {
396
+ index: {refresh_interval: '1s'}
397
+ }
398
+ end
399
+
369
400
  before { CitiesIndex.reset!('2013') }
401
+ before { allow(Chewy).to receive(:reset_disable_refresh_interval).and_return(reset_disable_refresh_interval) }
402
+
403
+ context 'activated' do
404
+ let(:reset_disable_refresh_interval) { true }
405
+ specify do
406
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: before_import_body).once
407
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: after_import_body).once
408
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: false).and_call_original
409
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
410
+ end
370
411
 
371
- specify { expect(CitiesIndex.all).to have(1).item }
372
- specify { expect(CitiesIndex.aliases).to eq([]) }
373
- specify { expect(CitiesIndex.indexes).to eq(['cities_2013']) }
412
+ context 'refresh_interval already defined' do
413
+ before do
414
+ stub_index(:cities) do
415
+ settings index: {refresh_interval: '2s'}
416
+ define_type City
417
+ end
418
+ end
374
419
 
375
- context do
376
- before { CitiesIndex.reset!('2014') }
420
+ let(:after_import_body) do
421
+ {
422
+ index: {refresh_interval: '2s'}
423
+ }
424
+ end
377
425
 
378
- specify { expect(CitiesIndex.all).to have(1).item }
379
- specify { expect(CitiesIndex.aliases).to eq([]) }
380
- specify { expect(CitiesIndex.indexes).to eq(['cities_2014']) }
381
- specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
426
+ specify do
427
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: before_import_body).once
428
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: after_import_body).once
429
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: false).and_call_original
430
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
431
+ end
432
+ end
382
433
  end
383
434
 
384
- context do
385
- before { CitiesIndex.reset! }
435
+ context 'not activated' do
436
+ let(:reset_disable_refresh_interval) { false }
437
+ specify do
438
+ expect(CitiesIndex.client.indices).not_to receive(:put_settings)
439
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: true).and_call_original
440
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
441
+ end
442
+ end
443
+ end
386
444
 
387
- specify { expect(CitiesIndex.all).to have(1).item }
388
- specify { expect(CitiesIndex.aliases).to eq([]) }
389
- specify { expect(CitiesIndex.indexes).to eq([]) }
390
- specify { expect(Chewy.client.indices.exists(index: 'cities_2013')).to eq(false) }
445
+ context 'reset_no_replicas' do
446
+ let(:suffix) { Time.now.to_i }
447
+ let(:name) { CitiesIndex.index_name(suffix: suffix) }
448
+ let(:before_import_body) do
449
+ {
450
+ index: {number_of_replicas: 0}
451
+ }
452
+ end
453
+ let(:after_import_body) do
454
+ {
455
+ index: {number_of_replicas: 0}
456
+ }
457
+ end
458
+
459
+ before { allow(Chewy).to receive(:reset_no_replicas).and_return(reset_no_replicas) }
460
+
461
+ context 'activated' do
462
+ let(:reset_no_replicas) { true }
463
+ specify do
464
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: before_import_body).once
465
+ expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: after_import_body).once
466
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: true).and_call_original
467
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
468
+ end
469
+ end
470
+
471
+ context 'not activated' do
472
+ let(:reset_no_replicas) { false }
473
+ specify do
474
+ expect(CitiesIndex.client.indices).not_to receive(:put_settings)
475
+ expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: true).and_call_original
476
+ expect(CitiesIndex.reset!(suffix)).to eq(true)
477
+ end
391
478
  end
392
479
  end
393
480
 
394
- context 'journaling' do
481
+ context 'applying journal' do
395
482
  before do
396
- begin
397
- Chewy.client.indices.delete(index: Chewy::Journal.index_name)
398
- rescue Elasticsearch::Transport::Transport::Errors::NotFound
399
- nil
483
+ stub_index(:cities) do
484
+ define_type City do
485
+ field :name, value: (lambda do
486
+ sleep(rating)
487
+ name
488
+ end)
489
+ end
400
490
  end
401
491
  end
402
492
 
493
+ let!(:cities) { Array.new(3) { |i| City.create!(id: i + 1, name: "Name#{i + 1}", rating: 1) } }
494
+
495
+ let(:parallel_update) do
496
+ Thread.new do
497
+ sleep(1.5)
498
+ cities.first.update(name: 'NewName1', rating: 0)
499
+ cities.last.update(name: 'NewName3', rating: 0)
500
+ CitiesIndex::City.import!([cities.first, cities.last], journal: true)
501
+ end
502
+ end
503
+
504
+ specify 'with journal application' do
505
+ parallel_update
506
+ CitiesIndex.reset!('suffix')
507
+ expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 NewName1], %w[2 Name2], %w[3 NewName3])
508
+ end
509
+
510
+ specify 'without journal application' do
511
+ parallel_update
512
+ CitiesIndex.reset!('suffix', apply_journal: false)
513
+ expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 Name1], %w[2 Name2], %w[3 Name3])
514
+ end
515
+ end
516
+
517
+ context 'journaling' do
518
+ before { City.create!(id: 1, name: 'Moscow') }
519
+
403
520
  specify do
404
521
  CitiesIndex.reset!
405
-
406
- expect(Chewy.client.indices.exists?(index: Chewy::Journal.index_name)).to eq false
522
+ expect(Chewy::Stash::Journal.count).to eq(0)
407
523
  end
408
524
 
409
525
  specify do
410
526
  CitiesIndex.reset! journal: true
527
+ expect(Chewy::Stash::Journal.count).to be > 0
528
+ end
529
+ end
411
530
 
412
- expect(Chewy.client.indices.exists?(index: Chewy::Journal.index_name)).to eq true
413
- expect(Chewy.client.count(index: Chewy::Journal.index_name)['count']).not_to eq 0
531
+ context 'other options' do
532
+ specify do
533
+ expect(CitiesIndex::City).to receive(:import).with(parallel: true, journal: false).once.and_return(true)
534
+ expect(CitiesIndex.reset!(parallel: true)).to eq(true)
535
+ end
536
+
537
+ specify do
538
+ expect(CitiesIndex::City).to receive(:import).with(suffix: 'suffix', parallel: true, journal: false, refresh: true).once.and_return(true)
539
+ expect(CitiesIndex.reset!('suffix', parallel: true)).to eq(true)
414
540
  end
415
541
  end
416
542
  end
543
+
544
+ describe '.journal' do
545
+ specify { expect(DummiesIndex.journal).to be_a(Chewy::Journal) }
546
+ end
417
547
  end
@@ -22,7 +22,7 @@ describe Chewy::Index::Aliases do
22
22
  context do
23
23
  before { DummiesIndex.create! '2013' }
24
24
  before { DummiesIndex.create! '2014' }
25
- specify { expect(DummiesIndex.indexes).to match_array(%w(dummies_2013 dummies_2014)) }
25
+ specify { expect(DummiesIndex.indexes).to match_array(%w[dummies_2013 dummies_2014]) }
26
26
  end
27
27
  end
28
28
 
@@ -38,7 +38,7 @@ describe Chewy::Index::Aliases do
38
38
  before { DummiesIndex.create! }
39
39
  before { Chewy.client.indices.put_alias index: 'dummies', name: 'dummies_2013' }
40
40
  before { Chewy.client.indices.put_alias index: 'dummies', name: 'dummies_2014' }
41
- specify { expect(DummiesIndex.aliases).to match_array(%w(dummies_2013 dummies_2014)) }
41
+ specify { expect(DummiesIndex.aliases).to match_array(%w[dummies_2013 dummies_2014]) }
42
42
  end
43
43
 
44
44
  context do
@@ -6,30 +6,59 @@ describe Chewy::Index::Settings do
6
6
  before { allow(Chewy).to receive_messages(repository: Chewy::Repository.send(:new)) }
7
7
 
8
8
  specify { expect(described_class.new.to_hash).to eq({}) }
9
- specify { expect(described_class.new(number_of_nodes: 3).to_hash).to eq(settings: { number_of_nodes: 3 }) }
9
+ specify { expect(described_class.new(number_of_nodes: 3).to_hash).to eq(settings: {number_of_nodes: 3}) }
10
10
  specify do
11
11
  expect(described_class.new(number_of_nodes: 3, analysis: {}).to_hash)
12
- .to eq(settings: { number_of_nodes: 3, analysis: {} })
12
+ .to eq(settings: {number_of_nodes: 3, analysis: {}})
13
13
  end
14
14
  specify do
15
- expect(described_class.new(number_of_nodes: 3, analysis: { filter: { filter1: {} } }).to_hash)
16
- .to eq(settings: { number_of_nodes: 3, analysis: { filter: { filter1: {} } } })
15
+ expect(described_class.new(number_of_nodes: 3, analysis: {filter: {filter1: {}}}).to_hash)
16
+ .to eq(settings: {number_of_nodes: 3, analysis: {filter: {filter1: {}}}})
17
17
  end
18
18
  specify do
19
- expect(described_class.new(number_of_nodes: 3, analysis: { analyzer: { analyzer1: {} } }).to_hash)
20
- .to eq(settings: { number_of_nodes: 3, analysis: { analyzer: { analyzer1: {} } } })
19
+ expect(described_class.new(number_of_nodes: 3, analysis: {analyzer: {analyzer1: {}}}).to_hash)
20
+ .to eq(settings: {number_of_nodes: 3, analysis: {analyzer: {analyzer1: {}}}})
21
21
  end
22
22
  specify do
23
23
  expect(described_class.new(number_of_nodes: 3, analysis: {
24
- analyzer: { analyzer1: { tokenizer: 'tokenizer1', filter: %w(filter1 filter2) } }
25
- }).to_hash)
26
- .to eq(settings: { number_of_nodes: 3, analysis: {
27
- analyzer: { analyzer1: { tokenizer: 'tokenizer1', filter: %w(filter1 filter2) } }
28
- } })
24
+ analyzer: {analyzer1: {tokenizer: 'tokenizer1', filter: %w[filter1 filter2]}}
25
+ }).to_hash)
26
+ .to eq(settings: {number_of_nodes: 3, analysis: {
27
+ analyzer: {analyzer1: {tokenizer: 'tokenizer1', filter: %w[filter1 filter2]}}
28
+ }})
29
29
  end
30
30
  specify do
31
- expect(described_class.new(number_of_nodes: 3, analysis: { analyser: ['analyzer1'] }).to_hash)
32
- .to eq(settings: { number_of_nodes: 3, analysis: {} })
31
+ expect(described_class.new(number_of_nodes: 3, analysis: {analyzer: ['analyzer1']}).to_hash)
32
+ .to eq(settings: {number_of_nodes: 3, analysis: {}})
33
+ end
34
+ specify do
35
+ expect(described_class.new(number_of_nodes: 3, analysis: {analyzer: {analyzer1: {}}, normalizer: {}}).to_hash)
36
+ .to eq(settings: {number_of_nodes: 3, analysis: {analyzer: {analyzer1: {}}, normalizer: {}}})
37
+ end
38
+ specify do
39
+ expect(described_class.new(number_of_nodes: 3, analysis: {analyzer: ['analyzer1'], normalizer: {}}).to_hash)
40
+ .to eq(settings: {number_of_nodes: 3, analysis: {normalizer: {}}})
41
+ end
42
+
43
+ specify do
44
+ stub_const('Synonyms', Class.new do
45
+ def self.synonyms
46
+ ['kaftan => dress']
47
+ end
48
+ end)
49
+ expect(
50
+ described_class.new do
51
+ {
52
+ analysis: {filter: {synonym: {type: 'synonym', synonyms: Synonyms.synonyms}}}
53
+ }
54
+ end.to_hash
55
+ ).to eq(settings: {
56
+ analysis: {filter: {
57
+ synonym: {
58
+ type: 'synonym', synonyms: ['kaftan => dress']
59
+ }
60
+ }}
61
+ })
33
62
  end
34
63
 
35
64
  context do
@@ -37,12 +66,12 @@ describe Chewy::Index::Settings do
37
66
 
38
67
  specify do
39
68
  expect(described_class.new(number_of_nodes: 3, analysis: {
40
- analyzer: { analyzer1: { tokenizer: 'tokenizer1', filter: %w(filter1 filter2) } }
41
- }).to_hash)
42
- .to eq(settings: { number_of_nodes: 3, analysis: {
43
- analyzer: { analyzer1: { tokenizer: 'tokenizer1', filter: %w(filter1 filter2) } },
44
- tokenizer: { tokenizer1: { options: 42 } }
45
- } })
69
+ analyzer: {analyzer1: {tokenizer: 'tokenizer1', filter: %w[filter1 filter2]}}
70
+ }).to_hash)
71
+ .to eq(settings: {number_of_nodes: 3, analysis: {
72
+ analyzer: {analyzer1: {tokenizer: 'tokenizer1', filter: %w[filter1 filter2]}},
73
+ tokenizer: {tokenizer1: {options: 42}}
74
+ }})
46
75
  end
47
76
  end
48
77
 
@@ -55,13 +84,13 @@ describe Chewy::Index::Settings do
55
84
 
56
85
  specify do
57
86
  expect(described_class.new(number_of_nodes: 3, analysis: {
58
- analyzer: { analyzer1: { tokenizer: 'tokenizer1', filter: %w(filter1 filter2) } },
59
- filter: ['filter3', { filter4: { options: 45 } }]
60
- }).to_hash)
61
- .to eq(settings: { number_of_nodes: 3, analysis: {
62
- analyzer: { analyzer1: { tokenizer: 'tokenizer1', filter: %w(filter1 filter2) } },
63
- filter: { filter2: { options: 42 }, filter3: { options: 43 }, filter4: { options: 45 } }
64
- } })
87
+ analyzer: {analyzer1: {tokenizer: 'tokenizer1', filter: %w[filter1 filter2]}},
88
+ filter: ['filter3', {filter4: {options: 45}}]
89
+ }).to_hash)
90
+ .to eq(settings: {number_of_nodes: 3, analysis: {
91
+ analyzer: {analyzer1: {tokenizer: 'tokenizer1', filter: %w[filter1 filter2]}},
92
+ filter: {filter2: {options: 42}, filter3: {options: 43}, filter4: {options: 45}}
93
+ }})
65
94
  end
66
95
  end
67
96
 
@@ -73,31 +102,31 @@ describe Chewy::Index::Settings do
73
102
 
74
103
  specify do
75
104
  expect(described_class.new(number_of_nodes: 3, analysis: {
76
- analyzer: ['analyzer1', { analyzer2: { options: 44 } }]
77
- }).to_hash)
78
- .to eq(settings: { number_of_nodes: 3, analysis: {
79
- analyzer: { analyzer1: { options: 42, tokenizer: 'tokenizer1' }, analyzer2: { options: 44 } },
80
- tokenizer: { tokenizer1: { options: 43 } }
81
- } })
105
+ analyzer: ['analyzer1', {analyzer2: {options: 44}}]
106
+ }).to_hash)
107
+ .to eq(settings: {number_of_nodes: 3, analysis: {
108
+ analyzer: {analyzer1: {options: 42, tokenizer: 'tokenizer1'}, analyzer2: {options: 44}},
109
+ tokenizer: {tokenizer1: {options: 43}}
110
+ }})
82
111
  end
83
112
  end
84
113
 
85
114
  context ':index' do
86
115
  specify do
87
- expect(described_class.new(index: { number_of_shards: 3 }).to_hash)
88
- .to eq(settings: { index: { number_of_shards: 3 } })
116
+ expect(described_class.new(index: {number_of_shards: 3}).to_hash)
117
+ .to eq(settings: {index: {number_of_shards: 3}})
89
118
  end
90
119
 
91
120
  context do
92
- before { allow(Chewy).to receive_messages(configuration: { index: { number_of_shards: 7, number_of_replicas: 2 } }) }
121
+ before { allow(Chewy).to receive_messages(configuration: {index: {number_of_shards: 7, number_of_replicas: 2}}) }
93
122
 
94
123
  specify do
95
124
  expect(described_class.new.to_hash)
96
- .to eq(settings: { index: { number_of_shards: 7, number_of_replicas: 2 } })
125
+ .to eq(settings: {index: {number_of_shards: 7, number_of_replicas: 2}})
97
126
  end
98
127
  specify do
99
- expect(described_class.new(index: { number_of_shards: 3 }).to_hash)
100
- .to eq(settings: { index: { number_of_shards: 3, number_of_replicas: 2 } })
128
+ expect(described_class.new(index: {number_of_shards: 3}).to_hash)
129
+ .to eq(settings: {index: {number_of_shards: 3, number_of_replicas: 2}})
101
130
  end
102
131
  end
103
132
  end