Skip to content

Instantly share code, notes, and snippets.

@afym
Created July 19, 2017 21:45
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 afym/f1ac96d2fae449b3392c39d69b20781a to your computer and use it in GitHub Desktop.
Save afym/f1ac96d2fae449b3392c39d69b20781a to your computer and use it in GitHub Desktop.
#!/bin/sh
# 0. Some constants to re-define to match your environment
ES_HOST=localhost:9200
ES_INDEX=bookingmotor/hotels
JSON_FILE_IN=/home/angel/personal/elasticsearch/result-14-v1.json
JSON_FILE_OUT=/home/angel/personal/elasticsearch/result-14-bulk-v1.json
# 1. Python code to transform your JSON file
PYTHON="import json,sys;
out = open('$JSON_FILE_OUT', 'w');
with open('$JSON_FILE_IN') as json_in:
docs = json.loads(json_in.read());
for doc in docs:
out.write('%s\n' % json.dumps({'index': {}}));
out.write('%s\n' % json.dumps(doc, indent=0).replace('\n', ''));
"
# 2. run the Python script from step 1
python -c "$PYTHON"
# 3. use the output file from step 2 in the curl command
curl -s -XPOST $ES_HOST/$ES_INDEX/_bulk --data-binary @$JSON_FILE_OUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment