Commit 5fae015006773066c37043734249f5d5e4880931
1 parent
c44781e7
Exists in
master
and in
19 other branches
Added Mongoid and NoBrainer support for queues
Showing
7 changed files
with
25 additions
and
18 deletions
Show diff stats
README.md
... | ... | @@ -1185,7 +1185,7 @@ And use: |
1185 | 1185 | Searchkick.reindex_status(index_name) |
1186 | 1186 | ``` |
1187 | 1187 | |
1188 | -### Queues [master, experimental, ActiveRecord only] | |
1188 | +### Queues [master, experimental] | |
1189 | 1189 | |
1190 | 1190 | You can also queue updates and do them in bulk for better performance. First, set up Redis in an initializer. |
1191 | 1191 | ... | ... |
lib/searchkick.rb
... | ... | @@ -146,6 +146,25 @@ module Searchkick |
146 | 146 | end |
147 | 147 | |
148 | 148 | # private |
149 | + def self.load_records(records, ids) | |
150 | + records = | |
151 | + if records.respond_to?(:primary_key) | |
152 | + # ActiveRecord | |
153 | + records.where(records.primary_key => ids) if records.primary_key | |
154 | + elsif records.respond_to?(:queryable) | |
155 | + # Mongoid 3+ | |
156 | + records.queryable.for_ids(ids) | |
157 | + elsif records.respond_to?(:unscoped) && :id.respond_to?(:in) | |
158 | + # Nobrainer | |
159 | + records.unscoped.where(:id.in => ids) | |
160 | + end | |
161 | + | |
162 | + raise Searchkick::Error, "Not sure how to load records" if !records | |
163 | + | |
164 | + records | |
165 | + end | |
166 | + | |
167 | + # private | |
149 | 168 | def self.indexer |
150 | 169 | Thread.current[:searchkick_indexer] ||= Searchkick::Indexer.new |
151 | 170 | end | ... | ... |
lib/searchkick/bulk_reindex_job.rb
... | ... | @@ -6,7 +6,7 @@ module Searchkick |
6 | 6 | klass = class_name.constantize |
7 | 7 | index = index_name ? Searchkick::Index.new(index_name) : klass.searchkick_index |
8 | 8 | record_ids ||= min_id..max_id |
9 | - index.import_scope(klass.where(klass.primary_key => record_ids), method_name: method_name, batch: true, batch_id: batch_id) | |
9 | + index.import_scope(Searchkick.load_records(klass, record_ids), method_name: method_name, batch: true, batch_id: batch_id) | |
10 | 10 | end |
11 | 11 | end |
12 | 12 | end | ... | ... |
lib/searchkick/process_batch_job.rb
... | ... | @@ -4,7 +4,7 @@ module Searchkick |
4 | 4 | |
5 | 5 | def perform(class_name:, record_ids:) |
6 | 6 | klass = class_name.constantize |
7 | - scope = klass.where(klass.primary_key => record_ids) | |
7 | + scope = Searchkick.load_records(klass, record_ids) | |
8 | 8 | scope = scope.search_import if scope.respond_to?(:search_import) |
9 | 9 | records = scope.select(&:should_index?) |
10 | 10 | ... | ... |
lib/searchkick/results.rb
... | ... | @@ -206,21 +206,7 @@ module Searchkick |
206 | 206 | end |
207 | 207 | end |
208 | 208 | |
209 | - records = | |
210 | - if records.respond_to?(:primary_key) | |
211 | - # ActiveRecord | |
212 | - records.where(records.primary_key => ids) if records.primary_key | |
213 | - elsif records.respond_to?(:queryable) | |
214 | - # Mongoid 3+ | |
215 | - records.queryable.for_ids(ids) | |
216 | - elsif records.respond_to?(:unscoped) && :id.respond_to?(:in) | |
217 | - # Nobrainer | |
218 | - records.unscoped.where(:id.in => ids) | |
219 | - end | |
220 | - | |
221 | - raise Searchkick::Error, "Not sure how to load records" if !records | |
222 | - | |
223 | - records | |
209 | + Searchkick.load_records(records, ids) | |
224 | 210 | end |
225 | 211 | |
226 | 212 | def base_field(k) | ... | ... |
test/gemfiles/mongoid6.gemfile
test/gemfiles/nobrainer.gemfile