Commit 873816e23a3de2904a4af24b725eb2a72d754b32
1 parent
815dee64
Exists in
master
and in
2 other branches
Moved logic to index [skip ci]
Showing
3 changed files
with
46 additions
and
10 deletions
Show diff stats
lib/searchkick/index.rb
... | ... | @@ -209,7 +209,28 @@ module Searchkick |
209 | 209 | |
210 | 210 | # reindex |
211 | 211 | |
212 | - def reindex(relation, method_name, scoped:, full: false, **options) | |
212 | + def reindex(object, method_name: nil, full: false, **options) | |
213 | + if object.is_a?(Array) | |
214 | + mode = options.delete(:mode) | |
215 | + mode ||= Searchkick.callbacks_value || @options[:callbacks] || true | |
216 | + mode = :inline if mode == :bulk | |
217 | + | |
218 | + refresh = options.delete(:refresh) | |
219 | + | |
220 | + # note: always want full to be false here | |
221 | + result = RecordIndexer.new(self).reindex(object, mode: mode, method_name: method_name, full: false, **options) | |
222 | + self.refresh if refresh | |
223 | + return result | |
224 | + end | |
225 | + | |
226 | + if !object.respond_to?(:searchkick_klass) | |
227 | + raise Error, "Cannot reindex object" | |
228 | + end | |
229 | + | |
230 | + scoped = Searchkick.relation?(object) | |
231 | + # call searchkick_klass for inheritance | |
232 | + relation = scoped ? object.all : Searchkick.scope(object.searchkick_klass).all | |
233 | + | |
213 | 234 | refresh = options.fetch(:refresh, !scoped) |
214 | 235 | options.delete(:refresh) |
215 | 236 | ... | ... |
lib/searchkick/model.rb
... | ... | @@ -26,11 +26,7 @@ module Searchkick |
26 | 26 | include(mod) |
27 | 27 | mod.module_eval do |
28 | 28 | def reindex(method_name = nil, mode: nil, refresh: false) |
29 | - mode ||= Searchkick.callbacks_value || self.class.searchkick_index.options[:callbacks] || true | |
30 | - mode = :inline if mode == :bulk | |
31 | - result = RecordIndexer.new(self.class.searchkick_index).reindex([self], mode: mode, method_name: method_name, single: true) | |
32 | - self.class.searchkick_index.refresh if refresh | |
33 | - result | |
29 | + self.class.searchkick_index.reindex([self], method_name: method_name, mode: mode, refresh: refresh, single: true) | |
34 | 30 | end |
35 | 31 | |
36 | 32 | def similar(**options) |
... | ... | @@ -76,10 +72,7 @@ module Searchkick |
76 | 72 | alias_method :search_index, :searchkick_index unless method_defined?(:search_index) |
77 | 73 | |
78 | 74 | def searchkick_reindex(method_name = nil, **options) |
79 | - scoped = Searchkick.relation?(self) | |
80 | - # call searchkick_klass for inheritance | |
81 | - relation = scoped ? all : Searchkick.scope(searchkick_klass).all | |
82 | - searchkick_index.reindex(relation, method_name, scoped: scoped, **options) | |
75 | + searchkick_index.reindex(self, method_name: method_name, **options) | |
83 | 76 | end |
84 | 77 | alias_method :reindex, :searchkick_reindex unless method_defined?(:reindex) |
85 | 78 | ... | ... |
test/reindex_test.rb
... | ... | @@ -47,6 +47,14 @@ class ReindexTest < Minitest::Test |
47 | 47 | assert_search "product", ["Product A"] |
48 | 48 | end |
49 | 49 | |
50 | + def test_record_index | |
51 | + store_names ["Product A", "Product B"], reindex: false | |
52 | + | |
53 | + product = Product.find_by!(name: "Product A") | |
54 | + assert_equal true, Product.search_index.reindex([product], refresh: true) | |
55 | + assert_search "product", ["Product A"] | |
56 | + end | |
57 | + | |
50 | 58 | def test_relation_inline |
51 | 59 | store_names ["Product A"] |
52 | 60 | store_names ["Product B", "Product C"], reindex: false |
... | ... | @@ -142,6 +150,13 @@ class ReindexTest < Minitest::Test |
142 | 150 | assert_search "product", ["Product A", "Product B"] |
143 | 151 | end |
144 | 152 | |
153 | + def test_relation_index | |
154 | + store_names ["Product A"] | |
155 | + store_names ["Product B", "Product C"], reindex: false | |
156 | + Product.search_index.reindex(Product.where(name: "Product B"), refresh: true) | |
157 | + assert_search "product", ["Product A", "Product B"] | |
158 | + end | |
159 | + | |
145 | 160 | def test_full_async |
146 | 161 | store_names ["Product A"], reindex: false |
147 | 162 | reindex = nil |
... | ... | @@ -290,6 +305,13 @@ class ReindexTest < Minitest::Test |
290 | 305 | Searchkick::ProcessQueueJob.perform_now(class_name: "Product") |
291 | 306 | end |
292 | 307 | |
308 | + def test_object_index | |
309 | + error = assert_raises(Searchkick::Error) do | |
310 | + Product.search_index.reindex(Object.new) | |
311 | + end | |
312 | + assert_equal "Cannot reindex object", error.message | |
313 | + end | |
314 | + | |
293 | 315 | def test_transaction |
294 | 316 | skip unless activerecord? |
295 | 317 | ... | ... |