chewy 5.1.0 → 7.2.7

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