Created
February 12, 2018 15:59
-
-
Save gquintana/a467d658aed8465e927d5a85005e108c to your computer and use it in GitHub Desktop.
Convert Elasticsearch search result to Bulk data
This file contains hidden or 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
#!/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