diff --git a/lib/searchkick/record_indexer.rb b/lib/searchkick/record_indexer.rb index fa82d9b..efb2a1d 100644 --- a/lib/searchkick/record_indexer.rb +++ b/lib/searchkick/record_indexer.rb @@ -34,7 +34,7 @@ module Searchkick else Searchkick::BulkReindexJob.perform_later( class_name: records.first.class.searchkick_options[:class_name], - record_ids: records.map(&:id), + record_ids: records.map { |r| r.id.to_s }, index_name: index.name, method_name: method_name ? method_name.to_s : nil ) diff --git a/lib/searchkick/relation_indexer.rb b/lib/searchkick/relation_indexer.rb index 4be89d9..6b17702 100644 --- a/lib/searchkick/relation_indexer.rb +++ b/lib/searchkick/relation_indexer.rb @@ -29,6 +29,8 @@ module Searchkick return full_reindex_async(relation) end + relation = resume_relation(relation) if resume + reindex_options = { mode: mode, method_name: method_name, @@ -36,18 +38,6 @@ module Searchkick } record_indexer = RecordIndexer.new(index) - if resume - if relation.respond_to?(:primary_key) - # use total docs instead of max id since there's not a great way - # to get the max _id without scripting since it's a string - - # TODO use primary key and prefix with table name - relation = relation.where("id > ?", index.total_docs) - else - # TODO support resume for Mongoid - end - end - if relation.respond_to?(:find_in_batches) relation.find_in_batches(batch_size: batch_size) do |items| record_indexer.reindex(items, **reindex_options) @@ -69,6 +59,18 @@ module Searchkick private + def resume_relation(relation) + if relation.respond_to?(:primary_key) + # use total docs instead of max id since there's not a great way + # to get the max _id without scripting since it's a string + + # TODO use primary key and prefix with table name + relation = relation.where("id > ?", index.total_docs) + else + raise Error, "Resume not supported for Mongoid" + end + end + def full_reindex_async(scope) if scope.respond_to?(:primary_key) # TODO expire Redis key diff --git a/test/reindex_test.rb b/test/reindex_test.rb index cfc5a87..03d20b4 100644 --- a/test/reindex_test.rb +++ b/test/reindex_test.rb @@ -163,7 +163,14 @@ class ReindexTest < Minitest::Test end def test_full_resume - assert Product.reindex(resume: true) + if mongoid? + error = assert_raises(Searchkick::Error) do + Product.reindex(resume: true) + end + assert_equal "Resume not supported for Mongoid", error.message + else + assert Product.reindex(resume: true) + end end def test_full_refresh diff --git a/test/support/mongoid.rb b/test/support/mongoid.rb index 7ab1355..de2a237 100644 --- a/test/support/mongoid.rb +++ b/test/support/mongoid.rb @@ -2,7 +2,7 @@ Mongoid.logger = $logger Mongo::Logger.logger = $logger if defined?(Mongo::Logger) Mongoid.configure do |config| - config.connect_to "searchkick_test" + config.connect_to "searchkick_test", server_selection_timeout: 1 end class Product diff --git a/test/test_helper.rb b/test/test_helper.rb index 8f5c526..4979f79 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -43,6 +43,10 @@ else require_relative "support/activerecord" end +def mongoid? + defined?(Mongoid) +end + # models Dir["#{__dir__}/models/*"].each do |file| require file -- libgit2 0.21.0