lib/searchkick/index.rb in searchkick-5.0.0 vs lib/searchkick/index.rb in searchkick-5.0.1

- old
+ new

@@ -69,16 +69,16 @@ query: {match_all: {}}, size: 0 } ) - Searchkick::Results.new(nil, response).total_count + Results.new(nil, response).total_count end def promote(new_name, update_refresh_interval: false) if update_refresh_interval - new_index = Searchkick::Index.new(new_name, @options) + new_index = Index.new(new_name, @options) settings = options[:settings] || {} refresh_interval = (settings[:index] && settings[:index][:refresh_interval]) || "1s" new_index.update_settings(index: {refresh_interval: refresh_interval}) end @@ -121,11 +121,11 @@ # remove old indices that start w/ index_name def clean_indices indices = all_indices(unaliased: true) indices.each do |index| - Searchkick::Index.new(index).delete + Index.new(index).delete end indices end def store(record) @@ -202,11 +202,11 @@ end # queue def reindex_queue - Searchkick::ReindexQueue.new(name) + ReindexQueue.new(name) end # reindex # note: this is designed to be used internally @@ -235,17 +235,30 @@ # import only import_scope(relation, method_name: method_name, mode: mode) self.refresh if refresh true else + async = options.delete(:async) + if async + if async.is_a?(Hash) && async[:wait] + # TODO warn in 5.1 + # Searchkick.warn "async option is deprecated - use mode: :async, wait: true instead" + options[:wait] = true unless options.key?(:wait) + else + # TODO warn in 5.1 + # Searchkick.warn "async option is deprecated - use mode: :async instead" + end + options[:mode] ||= :async + end + full_reindex(relation, **options) end end def create_index(index_options: nil) index_options ||= self.index_options - index = Searchkick::Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options) + index = Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options) index.create(index_options) index end def import_scope(relation, **options) @@ -324,36 +337,37 @@ def import_before_promotion(index, relation, **import_options) index.import_scope(relation, **import_options) end def reindex_records(object, mode: nil, refresh: false, **options) - mode ||= Searchkick.callbacks_value || @options[:callbacks] || true + mode ||= Searchkick.callbacks_value || @options[:callbacks] || :inline mode = :inline if mode == :bulk result = RecordIndexer.new(self).reindex(object, mode: mode, full: false, **options) self.refresh if refresh result end # https://gist.github.com/jarosan/3124884 # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/ - # TODO deprecate async in favor of mode: :async, wait: true/false - def full_reindex(relation, import: true, resume: false, retain: false, async: false, refresh_interval: nil, scope: nil) + def full_reindex(relation, import: true, resume: false, retain: false, mode: nil, refresh_interval: nil, scope: nil, wait: nil) + raise ArgumentError, "wait only available in :async mode" if !wait.nil? && mode != :async + if resume index_name = all_indices.sort.last - raise Searchkick::Error, "No index to resume" unless index_name - index = Searchkick::Index.new(index_name, @options) + raise Error, "No index to resume" unless index_name + index = Index.new(index_name, @options) else clean_indices unless retain index_options = relation.searchkick_index_options index_options.deep_merge!(settings: {index: {refresh_interval: refresh_interval}}) if refresh_interval index = create_index(index_options: index_options) end import_options = { - mode: (async ? :async : :inline), + mode: (mode || :inline), full: true, resume: resume, scope: scope } @@ -363,11 +377,11 @@ alias_exists = alias_exists? if alias_exists import_before_promotion(index, relation, **import_options) if import # get existing indices to remove - unless async + unless mode == :async check_uuid(uuid, index.uuid) promote(index.name, update_refresh_interval: !refresh_interval.nil?) clean_indices unless retain end else @@ -376,12 +390,12 @@ # import after promotion index.import_scope(relation, **import_options) if import end - if async - if async.is_a?(Hash) && async[:wait] + if mode == :async + if wait puts "Created index: #{index.name}" puts "Jobs queued. Waiting..." loop do sleep 3 status = Searchkick.reindex_status(index.name) @@ -415,10 +429,10 @@ # still a chance for race condition since its called before promotion # ideal is for user to disable automatic index creation # https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation def check_uuid(old_uuid, new_uuid) if old_uuid != new_uuid - raise Searchkick::Error, "Safety check failed - only run one Model.reindex per model at a time" + raise Error, "Safety check failed - only run one Model.reindex per model at a time" end end def notify(record, name) if Searchkick.callbacks_value == :bulk