Files
epo-api/upload.py
T

46 lines
1.6 KiB
Python

from elasticsearch import Elasticsearch
import sys
import os
import json
# elasticsearch-connection
# local
es = Elasticsearch({'host':'localhost', 'port': 9200, 'scheme': 'https'}, basic_auth=('elastic', 'elastic'), verify_certs=False, ssl_show_warn=False)
# uni-server
#es = Elasticsearch({'host':'beta.mowesta.com', 'port': 443, 'path_prefix':'elastic', 'scheme':'https'}, basic_auth=('elastic', 'nctLD+JV+HGl+YDEmAoc'))
index_name = "patentetest1"
# checks if elastic is up and creates the index if not already done
def check_elastic():
if not es.ping():
sys.exit("No connection to Elasticsearch")
if not es.indices.exists(index=index_name):
es.indices.create(index=index_name)
# takes the given path
# searches for all files in current directory and all sub-directories
# filters files for .json
# loads json into elasticsearch
def upload(path):
for root, dirs, files in os.walk(path):
for file in files:
with open(os.path.join(root, file), "r") as json_file:
if json_file.name.endswith('.json'):
try:
json_loaded = json.load(json_file)
id = json_file.name[-16:-5]
es.index(index=index_name, id=id, document=json_loaded)
except Exception as e:
print("--- Failed to upload " + json_file.name)
print(e)
# main - entry point
if __name__ == '__main__':
check_elastic()
if len(sys.argv) != 2:
sys.exit("No path given.")
upload(sys.argv[1])