plans_import

This commit is contained in:
hsafaei
2025-11-09 12:25:26 +03:30
parent 4f7adb9865
commit 1a0485dd72
+40 -12
View File
@@ -46,7 +46,7 @@ def recersive_sections(
"parent_id": parent_id, "parent_id": parent_id,
"child_order": child_order, "child_order": child_order,
"tcode": tcode_label, "tcode": tcode_label,
"time_create": time.time() "time_create": int(round(time.time() * 1000))
} }
}) })
@@ -63,7 +63,7 @@ def recersive_sections(
) )
def import_paln_json_to_elastic(elastic, file_path, index_name): def import_paln_json_to_elastic(all_codes, elastic, file_path, index_name):
try: try:
# Read JSON file # Read JSON file
@@ -74,38 +74,49 @@ def import_paln_json_to_elastic(elastic, file_path, index_name):
except FileNotFoundError: except FileNotFoundError:
raise FileNotFoundError(f"File not found: {file_path}") raise FileNotFoundError(f"File not found: {file_path}")
total = len(data) print("-"*10, " ... import_paln_json_to_elastic ....", file_path)
total = len(list(data))
list_bulk = [] list_bulk = []
batch_size = 20 batch_size = 20
processed_count = 0 processed_count = 0
total_update = 0 total_update = 0
batch_size_number = 1 batch_size_number = 1
file_name = os.path.basename(file_path)
_name = os.path.splitext(file_name)[0]
code_name = _name
tcode_label = data.get("law_title") tcode_label = data.get("law_title")
if not tcode_label : item_found = [item for item in all_codes if item.get("title") == tcode_label]
print(" error tcode_label", file_path) if not item_found :
return item_found = [item for item in all_codes if item.get("title") == code_name]
if item_found :
tcode_label = code_name
else :
print("################### error tcode_label , code_name ....", tcode_label, code_name, file_path)
return
# Step 1: Add root node (the law itself) # Step 1: Add root node (the law itself)
root_id = uuid.uuid4().hex[:8] root_id = uuid.uuid4().hex[:8]
root_path = data.get("law_title") + "/" root_path = tcode_label + "/"
# list_bulk.append({ # list_bulk.append({
# "_id": root_id, # "_id": root_id,
# "_source": { # "_source": {
# "id": root_id, # "id": root_id,
# "title": data.get("law_title"), # "title": tcode_label,
# "version_key": "D49_oss120", # "version_key": "D49_oss120",
# "full_path": root_path, # "full_path": root_path,
# "parent_id": 0, # or 0 if you prefer integer root parent # "parent_id": 0, # or 0 if you prefer integer root parent
# "child_order": 0, # "child_order": 0,
# "tcode": tcode_label, # "tcode": tcode_label,
# "time_create": time.time() # "time_create": int(round(time.time() * 1000))
# } # }
# }) # })
# Step 2: Recursively process top-level sections # Step 2: Recursively process top-level sections
for idx, section in enumerate(data.get("sections", []), start=1): for idx, section in enumerate(data.get("sections", []), start=1):
print("..start..", idx, section["title"]) # print("..start..", idx, section["title"])
recersive_sections( recersive_sections(
section=section, section=section,
list_bulk=list_bulk, list_bulk=list_bulk,
@@ -119,13 +130,13 @@ def import_paln_json_to_elastic(elastic, file_path, index_name):
elastic.importBulkList(list_bulk, index_name) elastic.importBulkList(list_bulk, index_name)
total_update += len(list_bulk) total_update += len(list_bulk)
list_bulk = [] list_bulk = []
print("updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path) # print("updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path)
batch_size_number += 1 batch_size_number += 1
if list_bulk: if list_bulk:
elastic.importBulkList(list_bulk, index_name) elastic.importBulkList(list_bulk, index_name)
total_update += len(list_bulk) total_update += len(list_bulk)
print(" .... finish updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path) # print(" .... finish updated batch : ", batch_size_number, f" {total_update}/{total} --->", file_path)
batch_size_number += 1 batch_size_number += 1
########################## save manual for test ########################## save manual for test
@@ -168,12 +179,29 @@ if __name__ == "__main__":
path_base_map = "G:/Project2/gateway2_main/repo/_other/" path_base_map = "G:/Project2/gateway2_main/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map) elastic = ElasticHelper(host, password, "elastic", path_base_map)
all_codes = []
with open("./data/codes49.json", "r", encoding="utf8") as f:
codes_1 = json.load(f)
all_codes = codes_1
index_name='mj_plan' index_name='mj_plan'
# file_path=r'data/plans/gpt120b_49_1/data_1/آمار، برنامه و بودجه.json' # file_path=r'data/plans/gpt120b_49_1/data_1/آمار، برنامه و بودجه.json'
file_path=r'data/plans/gpt120b_49_1/data_1/' file_path=r'data/plans/gpt120b_49_1/data_1/'
for file in list_json_files(file_path): for file in list_json_files(file_path):
import_paln_json_to_elastic( import_paln_json_to_elastic(
all_codes=all_codes,
elastic=elastic,
file_path=file,
index_name=index_name
)
file_path=r'data/plans/gpt120b_49_1/data_2/'
for file in list_json_files(file_path):
import_paln_json_to_elastic(
all_codes=all_codes,
elastic=elastic, elastic=elastic,
file_path=file, file_path=file,
index_name=index_name index_name=index_name