chewy 0.10.1 → 7.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) hide show
  1. checksums.yaml +5 -5
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.github/workflows/ruby.yml +74 -0
  7. data/.rubocop.yml +28 -23
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +480 -298
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -5
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +571 -333
  16. data/chewy.gemspec +12 -15
  17. data/gemfiles/rails.5.2.activerecord.gemfile +11 -0
  18. data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
  19. data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
  20. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  21. data/lib/chewy/config.rb +48 -77
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +88 -16
  24. data/lib/chewy/fields/root.rb +15 -21
  25. data/lib/chewy/index/actions.rb +67 -38
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +11 -12
  28. data/lib/chewy/{type → index}/adapter/object.rb +28 -32
  29. data/lib/chewy/{type → index}/adapter/orm.rb +26 -24
  30. data/lib/chewy/index/aliases.rb +14 -5
  31. data/lib/chewy/index/crutch.rb +40 -0
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +10 -9
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +19 -18
  36. data/lib/chewy/{type → index}/import.rb +82 -36
  37. data/lib/chewy/{type → index}/mapping.rb +63 -62
  38. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  39. data/lib/chewy/index/observe/callback.rb +34 -0
  40. data/lib/chewy/index/observe.rb +17 -0
  41. data/lib/chewy/index/settings.rb +2 -0
  42. data/lib/chewy/index/specification.rb +13 -10
  43. data/lib/chewy/{type → index}/syncer.rb +62 -63
  44. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  45. data/lib/chewy/{type → index}/wrapper.rb +16 -6
  46. data/lib/chewy/index.rb +68 -93
  47. data/lib/chewy/journal.rb +25 -14
  48. data/lib/chewy/minitest/helpers.rb +91 -18
  49. data/lib/chewy/minitest/search_index_receiver.rb +29 -33
  50. data/lib/chewy/multi_search.rb +62 -0
  51. data/lib/chewy/railtie.rb +8 -24
  52. data/lib/chewy/rake_helper.rb +141 -112
  53. data/lib/chewy/rspec/build_query.rb +12 -0
  54. data/lib/chewy/rspec/helpers.rb +55 -0
  55. data/lib/chewy/rspec/update_index.rb +58 -49
  56. data/lib/chewy/rspec.rb +2 -0
  57. data/lib/chewy/runtime.rb +1 -1
  58. data/lib/chewy/search/loader.rb +19 -41
  59. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  60. data/lib/chewy/search/parameters/collapse.rb +16 -0
  61. data/lib/chewy/search/parameters/concerns/query_storage.rb +6 -5
  62. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  63. data/lib/chewy/search/parameters/indices.rb +78 -0
  64. data/lib/chewy/search/parameters/none.rb +1 -3
  65. data/lib/chewy/search/parameters/order.rb +6 -19
  66. data/lib/chewy/search/parameters/source.rb +5 -1
  67. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  68. data/lib/chewy/search/parameters.rb +28 -8
  69. data/lib/chewy/search/query_proxy.rb +9 -2
  70. data/lib/chewy/search/request.rb +207 -157
  71. data/lib/chewy/search/response.rb +5 -5
  72. data/lib/chewy/search/scoping.rb +7 -8
  73. data/lib/chewy/search/scrolling.rb +14 -13
  74. data/lib/chewy/search.rb +7 -26
  75. data/lib/chewy/stash.rb +27 -29
  76. data/lib/chewy/strategy/active_job.rb +2 -2
  77. data/lib/chewy/strategy/atomic.rb +1 -1
  78. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  79. data/lib/chewy/strategy/base.rb +10 -0
  80. data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +148 -0
  81. data/lib/chewy/strategy/delayed_sidekiq/worker.rb +52 -0
  82. data/lib/chewy/strategy/delayed_sidekiq.rb +17 -0
  83. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  84. data/lib/chewy/strategy/sidekiq.rb +3 -2
  85. data/lib/chewy/strategy.rb +6 -19
  86. data/lib/chewy/version.rb +1 -1
  87. data/lib/chewy.rb +37 -80
  88. data/lib/generators/chewy/install_generator.rb +1 -1
  89. data/lib/tasks/chewy.rake +26 -32
  90. data/migration_guide.md +56 -0
  91. data/spec/chewy/config_spec.rb +27 -57
  92. data/spec/chewy/fields/base_spec.rb +457 -174
  93. data/spec/chewy/fields/root_spec.rb +24 -32
  94. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  95. data/spec/chewy/index/actions_spec.rb +425 -60
  96. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  97. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  98. data/spec/chewy/index/aliases_spec.rb +3 -3
  99. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  100. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  101. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +22 -30
  102. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  103. data/spec/chewy/{type → index}/import_spec.rb +154 -95
  104. data/spec/chewy/index/mapping_spec.rb +135 -0
  105. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  106. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  107. data/spec/chewy/index/observe_spec.rb +143 -0
  108. data/spec/chewy/index/settings_spec.rb +3 -1
  109. data/spec/chewy/index/specification_spec.rb +32 -33
  110. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  111. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  112. data/spec/chewy/index/wrapper_spec.rb +100 -0
  113. data/spec/chewy/index_spec.rb +99 -114
  114. data/spec/chewy/journal_spec.rb +56 -101
  115. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  116. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  117. data/spec/chewy/multi_search_spec.rb +84 -0
  118. data/spec/chewy/rake_helper_spec.rb +325 -101
  119. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  120. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  121. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  122. data/spec/chewy/runtime_spec.rb +2 -2
  123. data/spec/chewy/search/loader_spec.rb +19 -53
  124. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  125. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  126. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  127. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  128. data/spec/chewy/search/parameters/indices_spec.rb +99 -0
  129. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  130. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  131. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  132. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  133. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  134. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  135. data/spec/chewy/search/parameters_spec.rb +39 -8
  136. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  137. data/spec/chewy/search/request_spec.rb +360 -149
  138. data/spec/chewy/search/response_spec.rb +35 -25
  139. data/spec/chewy/search/scrolling_spec.rb +28 -26
  140. data/spec/chewy/search_spec.rb +73 -53
  141. data/spec/chewy/stash_spec.rb +16 -26
  142. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  143. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  144. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  145. data/spec/chewy/strategy/delayed_sidekiq_spec.rb +190 -0
  146. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  147. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  148. data/spec/chewy/strategy_spec.rb +19 -15
  149. data/spec/chewy_spec.rb +17 -110
  150. data/spec/spec_helper.rb +7 -22
  151. data/spec/support/active_record.rb +43 -5
  152. metadata +123 -198
  153. data/.travis.yml +0 -53
  154. data/Appraisals +0 -79
  155. data/LEGACY_DSL.md +0 -497
  156. data/gemfiles/rails.4.0.activerecord.gemfile +0 -14
  157. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  158. data/gemfiles/rails.4.2.activerecord.gemfile +0 -15
  159. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -15
  160. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  161. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  162. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  163. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  164. data/gemfiles/sequel.4.45.gemfile +0 -11
  165. data/lib/chewy/backports/deep_dup.rb +0 -46
  166. data/lib/chewy/backports/duplicable.rb +0 -91
  167. data/lib/chewy/query/compose.rb +0 -68
  168. data/lib/chewy/query/criteria.rb +0 -191
  169. data/lib/chewy/query/filters.rb +0 -227
  170. data/lib/chewy/query/loading.rb +0 -111
  171. data/lib/chewy/query/nodes/and.rb +0 -25
  172. data/lib/chewy/query/nodes/base.rb +0 -17
  173. data/lib/chewy/query/nodes/bool.rb +0 -34
  174. data/lib/chewy/query/nodes/equal.rb +0 -34
  175. data/lib/chewy/query/nodes/exists.rb +0 -20
  176. data/lib/chewy/query/nodes/expr.rb +0 -28
  177. data/lib/chewy/query/nodes/field.rb +0 -110
  178. data/lib/chewy/query/nodes/has_child.rb +0 -15
  179. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  180. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  181. data/lib/chewy/query/nodes/match_all.rb +0 -11
  182. data/lib/chewy/query/nodes/missing.rb +0 -20
  183. data/lib/chewy/query/nodes/not.rb +0 -25
  184. data/lib/chewy/query/nodes/or.rb +0 -25
  185. data/lib/chewy/query/nodes/prefix.rb +0 -19
  186. data/lib/chewy/query/nodes/query.rb +0 -20
  187. data/lib/chewy/query/nodes/range.rb +0 -63
  188. data/lib/chewy/query/nodes/raw.rb +0 -15
  189. data/lib/chewy/query/nodes/regexp.rb +0 -35
  190. data/lib/chewy/query/nodes/script.rb +0 -20
  191. data/lib/chewy/query/pagination.rb +0 -25
  192. data/lib/chewy/query.rb +0 -1098
  193. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  194. data/lib/chewy/search/parameters/types.rb +0 -20
  195. data/lib/chewy/strategy/resque.rb +0 -27
  196. data/lib/chewy/strategy/shoryuken.rb +0 -40
  197. data/lib/chewy/type/actions.rb +0 -43
  198. data/lib/chewy/type/adapter/mongoid.rb +0 -69
  199. data/lib/chewy/type/adapter/sequel.rb +0 -95
  200. data/lib/chewy/type/crutch.rb +0 -32
  201. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  202. data/lib/chewy/type/observe.rb +0 -78
  203. data/lib/chewy/type.rb +0 -117
  204. data/lib/sequel/plugins/chewy_observe.rb +0 -78
  205. data/spec/chewy/query/criteria_spec.rb +0 -700
  206. data/spec/chewy/query/filters_spec.rb +0 -201
  207. data/spec/chewy/query/loading_spec.rb +0 -124
  208. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  209. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  210. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  211. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  212. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  213. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  214. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  215. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  216. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  217. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  218. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  219. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  220. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  221. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  222. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  223. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  224. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  225. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  226. data/spec/chewy/query/pagination_spec.rb +0 -39
  227. data/spec/chewy/query_spec.rb +0 -636
  228. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  229. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  230. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
  231. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  232. data/spec/chewy/strategy/resque_spec.rb +0 -46
  233. data/spec/chewy/strategy/shoryuken_spec.rb +0 -64
  234. data/spec/chewy/type/actions_spec.rb +0 -50
  235. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  236. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  237. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  238. data/spec/chewy/type/mapping_spec.rb +0 -142
  239. data/spec/chewy/type/observe_spec.rb +0 -137
  240. data/spec/chewy/type/wrapper_spec.rb +0 -98
  241. data/spec/chewy/type_spec.rb +0 -55
  242. data/spec/support/mongoid.rb +0 -93
  243. data/spec/support/sequel.rb +0 -80
