Skip to content

Instantly share code, notes, and snippets.

@gquintana
Created February 12, 2018 15:59
Show Gist options
  • Save gquintana/a467d658aed8465e927d5a85005e108c to your computer and use it in GitHub Desktop.
Save gquintana/a467d658aed8465e927d5a85005e108c to your computer and use it in GitHub Desktop.
Convert Elasticsearch search result to Bulk data
#!/usr/bin/env python
import json
import sys
import os
def read_search(in_file_name):
with open(in_file_name, 'r') as json_file:
docs = json.load(json_file)
return docs
def create_action(action, index, type, id):
return {action: {"_index" : index, "_type": type, "_id": id}}
def write_bulk(docs, action, out_file_name):
with open(out_file_name, 'w') as out_file:
for doc in docs:
index = doc["_index"]
type = doc["_type"]
id = doc["_id"]
json.dump(create_action(action, index, type, id), out_file)
out_file.write('\n')
if action == "index":
source = doc["_source"]
json.dump(source, out_file)
out_file.write('\n')
if __name__ == '__main__':
if len(sys.argv) < 4:
print "Usage: search2bulk.py [delete|index] search.json bulk.json"
quit(1)
action = sys.argv[1]
in_file_name = sys.argv[2]
out_file_name = sys.argv[3]
docs = read_search(in_file_name)
write_bulk(docs, action, out_file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment