Skip to content

Instantly share code, notes, and snippets.

@1stvamp
Last active February 13, 2025 12:57
Show Gist options
  • Save 1stvamp/43eebde29d47b725c94800728b150e39 to your computer and use it in GitHub Desktop.
Save 1stvamp/43eebde29d47b725c94800728b150e39 to your computer and use it in GitHub Desktop.
Asynchronously migrate Searchkick created Elasticsearch indices between clusters (e.g. for a major version upgrade), for 0-downtime
# Connection to new v8 cluster
v8_es = Elasticsearch::Client.new(host: NEW_V8_ES_URL)
# v7 cluster details
host = Searchkick.client.transport.connections[0].host
# For every model we create an indentical (but empty) index and matching alias in the v8 cluster,
# then we create an async task in the new v8 cluster to reindex the contents of the index
# remotely from the v7 cluster.
Searchkick.models.each do |model|
index = model.search_index
index.all_indices.each do |name|
v8_es.indices.create(index: name, body: index.index_options) rescue nil
v8_es.indices.update_aliases(body: { actions: { add: { index: name, alias: index.name } } }) rescue nil
puts v8_es.reindex(body: {
source: {
index: index.name,
remote: {
host: "https://#{host[:host]}:#{host[:port]}",
username: host[:user],
password: host[:password]
}
},
dest: { index: name, version_type: 'external' },
conflicts: 'proceed'
}, wait_for_completion: false)
end
end
@1stvamp
Copy link
Author

1stvamp commented Feb 13, 2025

After switching over to the new ES cluster, you should run a Searchkick reindex job on each model:

# See the Searchkick readme for more information about configuring asynchronous reindex jobs,
# we use Sidekiq for ours on a custom queue for YMMV

Searchkick.models.each do |model|
  model.reindex(mode: async)
end

You shouldn't need to promote indices with as Searchkick.promote_index(index_name) after this as SK only creates new indices when the schema that created them differs, but you might want to check the indices created in the new cluster to be sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment