Commit c7074f13b33f6410c853e79be21ae6dc71e51a35

Authored by Andrew Kane
1 parent fd149dbb

Improved resume code and fixed Mongoid code [skip ci]

lib/searchkick/record_indexer.rb
@@ -34,7 +34,7 @@ module Searchkick @@ -34,7 +34,7 @@ module Searchkick
34 else 34 else
35 Searchkick::BulkReindexJob.perform_later( 35 Searchkick::BulkReindexJob.perform_later(
36 class_name: records.first.class.searchkick_options[:class_name], 36 class_name: records.first.class.searchkick_options[:class_name],
37 - record_ids: records.map(&:id), 37 + record_ids: records.map { |r| r.id.to_s },
38 index_name: index.name, 38 index_name: index.name,
39 method_name: method_name ? method_name.to_s : nil 39 method_name: method_name ? method_name.to_s : nil
40 ) 40 )
lib/searchkick/relation_indexer.rb
@@ -29,6 +29,8 @@ module Searchkick @@ -29,6 +29,8 @@ module Searchkick
29 return full_reindex_async(relation) 29 return full_reindex_async(relation)
30 end 30 end
31 31
  32 + relation = resume_relation(relation) if resume
  33 +
32 reindex_options = { 34 reindex_options = {
33 mode: mode, 35 mode: mode,
34 method_name: method_name, 36 method_name: method_name,
@@ -36,18 +38,6 @@ module Searchkick @@ -36,18 +38,6 @@ module Searchkick
36 } 38 }
37 record_indexer = RecordIndexer.new(index) 39 record_indexer = RecordIndexer.new(index)
38 40
39 - if resume  
40 - if relation.respond_to?(:primary_key)  
41 - # use total docs instead of max id since there's not a great way  
42 - # to get the max _id without scripting since it's a string  
43 -  
44 - # TODO use primary key and prefix with table name  
45 - relation = relation.where("id > ?", index.total_docs)  
46 - else  
47 - # TODO support resume for Mongoid  
48 - end  
49 - end  
50 -  
51 if relation.respond_to?(:find_in_batches) 41 if relation.respond_to?(:find_in_batches)
52 relation.find_in_batches(batch_size: batch_size) do |items| 42 relation.find_in_batches(batch_size: batch_size) do |items|
53 record_indexer.reindex(items, **reindex_options) 43 record_indexer.reindex(items, **reindex_options)
@@ -69,6 +59,18 @@ module Searchkick @@ -69,6 +59,18 @@ module Searchkick
69 59
70 private 60 private
71 61
  62 + def resume_relation(relation)
  63 + if relation.respond_to?(:primary_key)
  64 + # use total docs instead of max id since there's not a great way
  65 + # to get the max _id without scripting since it's a string
  66 +
  67 + # TODO use primary key and prefix with table name
  68 + relation = relation.where("id > ?", index.total_docs)
  69 + else
  70 + raise Error, "Resume not supported for Mongoid"
  71 + end
  72 + end
  73 +
72 def full_reindex_async(scope) 74 def full_reindex_async(scope)
73 if scope.respond_to?(:primary_key) 75 if scope.respond_to?(:primary_key)
74 # TODO expire Redis key 76 # TODO expire Redis key
test/reindex_test.rb
@@ -163,7 +163,14 @@ class ReindexTest < Minitest::Test @@ -163,7 +163,14 @@ class ReindexTest < Minitest::Test
163 end 163 end
164 164
165 def test_full_resume 165 def test_full_resume
166 - assert Product.reindex(resume: true) 166 + if mongoid?
  167 + error = assert_raises(Searchkick::Error) do
  168 + Product.reindex(resume: true)
  169 + end
  170 + assert_equal "Resume not supported for Mongoid", error.message
  171 + else
  172 + assert Product.reindex(resume: true)
  173 + end
167 end 174 end
168 175
169 def test_full_refresh 176 def test_full_refresh
test/support/mongoid.rb
@@ -2,7 +2,7 @@ Mongoid.logger = $logger @@ -2,7 +2,7 @@ Mongoid.logger = $logger
2 Mongo::Logger.logger = $logger if defined?(Mongo::Logger) 2 Mongo::Logger.logger = $logger if defined?(Mongo::Logger)
3 3
4 Mongoid.configure do |config| 4 Mongoid.configure do |config|
5 - config.connect_to "searchkick_test" 5 + config.connect_to "searchkick_test", server_selection_timeout: 1
6 end 6 end
7 7
8 class Product 8 class Product
test/test_helper.rb
@@ -43,6 +43,10 @@ else @@ -43,6 +43,10 @@ else
43 require_relative "support/activerecord" 43 require_relative "support/activerecord"
44 end 44 end
45 45
  46 +def mongoid?
  47 + defined?(Mongoid)
  48 +end
  49 +
46 # models 50 # models
47 Dir["#{__dir__}/models/*"].each do |file| 51 Dir["#{__dir__}/models/*"].each do |file|
48 require file 52 require file