From 1a0485dd7296f5306451c7c148643cfef1cc011f Mon Sep 17 00:00:00 2001 From: hsafaei Date: Sun, 9 Nov 2025 12:25:26 +0330 Subject: [PATCH] plans_import --- app/for_ai/plans_import.py | 52 +++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/app/for_ai/plans_import.py b/app/for_ai/plans_import.py index 1253977..9d716db 100644 --- a/app/for_ai/plans_import.py +++ b/app/for_ai/plans_import.py @@ -46,7 +46,7 @@ def recersive_sections( "parent_id": parent_id, "child_order": child_order, "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: # Read JSON file @@ -74,38 +74,49 @@ def import_paln_json_to_elastic(elastic, file_path, index_name): except FileNotFoundError: 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 = [] batch_size = 20 processed_count = 0 total_update = 0 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") - if not tcode_label : - print(" error tcode_label", file_path) - return + item_found = [item for item in all_codes if item.get("title") == tcode_label] + if not item_found : + 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) root_id = uuid.uuid4().hex[:8] - root_path = data.get("law_title") + "/" + root_path = tcode_label + "/" # list_bulk.append({ # "_id": root_id, # "_source": { # "id": root_id, - # "title": data.get("law_title"), + # "title": tcode_label, # "version_key": "D49_oss120", # "full_path": root_path, # "parent_id": 0, # or 0 if you prefer integer root parent # "child_order": 0, # "tcode": tcode_label, - # "time_create": time.time() + # "time_create": int(round(time.time() * 1000)) # } # }) # Step 2: Recursively process top-level sections for idx, section in enumerate(data.get("sections", []), start=1): - print("..start..", idx, section["title"]) + # print("..start..", idx, section["title"]) recersive_sections( section=section, 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) total_update += len(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 if list_bulk: elastic.importBulkList(list_bulk, index_name) 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 ########################## save manual for test @@ -168,12 +179,29 @@ if __name__ == "__main__": path_base_map = "G:/Project2/gateway2_main/repo/_other/" 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' # file_path=r'data/plans/gpt120b_49_1/data_1/آمار، برنامه و بودجه.json' file_path=r'data/plans/gpt120b_49_1/data_1/' for file in list_json_files(file_path): 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, file_path=file, index_name=index_name