Skip to content

Instantly share code, notes, and snippets.

@supairish
Forked from rubyconvict/bulk_reindexer.rb
Created February 15, 2023 21:42
Show Gist options
  • Select an option

  • Save supairish/15e31228fb840195017bb962841ddb92 to your computer and use it in GitHub Desktop.

Select an option

Save supairish/15e31228fb840195017bb962841ddb92 to your computer and use it in GitHub Desktop.

Revisions

  1. @rubyconvict rubyconvict revised this gist Aug 22, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions bulk_reindexer.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    # https://medium.com/rubyinside/asynchronous-elasticsearch-bulk-reindexing-with-rails-searchkick-and-sidekiq-26f2f9aa8513
    # https://github.com/ankane/searchkick
    # 2.3.2 [unreleased]
    # - Added wait option to async reindex
    # Searchkick.reindex(async: {wait: true})
    # This code has been ported to searchkick.
    require 'sidekiq/api'

    # BulkReindexer
  2. @VvanGemert VvanGemert created this gist Jul 2, 2017.
    28 changes: 28 additions & 0 deletions bulk_reindexer.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    require 'sidekiq/api'

    # BulkReindexer
    module BulkReindexer
    def self.reindex_model(model, promote_and_clean = true)
    puts "Reindexing #{model.name}..."
    index = model.reindex(async: true, refresh_interval: '30s')
    puts "All jobs are in queue. Index name: #{index[:index_name]}"
    loop do
    # Check the size of queue
    queue_size = Sidekiq::Queue.new('searchkick').size
    puts "Jobs left: #{queue_size}"
    # Check every 5 seconds
    sleep 5
    break if queue_size.zero?
    end
    puts 'Jobs complete. Promoting...'
    promote_and_clean(model, index) if promote_and_clean
    end

    def self.promote_and_clean(model, index)
    model.search_index.promote(index[:index_name],
    update_refresh_interval: true)
    puts "Reindex of #{model.name} complete."
    puts 'Cleaning old indices'
    model.search_index.clean_indices
    end
    end