Last active
December 2, 2016 16:51
-
-
Save timdorr/6c0e028fe0dc527abba34fc39c444f35 to your computer and use it in GitHub Desktop.
Clone (import/export) an Elasticsearch index
This file contains 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
index = args[:index] # If you're in a Rake task | |
# You'll have to write your own code for this part. | |
# It just creates an index for you, like so: | |
# es.indices.create(index: index, body: mappings_and_such) | |
ci = CreateIndex.new(index) | |
ci.create | |
es = Elasticsearch::Client.new | |
es_prod = Elasticsearch::Client.new(url: ENV['PROD_ELASTICSEARCH_URL']) | |
results = es_prod.search( | |
index: index, | |
scroll: '5m', | |
size: 1000, | |
body: { query: { match_all: {} } } | |
) | |
while results['hits']['hits'].present? do | |
puts "Inserting #{results['hits']['hits'].size} documents..." | |
es.bulk( | |
index: index, | |
body: results['hits']['hits'].map { |result| [ { create: { _id: result['_id'], _type: result['_type'] } }, result['_source'] ] }.flatten | |
) | |
results = es_prod.scroll(scroll_id: results['_scroll_id'], scroll: '5m') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment