Commit fd149dbb18e892cceb2c1a77544ca11f7bbadf18
1 parent
fc4a9dd7
Exists in
master
and in
2 other branches
Improved RelationIndexer code [skip ci]
Showing
1 changed file
with
17 additions
and
10 deletions
Show diff stats
lib/searchkick/relation_indexer.rb
... | ... | @@ -7,13 +7,23 @@ module Searchkick |
7 | 7 | end |
8 | 8 | |
9 | 9 | def import_scope(relation, resume: false, method_name: nil, async: false, full: false, scope: nil, mode: nil) |
10 | + mode ||= (async ? :async : :inline) | |
11 | + | |
12 | + # apply scopes | |
10 | 13 | if scope |
11 | 14 | relation = relation.send(scope) |
12 | 15 | elsif relation.respond_to?(:search_import) |
13 | 16 | relation = relation.search_import |
14 | 17 | end |
15 | 18 | |
16 | - mode ||= (async ? :async : :inline) | |
19 | + # remove unneeded conditions for async | |
20 | + if mode == :async | |
21 | + if relation.respond_to?(:primary_key) | |
22 | + relation = relation.select(relation.primary_key).except(:includes, :preload) | |
23 | + elsif relation.respond_to?(:only) | |
24 | + relation = relation.only(:_id) | |
25 | + end | |
26 | + end | |
17 | 27 | |
18 | 28 | if full && async |
19 | 29 | return full_reindex_async(relation) |
... | ... | @@ -26,22 +36,23 @@ module Searchkick |
26 | 36 | } |
27 | 37 | record_indexer = RecordIndexer.new(index) |
28 | 38 | |
29 | - if relation.respond_to?(:find_in_batches) | |
30 | - if resume | |
39 | + if resume | |
40 | + if relation.respond_to?(:primary_key) | |
31 | 41 | # use total docs instead of max id since there's not a great way |
32 | 42 | # to get the max _id without scripting since it's a string |
33 | 43 | |
34 | 44 | # TODO use primary key and prefix with table name |
35 | 45 | relation = relation.where("id > ?", index.total_docs) |
46 | + else | |
47 | + # TODO support resume for Mongoid | |
36 | 48 | end |
49 | + end | |
37 | 50 | |
38 | - relation = relation.select("id").except(:includes, :preload) if mode == :async | |
39 | - | |
51 | + if relation.respond_to?(:find_in_batches) | |
40 | 52 | relation.find_in_batches(batch_size: batch_size) do |items| |
41 | 53 | record_indexer.reindex(items, **reindex_options) |
42 | 54 | end |
43 | 55 | else |
44 | - # TODO handle resume option | |
45 | 56 | each_batch(relation, batch_size: batch_size) do |items| |
46 | 57 | record_indexer.reindex(items, **reindex_options) |
47 | 58 | end |
... | ... | @@ -63,8 +74,6 @@ module Searchkick |
63 | 74 | # TODO expire Redis key |
64 | 75 | primary_key = scope.primary_key |
65 | 76 | |
66 | - scope = scope.select(primary_key).except(:includes, :preload) | |
67 | - | |
68 | 77 | starting_id = |
69 | 78 | begin |
70 | 79 | scope.minimum(primary_key) |
... | ... | @@ -92,8 +101,6 @@ module Searchkick |
92 | 101 | end |
93 | 102 | else |
94 | 103 | batch_id = 1 |
95 | - # TODO remove any eager loading | |
96 | - scope = scope.only(:_id) if scope.respond_to?(:only) | |
97 | 104 | each_batch(scope, batch_size: batch_size) do |items| |
98 | 105 | bulk_reindex_job scope, batch_id, record_ids: items.map { |i| i.id.to_s } |
99 | 106 | batch_id += 1 | ... | ... |