Last active
August 29, 2015 14:19
-
-
Save abronner/2c0e0dba0e998eb3a4b1 to your computer and use it in GitHub Desktop.
Elasticsearch-Toronto Meetup: Zero Downtime (April 21, 2015)
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
# source: https://github.com/elastic/elasticsearch-py/blob/master/elasticsearch/helpers/__init__.py | |
def reindex(client, source_index, target_index, query=None, target_client=None, chunk_size=500, scroll='5m', scan_kwargs={}, bulk_kwargs={}): | |
target_client = client if target_client is None else target_client | |
docs = helpers.scan(client, query=query, index=source_index, scroll=scroll, **scan_kwargs) | |
def _change_doc_index(hits, index): | |
for h in hits: | |
h['_index'] = index | |
# SOME MODIFICATION TO THE DOCUMENT: | |
h['_source']['first_name'] = h['_source']['username'].split()[0] | |
yield h | |
kwargs = { | |
'stats_only': True, | |
} | |
kwargs.update(bulk_kwargs) | |
return helpers.bulk(target_client, _change_doc_index(docs, target_index), chunk_size=chunk_size, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment