Commit fce3f92e2fc9ea8bf7cfab0efbde58d789b9052d

Authored by Andrew Kane
1 parent 7bc3d4b7

Moved ReindexV2Job to BulkRecordIndexer [skip ci]

lib/searchkick/bulk_record_indexer.rb
... ... @@ -31,12 +31,12 @@ module Searchkick
31 31 end
32 32 end
33 33  
34   - def reindex_items(klass, items)
  34 + def reindex_items(klass, items, method_name:)
35 35 routing = items.to_h { |r| [r[:id], r[:routing]] }
36 36 record_ids = routing.keys
37 37  
38 38 scope = Searchkick.load_records(klass, record_ids)
39   - scope = scope.search_import if scope.respond_to?(:search_import)
  39 + # scope = scope.search_import if scope.respond_to?(:search_import)
40 40 records = scope.select(&:should_index?)
41 41  
42 42 # determine which records to delete
... ... @@ -46,7 +46,7 @@ module Searchkick
46 46 construct_record(klass, id, routing[id])
47 47 end
48 48  
49   - import_inline(records, delete_records, method_name: nil)
  49 + import_inline(records, delete_records, method_name: method_name)
50 50 end
51 51  
52 52 private
... ...
lib/searchkick/process_batch_job.rb
... ... @@ -13,7 +13,9 @@ module Searchkick
13 13 {id: parts[0], routing: parts[1]}
14 14 end
15 15  
16   - index.send(:bulk_record_indexer).reindex_items(klass, items)
  16 + relation = klass
  17 + relation = relation.search_import if relation.respond_to?(:search_import)
  18 + index.send(:bulk_record_indexer).reindex_items(relation, items, method_name: nil)
17 19 end
18 20 end
19 21 end
... ...
lib/searchkick/reindex_v2_job.rb
1 1 module Searchkick
2 2 class ReindexV2Job < ActiveJob::Base
3   - RECORD_NOT_FOUND_CLASSES = [
4   - "ActiveRecord::RecordNotFound",
5   - "Mongoid::Errors::DocumentNotFound"
6   - ]
7   -
8 3 queue_as { Searchkick.queue_name }
9 4  
10 5 def perform(klass, id, method_name = nil, routing: nil)
11 6 model = klass.constantize
12   - record =
13   - begin
14   - if model.respond_to?(:unscoped)
15   - model.unscoped.find(id)
16   - else
17   - model.find(id)
18   - end
19   - rescue => e
20   - # check by name rather than rescue directly so we don't need
21   - # to determine which classes are defined
22   - raise e unless RECORD_NOT_FOUND_CLASSES.include?(e.class.name)
23   - nil
24   - end
25   -
26   - # TODO move construct_record logic
27   - record ||= model.searchkick_index.send(:bulk_record_indexer).send(:construct_record, model, id, routing)
28   -
29   - RecordIndexer.new(record).reindex(method_name, mode: :inline)
  7 + # may not be needed if calling search_import later
  8 + model = model.unscoped if model.respond_to?(:unscoped)
  9 + items = [{id: id, routing: routing}]
  10 + # TODO improve notification
  11 + model.searchkick_index.send(:bulk_record_indexer).reindex_items(model, items, method_name: method_name)
30 12 end
31 13 end
32 14 end
... ...