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
@@ -12,14 +12,122 @@ describe :minitest_helper do
12
12
 
13
13
  include ::Chewy::Minitest::Helpers
14
14
 
15
+ def assert_equal(expected, actual, message)
16
+ raise message unless expected == actual
17
+ end
18
+
15
19
  before do
16
20
  Chewy.massacre
17
21
  end
18
22
 
19
23
  before do
20
24
  stub_index(:dummies) do
21
- define_type :dummy do
22
- root value: ->(_o) { {} }
25
+ root value: ->(_o) { {} }
26
+ end
27
+ end
28
+
29
+ describe 'mock_elasticsearch_response' do
30
+ let(:hits) do
31
+ [
32
+ {
33
+ '_index' => 'dummies',
34
+ '_type' => '_doc',
35
+ '_id' => '2',
36
+ '_score' => 3.14,
37
+ '_source' => source
38
+ }
39
+ ]
40
+ end
41
+
42
+ let(:source) { {'name' => 'some_name', id: '2'} }
43
+ let(:sources) { [source] }
44
+
45
+ context 'mocks by raw response' do
46
+ let(:raw_response) do
47
+ {
48
+ 'took' => 4,
49
+ 'timed_out' => false,
50
+ '_shards' => {
51
+ 'total' => 1,
52
+ 'successful' => 1,
53
+ 'skipped' => 0,
54
+ 'failed' => 0
55
+ },
56
+ 'hits' => {
57
+ 'total' => {
58
+ 'value' => 1,
59
+ 'relation' => 'eq'
60
+ },
61
+ 'max_score' => 1.0,
62
+ 'hits' => hits
63
+ }
64
+ }
65
+ end
66
+
67
+ specify do
68
+ mock_elasticsearch_response(DummiesIndex, raw_response) do
69
+ expect(DummiesIndex.query({}).hits).to eq(hits)
70
+ end
71
+ end
72
+ end
73
+
74
+ context 'mocks by response sources' do
75
+ specify do
76
+ mock_elasticsearch_response_sources(DummiesIndex, sources) do
77
+ expect(DummiesIndex.query({}).hits).to eq(hits)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'assert correct elasticsearch query' do
84
+ let(:query) do
85
+ DummiesIndex.filter.should { multi_match foo: 'bar' }.filter { match foo: 'bar' }
86
+ end
87
+
88
+ let(:expected_query) do
89
+ {
90
+ index: ['dummies'],
91
+ body: {
92
+ query: {
93
+ bool: {
94
+ filter: {
95
+ bool: {
96
+ must: {
97
+ match: {foo: 'bar'}
98
+ },
99
+ should: {
100
+ multi_match: {foo: 'bar'}
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
108
+ end
109
+
110
+ context 'will be built' do
111
+ specify do
112
+ expect { assert_elasticsearch_query(query, expected_query) }.not_to raise_error
113
+ end
114
+ end
115
+
116
+ context 'will not be built' do
117
+ let(:unexpected_query) do
118
+ {
119
+ index: ['what?'],
120
+ body: {}
121
+ }
122
+ end
123
+
124
+ let(:unexpected_query_error_message) do
125
+ 'got {:index=>["dummies"], :body=>{:query=>{:bool=>{:filter=>{:bool=>{:must=>{:match=>{:foo=>"bar"}}, :should=>{:multi_match=>{:foo=>"bar"}}}}}}}} instead of expected query.'
126
+ end
127
+
128
+ specify do
129
+ expect { assert_elasticsearch_query(query, unexpected_query) }
130
+ .to raise_error(RuntimeError, unexpected_query_error_message)
23
131
  end
24
132
  end
25
133
  end
@@ -27,45 +135,45 @@ describe :minitest_helper do
27
135
  context 'assert_indexes' do
28
136
  specify 'doesn\'t fail when index updates correctly' do
29
137
  expect do
30
- assert_indexes DummiesIndex::Dummy do
31
- DummiesIndex::Dummy.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
138
+ assert_indexes DummiesIndex do
139
+ DummiesIndex.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
32
140
  end
33
141
  end.to_not raise_error
34
142
  end
35
143
 
36
144
  specify 'fails when index doesn\'t update' do
37
145
  expect do
38
- assert_indexes DummiesIndex::Dummy do
146
+ assert_indexes DummiesIndex do
39
147
  end
40
148
  end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
41
149
  end
42
150
 
43
151
  specify 'SearchIndexReceiver catches the indexes' do
44
- receiver = assert_indexes DummiesIndex::Dummy do
45
- DummiesIndex::Dummy.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
152
+ receiver = assert_indexes DummiesIndex do
153
+ DummiesIndex.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
46
154
  end
47
155
 
48
156
  expect(receiver).to be_a(SearchIndexReceiver)
49
157
 
50
158
  expect(
51
- receiver.indexes_for(DummiesIndex::Dummy)
159
+ receiver.indexes_for(DummiesIndex)
52
160
  .map { |index| index[:_id] }
53
161
  ).to match_array([41, 42])
54
162
  end
55
163
 
56
164
  specify 'Real index is bypassed when asserting' do
57
- expect(DummiesIndex::Dummy).not_to receive(:bulk)
165
+ expect(DummiesIndex).not_to receive(:bulk)
58
166
 
59
- assert_indexes DummiesIndex::Dummy do
60
- DummiesIndex::Dummy.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
167
+ assert_indexes DummiesIndex do
168
+ DummiesIndex.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
61
169
  end
62
170
  end
63
171
 
64
172
  specify 'Real index is allowed when asserting' do
65
- expect(DummiesIndex::Dummy).to receive(:bulk)
173
+ expect(DummiesIndex).to receive(:bulk)
66
174
 
67
- assert_indexes DummiesIndex::Dummy, bypass_actual_index: false do
68
- DummiesIndex::Dummy.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
175
+ assert_indexes DummiesIndex, bypass_actual_index: false do
176
+ DummiesIndex.bulk body: [{index: {_id: 42, data: {}}}, {index: {_id: 41, data: {}}}]
69
177
  end
70
178
  end
71
179
  end
@@ -26,95 +26,93 @@ describe :search_index_receiver do
26
26
 
27
27
  before do
28
28
  stub_index(:dummies) do
29
- define_type :fizz do
30
- root value: ->(_o) { {} }
31
- end
29
+ root value: ->(_o) { {} }
30
+ end
32
31
 
33
- define_type :buzz do
34
- root value: ->(_o) { {} }
35
- end
32
+ stub_index(:dummies2) do
33
+ root value: ->(_o) { {} }
36
34
  end
37
35
  end
38
36
 
39
37
  context 'catch' do
40
38
  specify 'archives more than one type' do
41
- receiver.catch search_request(2), DummiesIndex::Fizz
42
- receiver.catch search_request(3), DummiesIndex::Buzz
43
- expect(receiver.indexes.keys).to match_array([DummiesIndex::Fizz, DummiesIndex::Buzz])
39
+ receiver.catch search_request(2), DummiesIndex
40
+ receiver.catch search_request(3), Dummies2Index
41
+ expect(receiver.indexes.keys).to match_array([DummiesIndex, Dummies2Index])
44
42
  end
45
43
  end
46
44
 
47
45
  context 'indexes_for' do
48
46
  before do
49
- receiver.catch search_request(2), DummiesIndex::Fizz
50
- receiver.catch search_request(3), DummiesIndex::Buzz
47
+ receiver.catch search_request(2), DummiesIndex
48
+ receiver.catch search_request(3), Dummies2Index
51
49
  end
52
50
 
53
51
  specify 'returns indexes for a specific type' do
54
- expect(parse_request(receiver.indexes_for(DummiesIndex::Fizz))).to match_array([1, 2])
52
+ expect(parse_request(receiver.indexes_for(DummiesIndex))).to match_array([1, 2])
55
53
  end
56
54
 
57
55
  specify 'returns only indexes for all types' do
58
56
  index_responses = receiver.indexes
59
- expect(index_responses.keys).to match_array([DummiesIndex::Fizz, DummiesIndex::Buzz])
57
+ expect(index_responses.keys).to match_array([DummiesIndex, Dummies2Index])
60
58
  expect(parse_request(index_responses.values.flatten)).to match_array([1, 2, 1, 2, 3])
61
59
  end
62
60
  end
63
61
 
64
62
  context 'deletes_for' do
65
63
  before do
66
- receiver.catch search_request(2, verb: :delete), DummiesIndex::Fizz
67
- receiver.catch search_request(3, verb: :delete), DummiesIndex::Buzz
64
+ receiver.catch search_request(2, verb: :delete), DummiesIndex
65
+ receiver.catch search_request(3, verb: :delete), Dummies2Index
68
66
  end
69
67
 
70
68
  specify 'returns deletes for a specific type' do
71
- expect(receiver.deletes_for(DummiesIndex::Buzz)).to match_array([1, 2, 3])
69
+ expect(receiver.deletes_for(Dummies2Index)).to match_array([1, 2, 3])
72
70
  end
73
71
 
74
72
  specify 'returns only deletes for all types' do
75
73
  deletes = receiver.deletes
76
- expect(deletes.keys).to match_array([DummiesIndex::Fizz, DummiesIndex::Buzz])
74
+ expect(deletes.keys).to match_array([DummiesIndex, Dummies2Index])
77
75
  expect(deletes.values.flatten).to match_array([1, 2, 1, 2, 3])
78
76
  end
79
77
  end
80
78
 
81
79
  context 'indexed?' do
82
80
  before do
83
- receiver.catch search_request(1), DummiesIndex::Fizz
81
+ receiver.catch search_request(1), DummiesIndex
84
82
  end
85
83
 
86
84
  specify 'validates that an object was indexed' do
87
85
  dummy = OpenStruct.new(id: 1)
88
- expect(receiver.indexed?(dummy, DummiesIndex::Fizz)).to be(true)
86
+ expect(receiver.indexed?(dummy, DummiesIndex)).to be(true)
89
87
  end
90
88
 
91
89
  specify 'doesn\'t validate than unindexed objects were indexed' do
92
90
  dummy = OpenStruct.new(id: 2)
93
- expect(receiver.indexed?(dummy, DummiesIndex::Fizz)).to be(false)
91
+ expect(receiver.indexed?(dummy, DummiesIndex)).to be(false)
94
92
  end
95
93
  end
96
94
 
97
95
  context 'deleted?' do
98
96
  before do
99
- receiver.catch search_request(1, verb: :delete), DummiesIndex::Fizz
97
+ receiver.catch search_request(1, verb: :delete), DummiesIndex
100
98
  end
101
99
 
102
100
  specify 'validates than an object was deleted' do
103
101
  dummy = OpenStruct.new(id: 1)
104
- expect(receiver.deleted?(dummy, DummiesIndex::Fizz)).to be(true)
102
+ expect(receiver.deleted?(dummy, DummiesIndex)).to be(true)
105
103
  end
106
104
 
107
105
  specify 'doesn\'t validate than undeleted objects were deleted' do
108
106
  dummy = OpenStruct.new(id: 2)
109
- expect(receiver.deleted?(dummy, DummiesIndex::Fizz)).to be(false)
107
+ expect(receiver.deleted?(dummy, DummiesIndex)).to be(false)
110
108
  end
111
109
  end
112
110
 
113
111
  context 'updated_indexes' do
114
112
  specify 'provides a list of indices updated' do
115
- receiver.catch search_request(2, verb: :delete), DummiesIndex::Fizz
116
- receiver.catch search_request(3, verb: :delete), DummiesIndex::Buzz
117
- expect(receiver.updated_indexes).to match_array([DummiesIndex::Fizz, DummiesIndex::Buzz])
113
+ receiver.catch search_request(2, verb: :delete), DummiesIndex
114
+ receiver.catch search_request(3, verb: :delete), Dummies2Index
115
+ expect(receiver.updated_indexes).to match_array([DummiesIndex, Dummies2Index])
118
116
  end
119
117
  end
120
118
  end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+ require 'chewy/multi_search'
3
+
4
+ describe Chewy::MultiSearch do
5
+ before { Chewy.massacre }
6
+
7
+ before do
8
+ stub_model(:city)
9
+ stub_model(:country)
10
+
11
+ stub_index(:cities) do
12
+ def self.aggregate_by_country
13
+ aggs(country: {terms: {field: :country_id}})
14
+ end
15
+
16
+ index_scope City
17
+ field :name, type: 'keyword'
18
+ field :country_id, type: 'keyword'
19
+ end
20
+ end
21
+
22
+ let(:places_query) { CitiesIndex.all }
23
+
24
+ describe '#queries' do
25
+ specify 'returns the queries that are a part of the multi search' do
26
+ multi_search = described_class.new([places_query])
27
+ expect(multi_search.queries).to contain_exactly(places_query)
28
+ end
29
+ end
30
+
31
+ describe '#add_query' do
32
+ specify 'adds a query to the multi search' do
33
+ multi_search = described_class.new([])
34
+ expect do
35
+ multi_search.add_query(places_query)
36
+ end.to change {
37
+ multi_search.queries
38
+ }.from([]).to([places_query])
39
+ end
40
+ end
41
+
42
+ context 'when given two queries' do
43
+ let(:queries) { [aggregates, results] }
44
+ let(:aggregates) { CitiesIndex.aggregate_by_country.limit(0) }
45
+ let(:results) { CitiesIndex.limit(10) }
46
+ let(:multi_search) { described_class.new(queries) }
47
+ let(:cities) { Array.new(3) { |i| City.create! name: "Name#{i + 2}", country_id: i + 1 } }
48
+ before { CitiesIndex.import! city: cities }
49
+
50
+ describe '#perform' do
51
+ specify 'performs each query' do
52
+ expect { multi_search.perform }
53
+ .to change(aggregates, :performed?).from(false).to(true)
54
+ .and change(results, :performed?).from(false).to(true)
55
+ end
56
+
57
+ specify 'issues a single request using the msearch endpoint', :aggregate_failures do
58
+ expect(Chewy.client).to receive(:msearch).once.and_return('responses' => [])
59
+ expect(Chewy.client).to_not receive(:search)
60
+ multi_search.perform
61
+ end
62
+ end
63
+
64
+ describe '#responses' do
65
+ subject(:responses) { multi_search.responses }
66
+
67
+ context 'on a previously performed multi search' do
68
+ before { multi_search.perform }
69
+
70
+ it 'does not perform the query again' do
71
+ expect(Chewy.client).to_not receive(:msearch)
72
+ multi_search.responses
73
+ end
74
+ end
75
+
76
+ specify 'returns the results of each query', :aggregate_failures do
77
+ is_expected.to have(2).responses
78
+ expect(responses[0]).to be_a(Chewy::Search::Response)
79
+ expect(responses[1]).to be_a(Chewy::Search::Response)
80
+ expect(responses[1].wrappers).to all(be_a CitiesIndex)
81
+ end
82
+ end
83
+ end
84
+ end