This commit is contained in:
hsafaei
2026-02-02 19:25:35 +03:30
parent 7962b6767e
commit 8456abe1a6
3 changed files with 179 additions and 16 deletions
+138 -16
View File
@@ -28,11 +28,11 @@ load_dotenv(dotenv_path="config.env")
# host = os.getenv("HOST_160_LOCAL")
# password = os.getenv("HOST_160_PASS")
# host = os.getenv("HOST_60")
# password = os.getenv("HOST_60_PASS")
host = os.getenv("HOST_60")
password = os.getenv("HOST_60_PASS")
host = os.getenv("HOST_133")
password = os.getenv("HOST_133_PASS")
# host = os.getenv("HOST_133")
# password = os.getenv("HOST_133_PASS")
# print(host)
# print(password)
@@ -41,6 +41,9 @@ password = os.getenv("HOST_133_PASS")
path_base_map = (
"G:\Project2\gateaway_py\elastic_backend/elastic_backend/app/schemas/mappings/"
)
# path_base_map = (
# "G:\Project2\tavasi_60\src\repo\_other/"
# )
elastic = ElasticHelper(host, password, "elastic", path_base_map)
text_normalize = TextNormalizer()
@@ -174,7 +177,7 @@ def import_project_to_elastic(
"tree_key": main_type_key,
"tree_label": project_title,
"main_type": "tree",
# "group_key" : "",
"group_key" : "موضوعی",
"title": title,
"parent_id": parent_id,
"child_order": child_order,
@@ -205,7 +208,7 @@ def import_project_to_elastic(
print(t2 - t1)
def import_tlist_projects():
def import_tlist_projects(index_name_o="gn_tree"):
config_env = load_dotenv(dotenv_path="./config.env")
MYSQL_username = os.getenv("MYSQL_username")
@@ -247,7 +250,7 @@ def import_tlist_projects():
for item in list_projects:
print("*" * 100)
print(item)
import_project_to_elastic(cursor, elastic, item["id"], item["title"], "gn_tree")
import_project_to_elastic(cursor, elastic, item["id"], item["title"], index_name_o)
cursor.close()
@@ -256,7 +259,7 @@ def import_option_to_elastic(
elastic,
main_key,
main_label,
group_key="monir_base",
group_key="عناوین انتخابی برنامه",
eid="",
index_name_o="gn_option",
):
@@ -279,7 +282,7 @@ def import_option_to_elastic(
elastic.es.index(index=index_name_o, id=eid, document=data)
def import_option_base_list(elastic):
def import_option_base_list(elastic, index_name_o="gn_option"):
op_list = [
{
"main_key": "calendar_main_type",
@@ -293,6 +296,10 @@ def import_option_base_list(elastic):
"main_key": "calendar_domain",
"main_label": "تقویم - حوزه درخواست",
},
{
"main_key": "calendar_audience",
"main_label": "تقویم - نوع مخاطبان",
},
{
"main_key": "calendar_format",
"main_label": "تقویم - قالب جلسه",
@@ -346,7 +353,7 @@ def import_option_base_list(elastic):
group_key = op["group_key"]
import_option_to_elastic(
elastic, op["main_key"], op["main_label"], group_key, eid
elastic, op["main_key"], op["main_label"], group_key, eid, index_name_o
)
@@ -361,6 +368,7 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
print(len(lines))
ostan_dic={}
ostan_list = []
number = 0
for line in lines:
items = line.strip('\n').split('\t')
@@ -374,12 +382,29 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
shahr = text_normalize.normalize_qanon(shahr)
if ostan not in ostan_dic:
ostan_list.append(ostan)
ostan_dic[ostan] = []
ostan_dic[ostan].append(shahr)
eid = 'iros_0'
main_key = "ir_ostan"
main_label = "استانهای ایران"
group_key = "استان و شهرهای ایران"
main_type = "list_simple"
data = {
"id": eid,
"version_key": "b1",
"main_key": main_key,
"main_label": main_label,
"group_key": group_key,
"titles": ostan_list,
}
elastic.es.index(index=index_name_o, id=eid, document=data)
number = 0
for key, value in ostan_dic.items():
number = number + 1
@@ -387,7 +412,7 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
shahrs = value
eid = 'iros_' + str(number)
main_key = "iros_" + str(number)
main_key = str(ostan)
main_label = "شهرهای استان " + ostan
group_key = "استان و شهرهای ایران"
main_type = "list_simple"
@@ -403,15 +428,112 @@ def import_option_ostan_shahr(elastic, index_name_o="gn_option"):
##### -------------------------------------------------------------
def convert_excel_data_to_json(item):
_id = "gnp" + str(item["id"])
tree_label = item["person_title"]
person_id = item["person_id"]
title = item["title"]
group_key = ""
if person_id == 1103 :
group_key = "استاد حسینی"
tree_label = "فهرست موضوعی جلسات استاد حسینی"
elif person_id == 1128 :
group_key = "استاد میرباقری"
tree_label = "فهرست موضوعی جلسات استاد میرباقری"
papa_id = item["papa_id"]
if papa_id == 0:
parent_id = str(0)
else:
parent_id = "gnp" + str(papa_id)
child_order = item["order"]
return {
"id": _id, # "gnt01741"
"version_key": "portal_2",
"tree_key": "tree" + str(person_id),
"tree_label": tree_label, # "فهرست موضوعات مسائل و پاسخها",
"group_key": group_key,
"main_type": "tree",
"title": title, # "پیامدهای روبنایی",
"parent_id": parent_id,
"child_order": child_order,
"tags": [],
"meta_info": {},
}
def import_excel_tree_monir(elastic, file_path, index_name_o = "gn_tree"):
data_excel = extract_excel_fields(file_path, sheet_name="Sheet1")
print(f'data_excel {len(data_excel)}')
try:
f_data = []
for i in data_excel:
f_data.append(convert_excel_data_to_json(i))
except Exception as e:
print(
f"*************",
)
traceback.print_exc()
bulk_data=[]
total_count = 0
data_list = f_data
i = 0
for data in data_list :
eid = data["id"]
i = i + 1
if len(bulk_data) >= 100:
total_count += len(bulk_data)
print('=' * 10, 'to_elastic , total='+str(total_count) , str(i)+ ', id=' + eid )
elastic.importBulkList(bulk_data, index_name_o)
bulk_data = []
bulk_data.append(
{
'_id' : eid,
'_source' : data
}
)
if len(bulk_data) > 0:
total_count += len(bulk_data)
print('=' * 10, ' to_elastic , total='+str(total_count) )
elastic.importBulkList(bulk_data, index_name_o)
bulk_data = []
print("end success import_to_elastic")
##### -------------------------------------------------------------
if __name__ == "__main__":
# ---مرحله اول
# import_tlist_projects()
###### -------------- gn_tree -----------------------------
# import_option_base_list(elastic)
# import_tlist_projects("gn_tree")
# file_path = r"app/../data/monir/041106_لیست تمام موضوعات.xlsx"
# import_excel_tree_monir(elastic, file_path, "gn_tree")
# import_option_ostan_shahr(elastic)
###### -------------- gn_option -----------------------------
# import_option_base_list(elastic, "gn_option")
# import_option_ostan_shahr(elastic, "gn_option")
###### -------------- -----------------------------
# elastic.backupIndexToZipfile("G:/Project2/111", "mn_sanad_temp", "mn_sanad")
# elastic.backupIndexToZipfile("G:/Project2/111", "mn_steps_temp", "mn_steps")
# elastic.backupIndexToZipfile("G:/Project2/111", "gn_calendar", "gn_calendar")