Forked from plcstevens/elasticsearch_reindex.rb
Last active
September 8, 2015 17:33
-
-
Save lukaszkorecki/809529f27fcc55d0e285 to your computer and use it in GitHub Desktop.
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
es_alias = ENV.fetch('ALIAS') | |
hosts = ENV.fetch('HOSTS') | |
old_index = ENV.fetch('OLD_INDEX') | |
old_type = ENV.fetch('OLD_TYPE') | |
new_index = ENV.fetch('NEW_INDEX') | |
new_type = ENV.fetch('NEW_TYPE') | |
size = ENV.fetch('SCAN_SIZE', 100) | |
client = Elasticsearch::Client.new(hosts: hosts) | |
unless client.indices.exists(index: new_index) | |
body = { mappings: JSON.parse("PATH/TO/MAPPING.JSON") } | |
client.indices.create(index: new_index, body: body) | |
end | |
current_count = client.count(index: old_index) | |
puts "We will be copying #{current_count["count"]} documents from '#{old_index}' into '#{new_index}'" | |
# Fill up the new index with old index data. | |
# This currently does a match_all. | |
response = client.search(index: old_index, type: old_type, search_type: 'scan', scroll: '5m', size: size) | |
while (response = client.scroll(scroll_id: response['_scroll_id'], scroll: '5m')) do | |
break if response['hits']['hits'].empty? | |
body = response['hits']['hits'].map do |document| | |
{ | |
index: { | |
_index: new_index, | |
_type: new_type, | |
_id: document['_id'], | |
data: document['_source'] | |
} | |
} | |
end | |
client.bulk(body: body) | |
printf('.') | |
end | |
# You may wish to change this to be the last scroll id if you are running multiple scrolls! | |
client.clear_scroll(scroll_id: %w(_all)) | |
puts | |
puts "Updating alias #{es_alias} to point from '#{old_index}' to '#{new_index}'" | |
body = { | |
actions: [ | |
{ remove: { index: old_index, alias: es_alias } }, | |
{ add: { index: new_index, alias: es_alias } } | |
] | |
} | |
client.indices.update_aliases(body: body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment