import os, time
import requests
import json
import hashlib
from elasticsearch import Elasticsearch
es = Elasticsearch()
import traceback

# bpath = "./data_preprocessing/legal_word_txt/"

# dict_all = {}
# dirs = os.listdir(bpath)
# for x in dirs:
#     with open (bpath+x, "r", encoding="utf-8") as fp:
#         r = fp.read()
#         r = r.split("\n")
#         for x in r:
#             if x:
#                 dict_all[x] = True

# with open("./data_preprocessing/legal_word_all",'w',encoding="utf-8") as fp:
#     for x in dict_all:
#         fp.write(x+"\n")


with open("./data_preprocessing/legal_word_all",'r',encoding="utf-8") as fp:
    d = fp.read().split('\n')[:-1]
    for x in d:
        try:
            es.index(index="suggest", body={"legal_suggest":x}, id=hashlib.md5(x.encode(encoding='UTF-8')).hexdigest())
        except:
            print(traceback.format_exc())
            bv =1
            print("\n\n")
            time.sleep(5)