105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
from pydantic import BaseModel
|
|
from typing import List, Dict
|
|
from app.common.elastic_helper import ElasticHelper
|
|
from app.common.data_helper import DataHelper
|
|
from pydantic import BaseModel
|
|
from typing import List
|
|
from pathlib import Path
|
|
|
|
|
|
class NewArch(BaseModel):
|
|
id: str
|
|
ref_id: str
|
|
ref_key: str
|
|
begin_date: str
|
|
begin_year: int
|
|
author: str
|
|
branch: str
|
|
title: str
|
|
content: str
|
|
meet_no: int
|
|
permit_tags: str
|
|
format: str
|
|
embedding: List
|
|
|
|
|
|
host = "http://192.168.23.99:6900"
|
|
password = "5T6xbblcix3GdCGnYEk6"
|
|
print(f"host {host} password {password}")
|
|
elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic")
|
|
|
|
|
|
def do_convert(
|
|
elastic,
|
|
embed_json_path=Path(r"D:\init_mahdi\dataParsers\data\monir_mn_voice_sanad"),
|
|
dest_index_name="monir_mn_voice_sanad",
|
|
bulk_size=200,
|
|
):
|
|
|
|
map_mn_sanad = {
|
|
i["_id"]: i["_source"]
|
|
for i in DataHelper.load_orjson(r"D:\init_mahdi\dataParsers\data\mn_sanad.json")
|
|
}
|
|
map_mn_voice = {
|
|
i["_id"]: i["_source"]
|
|
for i in DataHelper.load_orjson(r"D:\init_mahdi\dataParsers\data\mn_voice.json")
|
|
}
|
|
|
|
bulk_list = []
|
|
f_counter = 0
|
|
total = 0
|
|
for j_path in embed_json_path.rglob("*.json"):
|
|
j_data = DataHelper.load_orjson(j_path)
|
|
total += len(j_data)
|
|
for data in j_data:
|
|
data = data["_source"]
|
|
index_key = data["index_key"]
|
|
mom_id = data["mom_id"]
|
|
# if index_key == "mn_voice":
|
|
# elif index_key == "mn_sanad":
|
|
try:
|
|
metadata = map_mn_sanad[mom_id]
|
|
except:
|
|
metadata = map_mn_voice[mom_id]
|
|
metadata["permit_tags"] = ""
|
|
metadata["begin_date"] = metadata["begin_date"]
|
|
if len(metadata["begin_date"]) > 0:
|
|
metadata["begin_year"] = int(metadata["begin_date"].split("-")[0])
|
|
else:
|
|
metadata["begin_year"] = 0
|
|
# "begin_date": "2013-11-05T00:00:00",
|
|
|
|
new_data = NewArch(
|
|
id=data["id"],
|
|
ref_key=index_key,
|
|
ref_id=mom_id,
|
|
author=metadata["author"],
|
|
branch=metadata["branch"],
|
|
title=metadata["title"],
|
|
content=data["content"],
|
|
meet_no=metadata["meet_no"],
|
|
permit_tags=metadata["permit_tags"],
|
|
format=metadata["format"],
|
|
begin_date=metadata["begin_date"],
|
|
begin_year=metadata["begin_year"],
|
|
embedding=data["embedding"],
|
|
)
|
|
bulk_list.append({"_id": new_data.id, "_source": new_data.model_dump()})
|
|
|
|
if len(bulk_list) >= bulk_size:
|
|
f_counter += len(bulk_list)
|
|
elastic.importBulkList(listData=bulk_list, index_name=dest_index_name)
|
|
bulk_list = []
|
|
print(f"--- {f_counter}/{total} ---")
|
|
|
|
if len(bulk_list) > 0:
|
|
f_counter += len(bulk_list)
|
|
elastic.importBulkList(listData=bulk_list, index_name=dest_index_name)
|
|
bulk_list = []
|
|
print(f"--- {f_counter}/{total} ---")
|
|
|
|
|
|
do_convert(elastic=elastic)
|
|
# cd dataParsers
|
|
# python -m app.update_monir_mn_voice_sanad
|