@@ -1,636 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query do
4
- if Chewy::Runtime.version < '5.0'
5
- before { Chewy.massacre }
6
-
7
- before do
8
- stub_index(:products) do
9
- define_type :product do
10
- field :name, :age
11
- end
12
- define_type :city
13
- define_type :country
14
- end
15
- end
16
-
17
- subject { described_class.new(ProductsIndex) }
18
-
19
- context 'unexistent index' do
20
- specify { expect(subject.to_a).to eq([]) }
21
- end
22
-
23
- context 'integration' do
24
- let(:products) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
25
- let(:cities) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
26
- let(:countries) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
27
- before do
28
- ProductsIndex::Product.import!(products.map { |h| double(h) })
29
- ProductsIndex::City.import!(cities.map { |h| double(h) })
30
- ProductsIndex::Country.import!(countries.map { |h| double(h) })
31
- end
32
-
33
- specify { expect(subject.count).to eq(9) }
34
- specify { expect(subject.first._data).to be_a Hash }
35
- specify { expect(subject.limit(6).count).to eq(6) }
36
- specify { expect(subject.offset(6).count).to eq(3) }
37
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name).to eq('Name3') }
38
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name_highlight).to eq('<em>Name3</em>') }
39
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name']).to eq('Name3') }
40
- specify { expect(subject.types(:product).count).to eq(3) }
41
- specify { expect(subject.types(:product, :country).count).to eq(6) }
42
- specify { expect(subject.filter(term: {age: 10}).count).to eq(1) }
43
- specify { expect(subject.query(term: {age: 10}).count).to eq(1) }
44
- specify { expect(subject.order(nil).count).to eq(9) }
45
- specify { expect(subject.search_type(:count).count).to eq(0) }
46
- specify { expect(subject.search_type(:count).total).to eq(9) }
47
- end
48
-
49
- describe '#==' do
50
- let(:data) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
51
- before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
52
-
53
- specify { expect(subject.query(match: 'hello')).to eq(subject.query(match: 'hello')) }
54
- specify { expect(subject.query(match: 'hello')).not_to eq(subject.query(match: 'world')) }
55
- specify { expect(subject.limit(10)).to eq(subject.limit(10)) }
56
- specify { expect(subject.limit(10)).not_to eq(subject.limit(11)) }
57
- specify { expect(subject.limit(2)).to eq(subject.limit(2).to_a) }
58
- end
59
-
60
- describe '#query_mode' do
61
- specify { expect(subject.query_mode(:should)).to be_a described_class }
62
- specify { expect(subject.query_mode(:should)).not_to eq(subject) }
63
- specify { expect(subject.query_mode(:should).criteria.options).to include(query_mode: :should) }
64
- specify { expect { subject.query_mode(:should) }.not_to change { subject.criteria.options } }
65
- end
66
-
67
- describe '#filter_mode' do
68
- specify { expect(subject.filter_mode(:or)).to be_a described_class }
69
- specify { expect(subject.filter_mode(:or)).not_to eq(subject) }
70
- specify { expect(subject.filter_mode(:or).criteria.options).to include(filter_mode: :or) }
71
- specify { expect { subject.filter_mode(:or) }.not_to change { subject.criteria.options } }
72
- end
73
-
74
- describe '#post_filter_mode' do
75
- specify { expect(subject.post_filter_mode(:or)).to be_a described_class }
76
- specify { expect(subject.post_filter_mode(:or)).not_to eq(subject) }
77
- specify { expect(subject.post_filter_mode(:or).criteria.options).to include(post_filter_mode: :or) }
78
- specify { expect { subject.post_filter_mode(:or) }.not_to change { subject.criteria.options } }
79
- end
80
-
81
- describe '#boost_mode' do
82
- specify { expect(subject.boost_mode(:replace)).to be_a described_class }
83
- specify { expect(subject.boost_mode(:replace)).not_to eq(subject) }
84
- specify { expect(subject.boost_mode(:replace).criteria.options).to include(boost_mode: :replace) }
85
- specify { expect { subject.boost_mode(:replace) }.not_to change { subject.criteria.options } }
86
- end
87
-
88
- describe '#score_mode' do
89
- specify { expect(subject.score_mode(:first)).to be_a described_class }
90
- specify { expect(subject.score_mode(:first)).not_to eq(subject) }
91
- specify { expect(subject.score_mode(:first).criteria.options).to include(score_mode: :first) }
92
- specify { expect { subject.score_mode(:first) }.not_to change { subject.criteria.options } }
93
- end
94
-
95
- describe '#limit' do
96
- specify { expect(subject.limit(10)).to be_a described_class }
97
- specify { expect(subject.limit(10)).not_to eq(subject) }
98
- specify { expect(subject.limit(10).criteria.request_options).to include(size: 10) }
99
- specify { expect { subject.limit(10) }.not_to change { subject.criteria.request_options } }
100
- specify { expect(subject.limit { 20 / 2 }.criteria.request_body[:body]).to include(size: 10) }
101
- end
102
-
103
- describe '#offset' do
104
- specify { expect(subject.offset(10)).to be_a described_class }
105
- specify { expect(subject.offset(10)).not_to eq(subject) }
106
- specify { expect(subject.offset(10).criteria.request_options).to include(from: 10) }
107
- specify { expect { subject.offset(10) }.not_to change { subject.criteria.request_options } }
108
- specify { expect(subject.offset { 20 / 2 }.criteria.request_body[:body]).to include(from: 10) }
109
- end
110
-
111
- describe '#track_scores' do
112
- specify { expect(subject.track_scores(true)).to be_a described_class }
113
- specify { expect(subject.track_scores(true)).not_to eq(subject) }
114
- specify { expect(subject.track_scores(true).criteria.request_options).to include(track_scores: true) }
115
- specify { expect { subject.track_scores(true) }.not_to change { subject.criteria.request_options } }
116
- specify { expect(subject.track_scores(false).criteria.request_body[:body]).to include(track_scores: false) }
117
- end
118
-
119
- describe '#script_fields' do
120
- specify { expect(subject.script_fields(distance: 'test()')).to be_a described_class }
121
- specify { expect(subject.script_fields(distance: 'test()')).not_to eq(subject) }
122
- specify { expect(subject.script_fields(distance: 'test()').criteria.script_fields).to include(distance: 'test()') }
123
- specify { expect { subject.script_fields(distance: 'test()') }.not_to change { subject.criteria.script_fields } }
124
- end
125
-
126
- describe '#script_score' do
127
- specify { expect(subject.script_score('23')).to be_a described_class }
128
- specify { expect(subject.script_score('23')).not_to eq(subject) }
129
- specify { expect(subject.script_score('23').criteria.scores).to eq([{script_score: {script: '23'}}]) }
130
- specify { expect { subject.script_score('23') }.not_to change { subject.criteria.scores } }
131
- specify { expect(subject.script_score('23 * factor', params: {factor: 0.5}).criteria.scores).to eq([{script_score: {script: '23 * factor', params: {factor: 0.5}}}]) }
132
- end
133
-
134
- describe '#boost_factor' do
135
- specify { expect(subject.boost_factor('23')).to be_a described_class }
136
- specify { expect(subject.boost_factor('23')).not_to eq(subject) }
137
- specify { expect(subject.boost_factor('23').criteria.scores).to eq([{boost_factor: 23}]) }
138
- specify { expect { subject.boost_factor('23') }.not_to change { subject.criteria.scores } }
139
- specify { expect(subject.boost_factor('23', filter: {foo: :bar}).criteria.scores).to eq([{boost_factor: 23, filter: {foo: :bar}}]) }
140
- end
141
-
142
- describe '#weight' do
143
- specify { expect(subject.weight('23')).to be_a described_class }
144
- specify { expect(subject.weight('23')).not_to eq(subject) }
145
- specify { expect(subject.weight('23').criteria.scores).to eq([{weight: 23}]) }
146
- specify { expect { subject.weight('23') }.not_to change { subject.criteria.scores } }
147
- specify { expect(subject.weight('23', filter: {foo: :bar}).criteria.scores).to eq([{weight: 23, filter: {foo: :bar}}]) }
148
- end
149
-
150
- describe '#random_score' do
151
- specify { expect(subject.random_score('23')).to be_a described_class }
152
- specify { expect(subject.random_score('23')).not_to eq(subject) }
153
- specify { expect(subject.random_score('23').criteria.scores).to eq([{random_score: {seed: 23}}]) }
154
- specify { expect { subject.random_score('23') }.not_to change { subject.criteria.scores } }
155
- specify { expect(subject.random_score('23', filter: {foo: :bar}).criteria.scores).to eq([{random_score: {seed: 23}, filter: {foo: :bar}}]) }
156
- end
157
-
158
- describe '#field_value_score' do
159
- specify { expect(subject.field_value_factor(field: :boost)).to be_a described_class }
160
- specify { expect(subject.field_value_factor(field: :boost)).not_to eq(subject) }
161
- specify { expect(subject.field_value_factor(field: :boost).criteria.scores).to eq([{field_value_factor: {field: :boost}}]) }
162
- specify { expect { subject.field_value_factor(field: :boost) }.not_to change { subject.criteria.scores } }
163
- specify { expect(subject.field_value_factor({field: :boost}, filter: {foo: :bar}).criteria.scores).to eq([{field_value_factor: {field: :boost}, filter: {foo: :bar}}]) }
164
- end
165
-
166
- describe '#decay' do
167
- specify { expect(subject.decay(:gauss, :field)).to be_a described_class }
168
- specify { expect(subject.decay(:gauss, :field)).not_to eq(subject) }
169
- specify do
170
- expect(subject.decay(:gauss, :field).criteria.scores).to eq([{
171
- gauss: {
172
- field: {}
173
- }
174
- }])
175
- end
176
- specify { expect { subject.decay(:gauss, :field) }.not_to change { subject.criteria.scores } }
177
- specify do
178
- expect(subject.decay(:gauss, :field,
179
- origin: '11, 12',
180
- scale: '2km',
181
- offset: '5km',
182
- decay: 0.4,
183
- filter: {foo: :bar}).criteria.scores).to eq([
184
- {
185
- gauss: {
186
- field: {
187
- origin: '11, 12',
188
- scale: '2km',
189
- offset: '5km',
190
- decay: 0.4
191
- }
192
- },
193
- filter: {foo: :bar}
194
- }
195
- ])
196
- end
197
- end
198
-
199
- describe '#preference' do
200
- specify { expect(subject.preference(:_primary)).to be_a described_class }
201
- specify { expect(subject.preference(:_primary)).not_to eq(subject) }
202
- specify { expect(subject.preference(:_primary).criteria.search_options).to include(preference: :_primary) }
203
- specify { expect { subject.preference(:_primary) }.not_to change { subject.criteria.search_options } }
204
- specify { expect(subject.preference(:_primary).criteria.request_body).to include(preference: :_primary) }
205
- end
206
-
207
- describe '#aggregations' do
208
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).to be_a described_class }
209
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).not_to eq(subject) }
210
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'}).criteria.aggregations).to include(aggregation1: {field: 'hello'}) }
211
- specify { expect { subject.aggregations(aggregation1: {field: 'hello'}) }.not_to change { subject.criteria.aggregations } }
212
-
213
- context 'when requesting a named aggregation' do
214
- before do
215
- stub_index(:products) do
216
- define_type :product do
217
- root do
218
- field :name, 'surname'
219
- field :title, type: 'string' do
220
- field :subfield1
221
- end
222
- field 'price', type: 'float' do
223
- field :subfield2
224
- end
225
- agg :uniquely_named_agg do
226
- {min: {field: 'title.subfield1'}}
227
- end
228
- end
229
- end
230
- end
231
- end
232
- specify { expect(subject.aggregations(:uniquely_named_agg).criteria.aggregations).to include(uniquely_named_agg: {min: {field: 'title.subfield1'}}) }
233
-
234
- context 'when more than one aggregation of the same name exists' do
235
- before do
236
- stub_index(:products) do
237
- define_type :product do
238
- root do
239
- field :name, 'surname'
240
- field :title, type: 'string' do
241
- field :subfield1
242
- end
243
- field 'price', type: 'float' do
244
- field :subfield2
245
- end
246
- agg :uniquely_named_agg do
247
- {min: {field: 'title.subfield1'}}
248
- end
249
- agg :named_agg do
250
- {avg: {field: 'title.subfield1'}}
251
- end
252
- end
253
- end
254
- define_type :review do
255
- field :title, :body
256
- field :comments do
257
- field :message
258
- field :rating, type: 'long'
259
- end
260
- agg :named_agg do
261
- {avg: {field: 'comments.rating'}}
262
- end
263
- end
264
- end
265
- end
266
-
267
- it 'is the aggregation definition that was last defined' do
268
- expect(subject.aggregations(:named_agg).criteria.aggregations).to include(named_agg: {avg: {field: 'comments.rating'}})
269
- end
270
-
271
- context 'when the fully qualified aggregation name is provided' do
272
- specify do
273
- expect(subject
274
- .aggregations('products#product.named_agg')
275
- .criteria
276
- .aggregations)
277
- .to include('products#product.named_agg' => {avg: {field: 'title.subfield1'}})
278
- end
279
- end
280
- end
281
- end
282
-
283
- context 'results', :orm do
284
- before { stub_model(:city) }
285
- let(:cities) { Array.new(10) { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
286
-
287
- context do
288
- before do
289
- stub_index(:cities) do
290
- define_type :city do
291
- field :rating, type: 'integer'
292
- end
293
- end
294
- end
295
-
296
- before { CitiesIndex::City.import! cities }
297
- subject { described_class.new(CitiesIndex) }
298
-
299
- specify { expect(subject.aggregations).to eq({}) }
300
- specify do
301
- expect(subject.aggregations(ratings: {terms: {field: 'rating'}})
302
- .aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }).to eq([
303
- {'key' => 0, 'doc_count' => 4},
304
- {'key' => 1, 'doc_count' => 3},
305
- {'key' => 2, 'doc_count' => 3}
306
- ])
307
- end
308
- end
309
- end
310
- end
311
-
312
- describe '#suggest' do
313
- specify { subject.suggest(name1: {text: 'hello', term: {field: 'name'}}) }
314
- specify { expect(subject.suggest(name1: {text: 'hello'})).not_to eq(subject) }
315
- specify { expect(subject.suggest(name1: {text: 'hello'}).criteria.suggest).to include(name1: {text: 'hello'}) }
316
- specify { expect { subject.suggest(name1: {text: 'hello'}) }.not_to change { subject.criteria.suggest } }
317
-
318
- context 'results', :orm do
319
- before { stub_model(:city) }
320
- let(:cities) { Array.new(10) { |i| City.create! id: i + 1, name: "name#{i}" } }
321
-
322
- context do
323
- before do
324
- stub_index(:cities) do
325
- define_type :city do
326
- field :name
327
- end
328
- end
329
- end
330
-
331
- before { CitiesIndex::City.import! cities }
332
- subject { described_class.new(CitiesIndex) }
333
-
334
- specify { expect(subject.suggest).to eq({}) }
335
- specify do
336
- expect(subject.suggest(name: {text: 'name', term: {field: 'name'}}).suggest).to eq('name' => [
337
- {'text' => 'name', 'offset' => 0, 'length' => 4, 'options' => [
338
- {'text' => 'name0', 'score' => 0.75, 'freq' => 1},
339
- {'text' => 'name1', 'score' => 0.75, 'freq' => 1},
340
- {'text' => 'name2', 'score' => 0.75, 'freq' => 1},
341
- {'text' => 'name3', 'score' => 0.75, 'freq' => 1},
342
- {'text' => 'name4', 'score' => 0.75, 'freq' => 1}
343
- ]}
344
- ])
345
- end
346
- end
347
- end
348
- end
349
-
350
- describe '#delete_all' do
351
- let(:products) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
352
- let(:cities) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
353
- let(:countries) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
354
-
355
- before do
356
- ProductsIndex::Product.import!(products.map { |h| double(h) })
357
- ProductsIndex::City.import!(cities.map { |h| double(h) })
358
- ProductsIndex::Country.import!(countries.map { |h| double(h) })
359
- end
360
-
361
- specify do
362
- expect do
363
- subject.query(match: {name: 'name3'}).delete_all
364
- Chewy.client.indices.refresh(index: 'products')
365
- end.to change { described_class.new(ProductsIndex).total }.from(9).to(8)
366
- end
367
- specify do
368
- expect do
369
- subject.filter { age == [10, 20] }.delete_all
370
- Chewy.client.indices.refresh(index: 'products')
371
- end.to change { described_class.new(ProductsIndex).total_count }.from(9).to(7)
372
- end
373
- specify do
374
- expect do
375
- subject.types(:product).delete_all
376
- Chewy.client.indices.refresh(index: 'products')
377
- end.to change { described_class.new(ProductsIndex::Product).total_entries }.from(3).to(0)
378
- end
379
- specify do
380
- expect do
381
- subject.delete_all
382
- Chewy.client.indices.refresh(index: 'products')
383
- end.to change { described_class.new(ProductsIndex).total }.from(9).to(0)
384
- end
385
- specify do
386
- expect do
387
- described_class.new(ProductsIndex::City).delete_all
388
- Chewy.client.indices.refresh(index: 'products')
389
- end.to change { described_class.new(ProductsIndex).total }.from(9).to(6)
390
- end
391
- end
392
-
393
- describe '#find' do
394
- let(:products) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
395
- let(:cities) { Array.new(1) { {id: '4'}.stringify_keys! } }
396
- let(:countries) { Array.new(1) { {id: '4'}.stringify_keys! } }
397
-
398
- before do
399
- ProductsIndex::Product.import!(products.map { |h| double(h) })
400
- ProductsIndex::City.import!(cities.map { |h| double(h) })
401
- ProductsIndex::Country.import!(countries.map { |h| double(h) })
402
- end
403
-
404
- specify { expect(subject.find(1)).to be_a(ProductsIndex::Product) }
405
- specify { expect(subject.find(1).id).to eq('1') }
406
- specify { expect(subject.find(4).id).to eq('4') }
407
- specify { expect(subject.find([1]).map(&:id)).to match_array(%w[1]) }
408
- specify { expect(subject.find([4]).map(&:id)).to match_array(%w[4 4]) }
409
- specify { expect(subject.find([1, 3]).map(&:id)).to match_array(%w[1 3]) }
410
- specify { expect(subject.find(1, 3).map(&:id)).to match_array(%w[1 3]) }
411
- specify { expect(subject.find(1, 10).map(&:id)).to match_array(%w[1]) }
412
-
413
- specify { expect { subject.find(10) }.to raise_error Chewy::DocumentNotFound }
414
- specify { expect { subject.find([10]) }.to raise_error Chewy::DocumentNotFound }
415
- specify { expect { subject.find([10, 20]) }.to raise_error Chewy::DocumentNotFound }
416
- end
417
-
418
- describe '#exists?' do
419
- let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next} } }
420
-
421
- before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
422
-
423
- specify { expect(subject.exists?).to eq true }
424
- specify { expect(subject.limit(5).exists?).to eq true }
425
- specify { expect(subject.filter(range: {age: {gt: 20}}).limit(3).exists?).to eq true }
426
- specify { expect(subject.filter(range: {age: {lt: 0}}).exists?).to eq false }
427
- end
428
-
429
- describe '#unlimited' do
430
- let(:data_length) { 10 }
431
- let(:data) { Array.new(data_length) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next} } }
432
-
433
- before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
434
-
435
- specify { expect(subject.unlimited.count).to eq data_length }
436
- specify { expect(subject.offset(5).unlimited.count).to eq data_length }
437
- specify { expect(subject.limit(1).unlimited.count).to eq data_length }
438
- specify { expect(subject.unlimited.limit(1).count).to eq 1 }
439
- end
440
-
441
- describe '#none' do
442
- specify { expect(subject.none).to be_a described_class }
443
- specify { expect(subject.none).not_to eq(subject) }
444
- specify { expect(subject.none.criteria).to be_none }
445
-
446
- context do
447
- before { expect_any_instance_of(described_class).not_to receive(:_response) }
448
-
449
- specify { expect(subject.none.to_a).to eq([]) }
450
- specify { expect(subject.query(match: 'hello').none.to_a).to eq([]) }
451
- specify { expect(subject.none.query(match: 'hello').to_a).to eq([]) }
452
- end
453
- end
454
-
455
- describe '#strategy' do
456
- specify { expect(subject.strategy('query_first')).to be_a described_class }
457
- specify { expect(subject.strategy('query_first')).not_to eq(subject) }
458
- specify { expect(subject.strategy('query_first').criteria.options).to include(strategy: 'query_first') }
459
- specify { expect { subject.strategy('query_first') }.not_to change { subject.criteria.options } }
460
- end
461
-
462
- describe '#query' do
463
- specify { expect(subject.query(match: 'hello')).to be_a described_class }
464
- specify { expect(subject.query(match: 'hello')).not_to eq(subject) }
465
- specify { expect(subject.query(match: 'hello').criteria.queries).to include(match: 'hello') }
466
- specify { expect { subject.query(match: 'hello') }.not_to change { subject.criteria.queries } }
467
- end
468
-
469
- describe '#filter' do
470
- specify { expect(subject.filter(term: {field: 'hello'})).to be_a described_class }
471
- specify { expect(subject.filter(term: {field: 'hello'})).not_to eq(subject) }
472
- specify { expect { subject.filter(term: {field: 'hello'}) }.not_to change { subject.criteria.filters } }
473
- specify do
474
- expect(subject.filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.filters)
475
- .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}])
476
- end
477
-
478
- specify { expect { subject.filter { name == 'John' } }.not_to change { subject.criteria.filters } }
479
- specify { expect(subject.filter { name == 'John' }.criteria.filters).to eq([{term: {'name' => 'John'}}]) }
480
- end
481
-
482
- describe '#post_filter' do
483
- specify { expect(subject.post_filter(term: {field: 'hello'})).to be_a described_class }
484
- specify { expect(subject.post_filter(term: {field: 'hello'})).not_to eq(subject) }
485
- specify { expect { subject.post_filter(term: {field: 'hello'}) }.not_to change { subject.criteria.post_filters } }
486
- specify do
487
- expect(subject.post_filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.post_filters)
488
- .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}])
489
- end
490
-
491
- specify { expect { subject.post_filter { name == 'John' } }.not_to change { subject.criteria.post_filters } }
492
- specify { expect(subject.post_filter { name == 'John' }.criteria.post_filters).to eq([{term: {'name' => 'John'}}]) }
493
- end
494
-
495
- describe '#order' do
496
- specify { expect(subject.order(field: 'hello')).to be_a described_class }
497
- specify { expect(subject.order(field: 'hello')).not_to eq(subject) }
498
- specify { expect { subject.order(field: 'hello') }.not_to change { subject.criteria.sort } }
499
-
500
- specify { expect(subject.order(:field).criteria.sort).to eq([:field]) }
501
- specify { expect(subject.order(%i[field1 field2]).criteria.sort).to eq(%i[field1 field2]) }
502
- specify { expect(subject.order(field: :asc).criteria.sort).to eq([{field: :asc}]) }
503
- specify { expect(subject.order(field1: :asc, field2: :desc).criteria.sort).to eq([{field1: :asc}, {field2: :desc}]) }
504
- specify { expect(subject.order(field1: {order: :asc}, field2: :desc).order([:field3], :field4).criteria.sort).to eq([{field1: {order: :asc}}, {field2: :desc}, :field3, :field4]) }
505
- end
506
-
507
- describe '#reorder' do
508
- specify { expect(subject.reorder(field: 'hello')).to be_a described_class }
509
- specify { expect(subject.reorder(field: 'hello')).not_to eq(subject) }
510
- specify { expect { subject.reorder(field: 'hello') }.not_to change { subject.criteria.sort } }
511
-
512
- specify { expect(subject.order(:field1).reorder(:field2).criteria.sort).to eq([:field2]) }
513
- specify { expect(subject.order(:field1).reorder(:field2).order(:field3).criteria.sort).to eq(%i[field2 field3]) }
514
- specify { expect(subject.order(:field1).reorder(:field2).reorder(:field3).criteria.sort).to eq([:field3]) }
515
- end
516
-
517
- describe '#only' do
518
- specify { expect(subject.only(:field)).to be_a described_class }
519
- specify { expect(subject.only(:field)).not_to eq(subject) }
520
- specify { expect { subject.only(:field) }.not_to change { subject.criteria.fields } }
521
-
522
- specify { expect(subject.only(:field1, :field2).criteria.fields).to match_array(%w[field1 field2]) }
523
- specify { expect(subject.only(%i[field1 field2]).only(:field3).criteria.fields).to match_array(%w[field1 field2 field3]) }
524
- end
525
-
526
- describe '#only!' do
527
- specify { expect(subject.only!(:field)).to be_a described_class }
528
- specify { expect(subject.only!(:field)).not_to eq(subject) }
529
- specify { expect { subject.only!(:field) }.not_to change { subject.criteria.fields } }
530
-
531
- specify { expect(subject.only!(:field1, :field2).criteria.fields).to match_array(%w[field1 field2]) }
532
- specify { expect(subject.only!(%i[field1 field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
533
- specify { expect(subject.only(%i[field1 field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
534
- end
535
-
536
- describe '#types' do
537
- specify { expect(subject.types(:product)).to be_a described_class }
538
- specify { expect(subject.types(:product)).not_to eq(subject) }
539
- specify { expect { subject.types(:product) }.not_to change { subject.criteria.types } }
540
-
541
- specify { expect(subject.types(:user).criteria.types).to eq(['user']) }
542
- specify { expect(subject.types(:product, :city).criteria.types).to match_array(%w[product city]) }
543
- specify { expect(subject.types(%i[product city]).types(:country).criteria.types).to match_array(%w[product city country]) }
544
- end
545
-
546
- describe '#types!' do
547
- specify { expect(subject.types!(:product)).to be_a described_class }
548
- specify { expect(subject.types!(:product)).not_to eq(subject) }
549
- specify { expect { subject.types!(:product) }.not_to change { subject.criteria.types } }
550
-
551
- specify { expect(subject.types!(:user).criteria.types).to eq(['user']) }
552
- specify { expect(subject.types!(:product, :city).criteria.types).to match_array(%w[product city]) }
553
- specify { expect(subject.types!(%i[product city]).types!(:country).criteria.types).to match_array(['country']) }
554
- specify { expect(subject.types(%i[product city]).types!(:country).criteria.types).to match_array(['country']) }
555
- end
556
-
557
- describe '#search_type' do
558
- specify { expect(subject.search_type(:count).criteria.search_options).to include(search_type: :count) }
559
- end
560
-
561
- describe '#aggregations' do
562
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).to be_a described_class }
563
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).not_to eq(subject) }
564
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}}).criteria.request_body[:body]).to include(aggregations: {attribute: {terms: {field: 'attribute'}}}) }
565
- end
566
-
567
- describe '#merge' do
568
- let(:query) { described_class.new(ProductsIndex) }
569
-
570
- specify do
571
- expect(subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters)
572
- .to eq([{term: {'name' => 'name'}}, {term: {'age' => 42}}])
573
- end
574
- end
575
-
576
- describe '#to_a', :orm do
577
- before { stub_model(:city) }
578
- let(:cities) { Array.new(3) { |i| City.create! id: i + 1, name: "name#{i}", rating: i } }
579
- subject { described_class.new(CitiesIndex) }
580
-
581
- context do
582
- before do
583
- stub_index(:cities) do
584
- define_type :city do
585
- field :name
586
- field :rating, type: 'integer'
587
- field :nested, type: 'object', value: -> { {name: name} }
588
- end
589
- end
590
- end
591
-
592
- before { CitiesIndex::City.import! cities }
593
-
594
- specify { expect(subject.order(:rating).first).to be_a CitiesIndex::City }
595
- specify { expect(subject.order(:rating).first.name).to eq('name0') }
596
- specify { expect(subject.order(:rating).first.rating).to eq(0) }
597
- specify { expect(subject.order(:rating).first.nested).to eq('name' => 'name0') }
598
- specify { expect(subject.order(:rating).first.id).to eq(cities.first.id.to_s) }
599
-
600
- specify { expect(subject.order(:rating).only(:name).first.name).to eq('name0') }
601
- specify { expect(subject.order(:rating).only(:name).first.rating).to be_nil }
602
- specify { expect(subject.order(:rating).only(:nested).first.nested).to eq('name' => 'name0') }
603
-
604
- specify { expect(subject.order(:rating).first._score).to be_nil }
605
- specify { expect(subject.first._score).to be > 0 }
606
- specify { expect(subject.query(match: {name: 'name0'}).first._score).to be > 0 }
607
- specify { expect(subject.query(match: {name: 'name0'}).took).to be >= 0 }
608
-
609
- specify { expect(subject.order(:rating).first._explanation).to be_nil }
610
- specify { expect(subject.order(:rating).explain.first._explanation).to be_present }
611
- end
612
-
613
- context 'sourceless' do
614
- before do
615
- stub_index(:cities) do
616
- define_type :city do
617
- root _source: {enabled: false} do
618
- field :name
619
- field :rating, type: 'integer'
620
- field :nested, type: 'object', value: -> { {name: name} }
621
- end
622
- end
623
- end
624
- end
625
- before { CitiesIndex::City.import! cities }
626
-
627
- specify { expect(subject.order(:rating).first).to be_a CitiesIndex::City }
628
- specify { expect(subject.order(:rating).first.name).to be_nil }
629
- specify { expect(subject.order(:rating).first.rating).to be_nil }
630
- specify { expect(subject.order(:rating).first.nested).to be_nil }
631
- end
632
- end
633
- else
634
- xspecify 'Skip Chewy::Query specs for 5.0'
635
- end
636
- end