Skip to content

Instantly share code, notes, and snippets.

@nippe
Created March 11, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nippe/24a8a2d216e8870093b4 to your computer and use it in GitHub Desktop.
Save nippe/24a8a2d216e8870093b4 to your computer and use it in GitHub Desktop.
Elasticsearch index migration
require 'elasticsearch'
require 'redis'
require 'colorize'
unless ARGV.length == 3
puts 'Wrong number of arguments'
puts
puts 'Syntax: ruby migrate-doorman-v1.rb <elasticsearch URL> <source index> <target-index>'
puts 'Example: ruby migrate-doorman-v1.rb http://localhost:9200 doorman-v1 doorman-v2'
exit
end
esServerUrl = ARGV[0]
sourceIndex = ARGV[1]
targetIndex = ARGV[2]
client = Elasticsearch::Client.new host: esServerUrl, log: false
redisClient = Redis.new
r = client.search index: sourceIndex, type: 'rejectedRequest', scroll: '10m', search_type: 'scan', size: 500, body: {
query: {
filtered: {
filter: {
exists: {
field: "acastUrl"
}
}
}
}
}
while r = client.scroll(scroll_id: r['_scroll_id'], scroll: '15m') and not r['hits']['hits'].empty? do
puts
puts "--- BATCH #{defined?($i) ? $i += 1 : $i = 1} -------------------------------------------------"
r['hits']['hits'].each do |h|
h['_source']['acast_url'] = h['_source']['acastUrl']
h['_source']['channel_url'] = h['_source']['channelUrl']
unless redisClient.get('es_migr:' + h['_id'])
puts h['_id'].green
begin
client.index index: targetIndex, type: 'rejectedRequest', id: h['_id'], body: h['_source']
redisClient.set('es_migr:'+h['_id'], "foo")
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
else
puts print h['_id'].red
end
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment