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 = "cpc" # 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): settings = { "analysis": { "analyzer": { "searchabilityAnalyzer": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "asciifolding" ] } } } } es.indices.create(index=index_name, settings=settings) # takes the given path to the cpc-json # loads json into elasticsearch def upload(path): with open(path, "r", encoding='utf-8') as json_file: try: json_loaded = json.load(json_file) keys = list(json_loaded.keys()) id_counter = 0 for key in keys: for entry in json_loaded[key]: es.index(index=index_name, id=id_counter, document=entry) id_counter += 1 except Exception as e: print("--- Failed to upload CPC") print(e) # main - entry point if __name__ == '__main__': check_elastic() if len(sys.argv) != 2: sys.exit("No path given.") upload(sys.argv[1])