Commit 5b7405e5e6c9fbe9c780e007fad85bb04c1dfcf7
1 parent
85bd38ba
Exists in
master
and in
21 other branches
Improved syntax for partial reindex [skip ci]
Showing
2 changed files
with
22 additions
and
15 deletions
Show diff stats
lib/searchkick/model.rb
... | ... | @@ -43,21 +43,25 @@ module Searchkick |
43 | 43 | class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks? |
44 | 44 | end |
45 | 45 | |
46 | - def searchkick_reindex(options = {}) | |
47 | - unless options[:accept_danger] | |
48 | - if (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) || | |
49 | - (respond_to?(:queryable) && queryable != unscoped.with_default_scope) | |
50 | - raise Searchkick::DangerousOperation, "Only call reindex on models, not relations. Pass `accept_danger: true` if this is your intention." | |
46 | + def searchkick_reindex(method_name = nil, **options) | |
47 | + if method_name | |
48 | + searchkick_index.import_scope(searchkick_klass, method_name: method_name) | |
49 | + searchkick_index.refresh | |
50 | + true | |
51 | + else | |
52 | + unless options[:accept_danger] | |
53 | + if (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) || | |
54 | + (respond_to?(:queryable) && queryable != unscoped.with_default_scope) | |
55 | + raise Searchkick::DangerousOperation, "Only call reindex on models, not relations. Pass `accept_danger: true` if this is your intention." | |
56 | + end | |
51 | 57 | end |
58 | + searchkick_index.reindex_scope(searchkick_klass, options) | |
52 | 59 | end |
53 | - searchkick_index.reindex_scope(searchkick_klass, options) | |
54 | 60 | end |
55 | 61 | alias_method :reindex, :searchkick_reindex unless method_defined?(:reindex) |
56 | 62 | |
57 | 63 | def searchkick_partial_reindex(method_name) |
58 | - searchkick_index.import_scope(searchkick_klass, method_name: method_name) | |
59 | - searchkick_index.refresh | |
60 | - true | |
64 | + searchkick_reindex(method_name) | |
61 | 65 | end |
62 | 66 | alias_method :partial_reindex, :searchkick_partial_reindex unless method_defined?(:partial_reindex) |
63 | 67 | |
... | ... | @@ -93,8 +97,12 @@ module Searchkick |
93 | 97 | after_destroy callback_name, if: proc { self.class.search_callbacks? } |
94 | 98 | end |
95 | 99 | |
96 | - def reindex(options = {}) | |
97 | - self.class.searchkick_index.reindex_record(self) | |
100 | + def reindex(method_name = nil, **options) | |
101 | + if method_name | |
102 | + self.class.searchkick_index.bulk_update([self], method_name) | |
103 | + else | |
104 | + self.class.searchkick_index.reindex_record(self) | |
105 | + end | |
98 | 106 | self.class.searchkick_index.refresh if options[:refresh] |
99 | 107 | end unless method_defined?(:reindex) |
100 | 108 | |
... | ... | @@ -103,8 +111,7 @@ module Searchkick |
103 | 111 | end unless method_defined?(:reindex_async) |
104 | 112 | |
105 | 113 | def partial_reindex(method_name) |
106 | - self.class.searchkick_index.bulk_update([self], method_name) | |
107 | - self.class.searchkick_index.refresh | |
114 | + reindex(method_name, refresh: true) | |
108 | 115 | true |
109 | 116 | end unless method_defined?(:partial_reindex) |
110 | 117 | ... | ... |
test/partial_reindex_test.rb
... | ... | @@ -22,7 +22,7 @@ class PartialReindexTest < Minitest::Test |
22 | 22 | assert_search "blue", ["Hi"], fields: [:color], load: false |
23 | 23 | |
24 | 24 | # partial reindex |
25 | - Product.partial_reindex(:search_name) | |
25 | + Product.reindex(:search_name) | |
26 | 26 | |
27 | 27 | # name updated, but not color |
28 | 28 | assert_search "bye", ["Bye"], fields: [:name], load: false |
... | ... | @@ -49,7 +49,7 @@ class PartialReindexTest < Minitest::Test |
49 | 49 | assert_search "hi", ["Hi"], fields: [:name], load: false |
50 | 50 | assert_search "blue", ["Hi"], fields: [:color], load: false |
51 | 51 | |
52 | - product.partial_reindex(:search_name) | |
52 | + product.reindex(:search_name, refresh: true) | |
53 | 53 | |
54 | 54 | # name updated, but not color |
55 | 55 | assert_search "bye", ["Bye"], fields: [:name], load: false | ... | ... |