Commit 03093de60e98909e131912d87f244f732afd9f0d
Exists in
master
and in
21 other branches
Merge branch 'paddingtonsbear-master'
Showing
2 changed files
with
34 additions
and
1 deletions
Show diff stats
lib/searchkick/model.rb
... | ... | @@ -101,6 +101,12 @@ module Searchkick |
101 | 101 | self.class.searchkick_index.reindex_record_async(self) |
102 | 102 | end unless method_defined?(:reindex_async) |
103 | 103 | |
104 | + def partial_reindex(method_name) | |
105 | + self.class.searchkick_index.bulk_update([self], method_name) | |
106 | + self.class.searchkick_index.refresh | |
107 | + true | |
108 | + end unless method_defined?(:partial_reindex) | |
109 | + | |
104 | 110 | def similar(options = {}) |
105 | 111 | self.class.searchkick_index.similar_record(self, options) |
106 | 112 | end unless method_defined?(:similar) | ... | ... |
test/partial_reindex_test.rb
1 | 1 | require_relative "test_helper" |
2 | 2 | |
3 | 3 | class PartialReindexTest < Minitest::Test |
4 | - def test_basic | |
4 | + def test_class_method | |
5 | 5 | store [{name: "Hi", color: "Blue"}] |
6 | 6 | |
7 | 7 | # normal search |
... | ... | @@ -28,4 +28,31 @@ class PartialReindexTest < Minitest::Test |
28 | 28 | assert_search "bye", ["Bye"], fields: [:name], load: false |
29 | 29 | assert_search "blue", ["Bye"], fields: [:color], load: false |
30 | 30 | end |
31 | + | |
32 | + def test_instance_method | |
33 | + store [{name: "Hi", color: "Blue"}] | |
34 | + | |
35 | + # normal search | |
36 | + assert_search "hi", ["Hi"], fields: [:name], load: false | |
37 | + assert_search "blue", ["Hi"], fields: [:color], load: false | |
38 | + | |
39 | + # update | |
40 | + product = Product.first | |
41 | + product.name = "Bye" | |
42 | + product.color = "Red" | |
43 | + Searchkick.callbacks(false) do | |
44 | + product.save! | |
45 | + end | |
46 | + Product.searchkick_index.refresh | |
47 | + | |
48 | + # index not updated | |
49 | + assert_search "hi", ["Hi"], fields: [:name], load: false | |
50 | + assert_search "blue", ["Hi"], fields: [:color], load: false | |
51 | + | |
52 | + product.partial_reindex(:search_name) | |
53 | + | |
54 | + # name updated, but not color | |
55 | + assert_search "bye", ["Bye"], fields: [:name], load: false | |
56 | + assert_search "blue", ["Bye"], fields: [:color], load: false | |
57 | + end | |
31 | 58 | end | ... | ... |