import_context_rules_v2

This commit is contained in:
hsafaei
2026-02-21 10:51:06 +03:30
parent 6a6bed90d7
commit 763490f395
2 changed files with 308 additions and 19 deletions
+307 -18
View File
@@ -179,6 +179,267 @@ def extract_json_files(elastic, file_json, file_import_ids, file_type="groups",
print("end success import_to_elastic")
def import_error_groups_json(elastic, file_json, file_import_ids, file_type="groups", version_key="v2"):
bulk_data = []
total_count = 0
jump_ids = []
if os.path.exists(file_import_ids):
with open(file_import_ids, "r") as f:
jump_ids = f.readlines()
index_name_o = "mj_qa_context"
data_json = DataHelper.load_orjson(file_json)
delete_ids = []
new_ids = []
map_prev_ids = {}
for qanon_id, data in data_json.items() :
print(
f"***** {file_type} : ",
index_name_o,
"=>",
" qanon_id=",
qanon_id,
)
try:
index_name_o = "mj_qa_context"
chuk_order = 0
index_name_i = "mj_qa_section"
items = data
if "chunks" in data :
items = data["chunks"]
for item in items:
if len(item) == 0:
continue
#--- این موارد خطا نخوردن لذا بروز رسانی لازم نیست
if len(item) == 1:
continue
eid_prev = item[1] + "_" + version_key
delete_ids.append(eid_prev)
section_mom_id = item[0]
eid = item[0] + "_" + version_key
new_ids.append(eid)
map_prev_ids[eid_prev] = eid
section_ids = item
chuk_order = chuk_order + 1
qanon_title = ""
qanon_etebar = ""
state_etebar = ""
ts_date = None
ts_ref = ""
title = ""
content = ""
for section_id in section_ids:
source = None
try:
res = elastic.get_document(index_name_i, section_id)
source = res["_source"]
except:
error = (
" section_id not found in qa_section :"
+ qanon_id
+ ":"
+ section_id
)
if log_file:
log_file.write(5 * "*" + error + "\n")
continue
if not qanon_title and "qanon_etebar" in source:
qanon_title = source["qanon_title"]
if not qanon_etebar and "qanon_etebar" in source:
qanon_etebar = source["qanon_etebar"]
if not ts_date and "ts_date" in source:
ts_date = source["ts_date"]
if not ts_ref and "ts_ref" in source:
ts_ref = source["ts_ref"]
if section_id == section_mom_id:
if "state_etebar" in source:
state_etebar = source["state_etebar"]
if "other_info" in source:
title = source["other_info"]["full_path"]
if content:
content += "\n"
content += source["content"]
data = {
"id": eid,
"section_mom_id": section_mom_id,
"version_key": "base_"+version_key,
"qanon_id": qanon_id,
"qanon_title": qanon_title,
"qanon_etebar": qanon_etebar,
"state_etebar": state_etebar,
"ts_date": ts_date,
"title": title,
"ts_ref": ts_ref,
"chuk_order": chuk_order,
"content": content,
"group_section_ids": section_ids,
"time_create": int(round(time.time() * 1000)),
}
bulk_data.append({"_id": eid, "_source": data})
if len(bulk_data) >= 100:
total_count += len(bulk_data)
print("=" * 10, "to_elastic , total=" + str(total_count), ", id=" + eid)
elastic.importBulkList(bulk_data, index_name_o)
bulk_data = []
jump_ids.append(qanon_id)
except Exception as e:
print("error in file " + qanon_id + str(e))
if len(bulk_data) > 0:
total_count += len(bulk_data)
print("=" * 10, " to_elastic , total=" + str(total_count))
if file_type == "rules" or file_type == "groups":
elastic.importBulkList(bulk_data, index_name_o)
else:
elastic.updateBulkList(bulk_data, index_name_o)
bulk_data = []
path_out = f"G:/Project2/102_gpu/v2/"
DataHelper.save_orjson(path = path_out + "error1_delete_ids.json", data = delete_ids)
DataHelper.save_orjson(path = path_out + "error1_new_ids.json", data = new_ids)
DataHelper.save_orjson(path = path_out + "error1_map_prev_ids.json", data = map_prev_ids)
with open(file_import_ids, "w") as f:
f.write("\n".join(jump_ids))
print("end success import_to_elastic")
def fill_error_context_json(elastic, file_json, file_import_ids, file_type="groups", version_key="v2"):
bulk_data = []
total_count = 0
# jump_ids = []
# if os.path.exists(file_import_ids):
# with open(file_import_ids, "r") as f:
# jump_ids = f.readlines()
index_name_o = "mj_qa_context"
data_json = DataHelper.load_orjson(file_json)
delete_ids = []
new_ids = []
map_prev_ids = {}
no_find_prev_ids = []
for qanon_id, data in data_json.items() :
print(
f"***** {file_type} : ",
index_name_o,
"=>",
" qanon_id=",
qanon_id,
)
try:
index_name_o = "mj_qa_context"
chuk_order = 0
index_name_i = "mj_qa_section"
items = data
if "chunks" in data :
items = data["chunks"]
for item in items:
if len(item) == 0:
continue
#--- این موارد خطا نخوردن لذا بروز رسانی لازم نیست
if len(item) == 1:
continue
eid = item[0] + "_" + version_key
found = False
for ee in item :
eid_prev = ee + "_" + version_key
if eid == eid_prev :
continue
context = []
chuk_order = 0
try :
res = elastic.get_document(index_name_o, eid_prev)
source = res["_source"]
if "context" in source :
context = source["context"]
chuk_order = source["chuk_order"]
found = True
break
except:
continue
if not found :
no_find_prev_ids.append({"qanon_id": qanon_id, "id" : eid})
continue
delete_ids.append(eid_prev)
section_mom_id = item[0]
new_ids.append(eid)
map_prev_ids[eid_prev] = eid
# chuk_order = chuk_order + 1
data = {
"context": context,
"chuk_order": chuk_order,
}
bulk_data.append({"_id": eid, "_source": data})
if len(bulk_data) >= 100:
total_count += len(bulk_data)
print("=" * 10, "to_elastic , total=" + str(total_count), ", id=" + eid)
elastic.updateBulkList(bulk_data, index_name_o)
bulk_data = []
# jump_ids.append(qanon_id)
except Exception as e:
print("error in file " + qanon_id + str(e))
if len(bulk_data) > 0:
total_count += len(bulk_data)
print("=" * 10, " to_elastic , total=" + str(total_count))
elastic.updateBulkList(bulk_data, index_name_o)
bulk_data = []
path_out = f"G:/Project2/102_gpu/v2/"
DataHelper.save_orjson(path = path_out + "error1_delete_ids.json", data = delete_ids)
DataHelper.save_orjson(path = path_out + "error1_new_ids.json", data = new_ids)
DataHelper.save_orjson(path = path_out + "error1_map_prev_ids.json", data = map_prev_ids)
DataHelper.save_orjson(path = path_out + "error1_no_find_prev_ids.json", data = no_find_prev_ids)
# with open(file_import_ids, "w") as f:
# f.write("\n".join(jump_ids))
print("end success import_to_elastic")
@@ -583,7 +844,11 @@ def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na
if __name__ == "__main__":
# v2 :
# این نسخه با گروه بندی با توجه به nlp_childs تولید شد
### ----------------------------------------------
# data_key = "groups"
# log_file = open(f"G:/Project2/102_gpu/v2/log_{data_key}_1.txt", mode="w", encoding="utf8")
@@ -591,24 +856,24 @@ if __name__ == "__main__":
# file_import_ids = f"G:/Project2/102_gpu/v2/{data_key}_import_ids.txt"
# extract_json_files(elastic, file, file_import_ids, data_key, "v2")
# log_file.close()
# ### ----------------------------------------------
data_key = "contexts"
log_file = open(f"G:/Project2/102_gpu/log_{data_key}_1.txt", mode="w", encoding="utf8")
filezip = f"G:/Project2/102_gpu/{data_key}.zip"
file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt"
extract_zip_files(elastic, filezip, file_import_ids, data_key, "v2")
log_file.close()
# data_key = "contexts"
# log_file = open(f"G:/Project2/102_gpu/log_{data_key}_1.txt", mode="w", encoding="utf8")
# filezip = f"G:/Project2/102_gpu/{data_key}.zip"
# file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt"
# extract_zip_files(elastic, filezip, file_import_ids, data_key, "v2")
# log_file.close()
# ### ----------------------------------------------
data_key = "terms"
log_file = open(
f"G:/Project2/102_gpu/log_{data_key}_1.txt", mode="w", encoding="utf8"
)
filezip = f"G:/Project2/102_gpu/{data_key}.zip"
file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt"
extract_zip_files(elastic, filezip, file_import_ids, data_key, "v2")
log_file.close()
# # ### ----------------------------------------------
# data_key = "terms"
# log_file = open(
# f"G:/Project2/102_gpu/log_{data_key}_1.txt", mode="w", encoding="utf8"
# )
# filezip = f"G:/Project2/102_gpu/{data_key}.zip"
# file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt"
# extract_zip_files(elastic, filezip, file_import_ids, data_key, "v2")
# log_file.close()
# ### ----------------------------------------------
# data_key = "rules"
@@ -630,7 +895,7 @@ if __name__ == "__main__":
# )
# ### ----------------------------------------------
# elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_context", "mj_qa_context")
# elastic.backupIndexToZipfile("G:/Project2/102_gpu/v2", "mj_qa_context", "mj_qa_context")
# elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_rule", "mj_qa_rule")
# mapping_file_path = "G:/\Project2/tavasi_160/src/repo/_other/majles/mj_qa_context.json"
@@ -640,5 +905,29 @@ if __name__ == "__main__":
# elastic.restorFileToElastic2('G:/Project2/102_gpu/mj_qa_rule.zip', 'mj_qa_rule', 'majles', False, 'mj_qa_rule', mapping_file_path)
# ### ----------------------------------------------
### -------- repai error 1 groups--------------------------------------
### -------------------------------------------
# در گروه بندی که با nlp_child
# بود ، ترتیب بچه درست لحاظ نشده بود و باعث خطا در محتوای گروههای چند سکشنی شد
# لذا بعد از شناسایی و اصلاح موارد مذکور جدید اضافه شد و قبلی ها پاک شد
# data_key = "groups"
# log_file = open(f"G:/Project2/102_gpu/v2/error_log_{data_key}_1.txt", mode="w", encoding="utf8")
# file = f"G:/Project2/102_gpu/v2/f1_valid_5099_mj_qa_chunked_ids_28_11_1404.json"
# file_import_ids = f"G:/Project2/102_gpu/v2/error1_{data_key}_import_ids.txt"
# import_error_groups_json(elastic, file, file_import_ids, data_key, "v2")
# log_file.close()
# fill_error_context_json(elastic, file, file_import_ids, data_key, "v2")
# log_file.close()
### -------------------------------------------
# python -m dataParsers.app.import_context_rules
# print(":D")