lib/searchkick/index.rb in searchkick-2.3.1 vs lib/searchkick/index.rb in searchkick-2.3.2

- old
+ new

@@ -13,11 +13,17 @@ def create(body = {}) client.indices.create index: name, body: body end def delete - client.indices.delete index: name + if !Searchkick.server_below?("6.0.0-alpha1") && alias_exists? + # can't call delete directly on aliases in ES 6 + indices = client.indices.get_alias(name: name).keys + client.indices.delete index: indices + else + client.indices.delete index: name + end end def exists? client.indices.exists index: name end @@ -226,11 +232,12 @@ index_options.deep_merge!(settings: {index: {refresh_interval: refresh_interval}}) if refresh_interval index = create_index(index_options: index_options) end # check if alias exists - if alias_exists? + alias_exists = alias_exists? + if alias_exists # import before promotion index.import_scope(scope, resume: resume, async: async, full: true) if import # get existing indices to remove unless async @@ -244,10 +251,28 @@ # import after promotion index.import_scope(scope, resume: resume, async: async, full: true) if import end if async + if async.is_a?(Hash) && async[:wait] + puts "Created index: #{index.name}" + puts "Jobs queued. Waiting..." + loop do + sleep 3 + status = Searchkick.reindex_status(index.name) + break if status[:completed] + puts "Batches left: #{status[:batches_left]}" + end + # already promoted if alias didn't exist + if alias_exists + puts "Jobs complete. Promoting..." + promote(index.name, update_refresh_interval: !refresh_interval.nil?) + end + clean_indices unless retain + puts "SUCCESS!" + end + {index_name: index.name} else index.refresh true end @@ -271,12 +296,12 @@ scope = scope.where("id > ?", total_docs) end scope = scope.select("id").except(:includes, :preload) if async - scope.find_in_batches batch_size: batch_size do |batch| - import_or_update batch, method_name, async + scope.find_in_batches batch_size: batch_size do |items| + import_or_update items, method_name, async end else each_batch(scope) do |items| import_or_update items, method_name, async end @@ -288,11 +313,11 @@ end # other def tokens(text, options = {}) - client.indices.analyze({text: text, index: name}.merge(options))["tokens"].map { |t| t["token"] } + client.indices.analyze(body: {text: text}.merge(options), index: name)["tokens"].map { |t| t["token"] } end def klass_document_type(klass) @klass_document_type[klass] ||= begin if klass.respond_to?(:document_type) @@ -419,10 +444,16 @@ def full_reindex_async(scope) if scope.respond_to?(:primary_key) # TODO expire Redis key primary_key = scope.primary_key - starting_id = scope.minimum(primary_key) + starting_id = + begin + scope.minimum(primary_key) + rescue ActiveRecord::StatementInvalid + false + end + if starting_id.nil? # no records, do nothing elsif starting_id.is_a?(Numeric) max_id = scope.maximum(primary_key) batches_count = ((max_id - starting_id + 1) / batch_size.to_f).ceil