Last active
February 13, 2025 12:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After switching over to the new ES cluster, you should run a Searchkick reindex job on each model:
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.