Commit 80457def70a607922cbe04c9f2b6642007c0d6c1
1 parent
7dc437d4
Exists in
master
and in
2 other branches
Improved naming [skip ci]
Showing
2 changed files
with
20 additions
and
19 deletions
Show diff stats
lib/searchkick/index.rb
lib/searchkick/relation_indexer.rb
... | ... | @@ -6,7 +6,7 @@ module Searchkick |
6 | 6 | @index = index |
7 | 7 | end |
8 | 8 | |
9 | - def import_scope(relation, resume: false, method_name: nil, async: false, full: false, scope: nil, mode: nil) | |
9 | + def reindex(relation, resume: false, method_name: nil, async: false, full: false, scope: nil, mode: nil) | |
10 | 10 | mode ||= (async ? :async : :inline) |
11 | 11 | |
12 | 12 | # apply scopes |
... | ... | @@ -70,14 +70,16 @@ module Searchkick |
70 | 70 | end |
71 | 71 | end |
72 | 72 | |
73 | - def full_reindex_async(scope) | |
74 | - if scope.respond_to?(:primary_key) | |
73 | + def full_reindex_async(relation) | |
74 | + batch_id = 1 | |
75 | + | |
76 | + if relation.respond_to?(:primary_key) | |
75 | 77 | # TODO expire Redis key |
76 | - primary_key = scope.primary_key | |
78 | + primary_key = relation.primary_key | |
77 | 79 | |
78 | 80 | starting_id = |
79 | 81 | begin |
80 | - scope.minimum(primary_key) | |
82 | + relation.minimum(primary_key) | |
81 | 83 | rescue ActiveRecord::StatementInvalid |
82 | 84 | false |
83 | 85 | end |
... | ... | @@ -85,34 +87,33 @@ module Searchkick |
85 | 87 | if starting_id.nil? |
86 | 88 | # no records, do nothing |
87 | 89 | elsif starting_id.is_a?(Numeric) |
88 | - max_id = scope.maximum(primary_key) | |
90 | + max_id = relation.maximum(primary_key) | |
89 | 91 | batches_count = ((max_id - starting_id + 1) / batch_size.to_f).ceil |
90 | 92 | |
91 | 93 | batches_count.times do |i| |
92 | - batch_id = i + 1 | |
93 | 94 | min_id = starting_id + (i * batch_size) |
94 | - bulk_reindex_job(scope, batch_id, min_id: min_id, max_id: min_id + batch_size - 1) | |
95 | + bulk_reindex_job(relation, batch_id, min_id: min_id, max_id: min_id + batch_size - 1) | |
96 | + batch_id += 1 | |
95 | 97 | end |
96 | 98 | else |
97 | - scope.find_in_batches(batch_size: batch_size).each_with_index do |batch, i| | |
98 | - batch_id = i + 1 | |
99 | - bulk_reindex_job(scope, batch_id, record_ids: batch.map { |record| record.id.to_s }) | |
99 | + relation.find_in_batches(batch_size: batch_size) do |batch| | |
100 | + bulk_reindex_job(relation, batch_id, record_ids: batch.map { |record| record.id.to_s }) | |
101 | + batch_id += 1 | |
100 | 102 | end |
101 | 103 | end |
102 | 104 | else |
103 | - batch_id = 1 | |
104 | - each_batch(scope, batch_size: batch_size) do |items| | |
105 | - bulk_reindex_job(scope, batch_id, record_ids: items.map { |i| i.id.to_s }) | |
105 | + each_batch(relation, batch_size: batch_size) do |items| | |
106 | + bulk_reindex_job(relation, batch_id, record_ids: items.map { |i| i.id.to_s }) | |
106 | 107 | batch_id += 1 |
107 | 108 | end |
108 | 109 | end |
109 | 110 | end |
110 | 111 | |
111 | - def each_batch(scope, batch_size:) | |
112 | + def each_batch(relation, batch_size:) | |
112 | 113 | # https://github.com/karmi/tire/blob/master/lib/tire/model/import.rb |
113 | 114 | # use cursor for Mongoid |
114 | 115 | items = [] |
115 | - scope.all.each do |item| | |
116 | + relation.all.each do |item| | |
116 | 117 | items << item |
117 | 118 | if items.length == batch_size |
118 | 119 | yield items |
... | ... | @@ -122,10 +123,10 @@ module Searchkick |
122 | 123 | yield items if items.any? |
123 | 124 | end |
124 | 125 | |
125 | - def bulk_reindex_job(scope, batch_id, **options) | |
126 | + def bulk_reindex_job(relation, batch_id, **options) | |
126 | 127 | Searchkick.with_redis { |r| r.sadd(batches_key, batch_id) } |
127 | 128 | Searchkick::BulkReindexJob.perform_later( |
128 | - class_name: scope.searchkick_options[:class_name], | |
129 | + class_name: relation.searchkick_options[:class_name], | |
129 | 130 | index_name: index.name, |
130 | 131 | batch_id: batch_id, |
131 | 132 | **options | ... | ... |