From fce3f92e2fc9ea8bf7cfab0efbde58d789b9052d Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 20 Feb 2022 02:26:53 -0800 Subject: [PATCH] Moved ReindexV2Job to BulkRecordIndexer [skip ci] --- lib/searchkick/bulk_record_indexer.rb | 6 +++--- lib/searchkick/process_batch_job.rb | 4 +++- lib/searchkick/reindex_v2_job.rb | 28 +++++----------------------- 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/lib/searchkick/bulk_record_indexer.rb b/lib/searchkick/bulk_record_indexer.rb index e3793d0..1a35a65 100644 --- a/lib/searchkick/bulk_record_indexer.rb +++ b/lib/searchkick/bulk_record_indexer.rb @@ -31,12 +31,12 @@ module Searchkick end end - def reindex_items(klass, items) + def reindex_items(klass, items, method_name:) routing = items.to_h { |r| [r[:id], r[:routing]] } record_ids = routing.keys scope = Searchkick.load_records(klass, record_ids) - scope = scope.search_import if scope.respond_to?(:search_import) + # scope = scope.search_import if scope.respond_to?(:search_import) records = scope.select(&:should_index?) # determine which records to delete @@ -46,7 +46,7 @@ module Searchkick construct_record(klass, id, routing[id]) end - import_inline(records, delete_records, method_name: nil) + import_inline(records, delete_records, method_name: method_name) end private diff --git a/lib/searchkick/process_batch_job.rb b/lib/searchkick/process_batch_job.rb index ef5db57..84c0db5 100644 --- a/lib/searchkick/process_batch_job.rb +++ b/lib/searchkick/process_batch_job.rb @@ -13,7 +13,9 @@ module Searchkick {id: parts[0], routing: parts[1]} end - index.send(:bulk_record_indexer).reindex_items(klass, items) + relation = klass + relation = relation.search_import if relation.respond_to?(:search_import) + index.send(:bulk_record_indexer).reindex_items(relation, items, method_name: nil) end end end diff --git a/lib/searchkick/reindex_v2_job.rb b/lib/searchkick/reindex_v2_job.rb index 56bda06..5714160 100644 --- a/lib/searchkick/reindex_v2_job.rb +++ b/lib/searchkick/reindex_v2_job.rb @@ -1,32 +1,14 @@ module Searchkick class ReindexV2Job < ActiveJob::Base - RECORD_NOT_FOUND_CLASSES = [ - "ActiveRecord::RecordNotFound", - "Mongoid::Errors::DocumentNotFound" - ] - queue_as { Searchkick.queue_name } def perform(klass, id, method_name = nil, routing: nil) model = klass.constantize - record = - begin - if model.respond_to?(:unscoped) - model.unscoped.find(id) - else - model.find(id) - end - rescue => e - # check by name rather than rescue directly so we don't need - # to determine which classes are defined - raise e unless RECORD_NOT_FOUND_CLASSES.include?(e.class.name) - nil - end - - # TODO move construct_record logic - record ||= model.searchkick_index.send(:bulk_record_indexer).send(:construct_record, model, id, routing) - - RecordIndexer.new(record).reindex(method_name, mode: :inline) + # may not be needed if calling search_import later + model = model.unscoped if model.respond_to?(:unscoped) + items = [{id: id, routing: routing}] + # TODO improve notification + model.searchkick_index.send(:bulk_record_indexer).reindex_items(model, items, method_name: method_name) end end end -- libgit2 0.21.0