Commit df81e3c3c5e82d281a602442917cf7987f48cf40

Authored by Andrew Kane
1 parent 9acd45c1

Moved more methods into index

lib/searchkick/index.rb
... ... @@ -73,7 +73,7 @@ module Searchkick
73 73 )["_source"]
74 74 end
75 75  
76   - def reindex(record)
  76 + def reindex_record(record)
77 77 if record.destroyed? or !record.should_index?
78 78 begin
79 79 remove(record)
... ... @@ -85,6 +85,14 @@ module Searchkick
85 85 end
86 86 end
87 87  
  88 + def reindex_record_async(record)
  89 + if defined?(Searchkick::ReindexV2Job)
  90 + Searchkick::ReindexV2Job.perform_later(record.class.name, record.id.to_s)
  91 + else
  92 + Delayed::Job.enqueue Searchkick::ReindexJob.new(record.class.name, record.id.to_s)
  93 + end
  94 + end
  95 +
88 96 def klass_document_type(klass)
89 97 if klass.respond_to?(:document_type)
90 98 klass.document_type
... ... @@ -113,6 +121,36 @@ module Searchkick
113 121 index
114 122 end
115 123  
  124 + # https://gist.github.com/jarosan/3124884
  125 + # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
  126 + def reindex_scope(scope, options = {})
  127 + skip_import = options[:import] == false
  128 +
  129 + clean_indices
  130 +
  131 + index = create_index
  132 +
  133 + # check if alias exists
  134 + if alias_exists?
  135 + # import before swap
  136 + index.import_scope(scope) unless skip_import
  137 +
  138 + # get existing indices to remove
  139 + swap(index.name)
  140 + clean_indices
  141 + else
  142 + delete if exists?
  143 + swap(index.name)
  144 +
  145 + # import after swap
  146 + index.import_scope(scope) unless skip_import
  147 + end
  148 +
  149 + index.refresh
  150 +
  151 + true
  152 + end
  153 +
116 154 def import_scope(scope)
117 155 batch_size = @options[:batch_size] || 1000
118 156  
... ...
lib/searchkick/model.rb
... ... @@ -35,11 +35,7 @@ module Searchkick
35 35 include Searchkick::Similar
36 36  
37 37 def reindex_async
38   - if defined?(Searchkick::ReindexV2Job)
39   - Searchkick::ReindexV2Job.perform_later(self.class.name, id.to_s)
40   - else
41   - Delayed::Job.enqueue Searchkick::ReindexJob.new(self.class.name, id.to_s)
42   - end
  38 + self.class.searchkick_index.reindex_record_async(self)
43 39 end
44 40  
45 41 if callbacks
... ... @@ -69,7 +65,7 @@ module Searchkick
69 65 end
70 66  
71 67 def reindex
72   - self.class.searchkick_index.reindex(self)
  68 + self.class.searchkick_index.reindex_record(self)
73 69 end
74 70  
75 71 def search_data
... ...
lib/searchkick/reindex.rb
... ... @@ -6,34 +6,8 @@ module Searchkick
6 6 @descendents << klass unless @descendents.include?(klass)
7 7 end
8 8  
9   - # https://gist.github.com/jarosan/3124884
10   - # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
11 9 def reindex(options = {})
12   - skip_import = options[:import] == false
13   -
14   - clean_indices
15   -
16   - index = searchkick_create_index
17   -
18   - # check if alias exists
19   - if searchkick_index.alias_exists?
20   - # import before swap
21   - searchkick_import(index: index) unless skip_import
22   -
23   - # get existing indices to remove
24   - searchkick_index.swap(index.name)
25   - clean_indices
26   - else
27   - searchkick_index.delete if searchkick_index.exists?
28   - searchkick_index.swap(index.name)
29   -
30   - # import after swap
31   - searchkick_import(index: index) unless skip_import
32   - end
33   -
34   - index.refresh
35   -
36   - true
  10 + searchkick_index.reindex_scope(searchkick_klass, options)
37 11 end
38 12  
39 13 def clean_indices
... ... @@ -41,8 +15,7 @@ module Searchkick
41 15 end
42 16  
43 17 def searchkick_import(options = {})
44   - index = options[:index] || searchkick_index
45   - index.import_scope(searchkick_klass)
  18 + (options[:index] || searchkick_index).import_scope(searchkick_klass)
46 19 end
47 20  
48 21 def searchkick_create_index
... ...