Skip to content

Instantly share code, notes, and snippets.

@timdorr
Last active December 2, 2016 16:51
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 timdorr/6c0e028fe0dc527abba34fc39c444f35 to your computer and use it in GitHub Desktop.
Save timdorr/6c0e028fe0dc527abba34fc39c444f35 to your computer and use it in GitHub Desktop.
Clone (import/export) an Elasticsearch index
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