Skip to content

Instantly share code, notes, and snippets.

@simonw
Last active November 18, 2016 18:36
Show Gist options
  • Select an option

  • Save simonw/99d2c60f0218b680d59b4622bc5f9d37 to your computer and use it in GitHub Desktop.

Select an option

Save simonw/99d2c60f0218b680d59b4622bc5f9d37 to your computer and use it in GitHub Desktop.
Download and dump contents of a remote Elasticsearch index to a local newline-separated JSON file using Python
from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan
import json
REMOTE_URL = 'https://username:password@elasticsearch.example.com:9200/'
LOCAL_FILE = '/tmp/elasticsearch-dump.txt'
INDEX = 'my-index'
DOC_TYPE = 'document'
client = Elasticsearch([REMOTE_URL])
fp = open(LOCAL_FILE, 'w')
for row in scan(client, query={}, index=INDEX, doc_type=DOC_TYPE):
json.dump(row, fp)
fp.write("\n")
fp.flush() # So you can tail -f the file
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment