diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index 8cfebf1..dd4a1db 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -73,7 +73,7 @@ module Searchkick )["_source"] end - def reindex(record) + def reindex_record(record) if record.destroyed? or !record.should_index? begin remove(record) @@ -85,6 +85,14 @@ module Searchkick end end + def reindex_record_async(record) + if defined?(Searchkick::ReindexV2Job) + Searchkick::ReindexV2Job.perform_later(record.class.name, record.id.to_s) + else + Delayed::Job.enqueue Searchkick::ReindexJob.new(record.class.name, record.id.to_s) + end + end + def klass_document_type(klass) if klass.respond_to?(:document_type) klass.document_type @@ -113,6 +121,36 @@ module Searchkick index end + # https://gist.github.com/jarosan/3124884 + # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/ + def reindex_scope(scope, options = {}) + skip_import = options[:import] == false + + clean_indices + + index = create_index + + # check if alias exists + if alias_exists? + # import before swap + index.import_scope(scope) unless skip_import + + # get existing indices to remove + swap(index.name) + clean_indices + else + delete if exists? + swap(index.name) + + # import after swap + index.import_scope(scope) unless skip_import + end + + index.refresh + + true + end + def import_scope(scope) batch_size = @options[:batch_size] || 1000 diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 13755a4..16c2d1c 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -35,11 +35,7 @@ module Searchkick include Searchkick::Similar def reindex_async - if defined?(Searchkick::ReindexV2Job) - Searchkick::ReindexV2Job.perform_later(self.class.name, id.to_s) - else - Delayed::Job.enqueue Searchkick::ReindexJob.new(self.class.name, id.to_s) - end + self.class.searchkick_index.reindex_record_async(self) end if callbacks @@ -69,7 +65,7 @@ module Searchkick end def reindex - self.class.searchkick_index.reindex(self) + self.class.searchkick_index.reindex_record(self) end def search_data diff --git a/lib/searchkick/reindex.rb b/lib/searchkick/reindex.rb index 6d64121..717fd62 100644 --- a/lib/searchkick/reindex.rb +++ b/lib/searchkick/reindex.rb @@ -6,34 +6,8 @@ module Searchkick @descendents << klass unless @descendents.include?(klass) end - # https://gist.github.com/jarosan/3124884 - # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/ def reindex(options = {}) - skip_import = options[:import] == false - - clean_indices - - index = searchkick_create_index - - # check if alias exists - if searchkick_index.alias_exists? - # import before swap - searchkick_import(index: index) unless skip_import - - # get existing indices to remove - searchkick_index.swap(index.name) - clean_indices - else - searchkick_index.delete if searchkick_index.exists? - searchkick_index.swap(index.name) - - # import after swap - searchkick_import(index: index) unless skip_import - end - - index.refresh - - true + searchkick_index.reindex_scope(searchkick_klass, options) end def clean_indices @@ -41,8 +15,7 @@ module Searchkick end def searchkick_import(options = {}) - index = options[:index] || searchkick_index - index.import_scope(searchkick_klass) + (options[:index] || searchkick_index).import_scope(searchkick_klass) end def searchkick_create_index -- libgit2 0.21.0