Commit fd149dbb18e892cceb2c1a77544ca11f7bbadf18

Authored by Andrew Kane
1 parent fc4a9dd7

Improved RelationIndexer code [skip ci]

Showing 1 changed file with 17 additions and 10 deletions   Show diff stats
lib/searchkick/relation_indexer.rb
@@ -7,13 +7,23 @@ module Searchkick @@ -7,13 +7,23 @@ module Searchkick
7 end 7 end
8 8
9 def import_scope(relation, resume: false, method_name: nil, async: false, full: false, scope: nil, mode: nil) 9 def import_scope(relation, resume: false, method_name: nil, async: false, full: false, scope: nil, mode: nil)
  10 + mode ||= (async ? :async : :inline)
  11 +
  12 + # apply scopes
10 if scope 13 if scope
11 relation = relation.send(scope) 14 relation = relation.send(scope)
12 elsif relation.respond_to?(:search_import) 15 elsif relation.respond_to?(:search_import)
13 relation = relation.search_import 16 relation = relation.search_import
14 end 17 end
15 18
16 - mode ||= (async ? :async : :inline) 19 + # remove unneeded conditions for async
  20 + if mode == :async
  21 + if relation.respond_to?(:primary_key)
  22 + relation = relation.select(relation.primary_key).except(:includes, :preload)
  23 + elsif relation.respond_to?(:only)
  24 + relation = relation.only(:_id)
  25 + end
  26 + end
17 27
18 if full && async 28 if full && async
19 return full_reindex_async(relation) 29 return full_reindex_async(relation)
@@ -26,22 +36,23 @@ module Searchkick @@ -26,22 +36,23 @@ module Searchkick
26 } 36 }
27 record_indexer = RecordIndexer.new(index) 37 record_indexer = RecordIndexer.new(index)
28 38
29 - if relation.respond_to?(:find_in_batches)  
30 - if resume 39 + if resume
  40 + if relation.respond_to?(:primary_key)
31 # use total docs instead of max id since there's not a great way 41 # use total docs instead of max id since there's not a great way
32 # to get the max _id without scripting since it's a string 42 # to get the max _id without scripting since it's a string
33 43
34 # TODO use primary key and prefix with table name 44 # TODO use primary key and prefix with table name
35 relation = relation.where("id > ?", index.total_docs) 45 relation = relation.where("id > ?", index.total_docs)
  46 + else
  47 + # TODO support resume for Mongoid
36 end 48 end
  49 + end
37 50
38 - relation = relation.select("id").except(:includes, :preload) if mode == :async  
39 - 51 + if relation.respond_to?(:find_in_batches)
40 relation.find_in_batches(batch_size: batch_size) do |items| 52 relation.find_in_batches(batch_size: batch_size) do |items|
41 record_indexer.reindex(items, **reindex_options) 53 record_indexer.reindex(items, **reindex_options)
42 end 54 end
43 else 55 else
44 - # TODO handle resume option  
45 each_batch(relation, batch_size: batch_size) do |items| 56 each_batch(relation, batch_size: batch_size) do |items|
46 record_indexer.reindex(items, **reindex_options) 57 record_indexer.reindex(items, **reindex_options)
47 end 58 end
@@ -63,8 +74,6 @@ module Searchkick @@ -63,8 +74,6 @@ module Searchkick
63 # TODO expire Redis key 74 # TODO expire Redis key
64 primary_key = scope.primary_key 75 primary_key = scope.primary_key
65 76
66 - scope = scope.select(primary_key).except(:includes, :preload)  
67 -  
68 starting_id = 77 starting_id =
69 begin 78 begin
70 scope.minimum(primary_key) 79 scope.minimum(primary_key)
@@ -92,8 +101,6 @@ module Searchkick @@ -92,8 +101,6 @@ module Searchkick
92 end 101 end
93 else 102 else
94 batch_id = 1 103 batch_id = 1
95 - # TODO remove any eager loading  
96 - scope = scope.only(:_id) if scope.respond_to?(:only)  
97 each_batch(scope, batch_size: batch_size) do |items| 104 each_batch(scope, batch_size: batch_size) do |items|
98 bulk_reindex_job scope, batch_id, record_ids: items.map { |i| i.id.to_s } 105 bulk_reindex_job scope, batch_id, record_ids: items.map { |i| i.id.to_s }
99 batch_id += 1 106 batch_id += 1