138 lines
4.2 KiB
Python
138 lines
4.2 KiB
Python
import os
|
|
import uuid
|
|
from app.common.data_helper import DataHelper
|
|
from pathlib import Path
|
|
from dotenv import load_dotenv
|
|
from elasticsearch import helpers
|
|
from elasticsearch.helpers import scan
|
|
|
|
from app.common.elastic_helper import ElasticHelper
|
|
|
|
load_dotenv(dotenv_path=r"D:\dataParsers\config.env")
|
|
|
|
host = os.getenv("HOST_99")
|
|
password = os.getenv("PASSWORD_99")
|
|
|
|
elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic")
|
|
|
|
|
|
def create_index_mashruh(
|
|
elastic: ElasticHelper,
|
|
source_index: str = "majles_mj_mashruh",
|
|
index_name: str = "majles_mj_mashruh",
|
|
batch_size: int = 1000,
|
|
) -> int:
|
|
"""
|
|
رکوردهای ایندکس مبدأ را میخواند، id قدیمی را در ref_id نگه میدارد،
|
|
id جدید میسازد و در ایندکس مقصد ذخیره میکند.
|
|
تعداد کل رکوردهای نوشتهشده را برمیگرداند.
|
|
"""
|
|
query = {"query": {"bool": {"filter": [{"term": {"version_key": "docx_1"}}]}}}
|
|
query2 = {"query": {"bool": {"filter": [{"term": {"version_key": "docx_1_1"}}]}}}
|
|
|
|
def flush(actions: list) -> int:
|
|
if not actions:
|
|
return 0
|
|
success, _ = helpers.bulk(elastic.es, actions)
|
|
return success
|
|
|
|
actions = []
|
|
mapping_ids = {}
|
|
total = 0
|
|
START = 10_000_000 # اولین عدد ۸ رقمی
|
|
|
|
counter = START
|
|
|
|
for hit in scan(elastic.es, index=source_index, query=query, size=batch_size):
|
|
doc = hit["_source"]
|
|
old_id = doc["id"]
|
|
# new_id = f"dx{uuid.uuid4().hex[:8]}"
|
|
new_id = f"dx{counter}"
|
|
counter += 1
|
|
mapping_ids[hit["_id"]] = new_id
|
|
doc["ref_id"] = old_id
|
|
doc["id"] = new_id
|
|
doc["version_key"] = "docx_1_1"
|
|
|
|
actions.append({"_index": index_name, "_id": doc["id"], "_source": doc})
|
|
|
|
if len(actions) >= batch_size:
|
|
total += flush(actions)
|
|
actions = []
|
|
|
|
total += flush(actions) # باقیماندهی آخر
|
|
DataHelper.save_orjson(
|
|
data=mapping_ids, path=r"D:\dataParsers\data\mapping_ids_mashruh.json"
|
|
)
|
|
return total
|
|
|
|
|
|
import re
|
|
|
|
|
|
def create_index_group_mashruh(
|
|
elastic: ElasticHelper,
|
|
mapping_ids_mashruh: Path = Path(r"D:\dataParsers\data\mapping_ids_mashruh.json"),
|
|
source_index: str = "majles_group_mashruh",
|
|
target_index: str = "majles_group_mashruh",
|
|
source_version: str = "docx_1",
|
|
target_version: str = "docx_1_1",
|
|
batch_size: int = 1000,
|
|
) -> int:
|
|
"""
|
|
کل اسناد (با تمام فیلدها از جمله امبدینگ) را کپی میکند،
|
|
id و merge_ids را با mapping جدید عوض میکند و ورژن را به target_version میبرد.
|
|
"""
|
|
mapping = DataHelper.load_orjson(mapping_ids_mashruh)
|
|
print(f"mapping_ids_mashruh {len(mapping)}")
|
|
|
|
query = {"query": {"bool": {"filter": [{"term": {"version_key": source_version}}]}}}
|
|
|
|
def flush(actions: list) -> int:
|
|
if not actions:
|
|
return 0
|
|
success, _ = helpers.bulk(elastic.es, actions)
|
|
return success
|
|
|
|
actions = []
|
|
total = 0
|
|
missing = 0
|
|
|
|
for hit in scan(elastic.es, index=source_index, query=query, size=batch_size):
|
|
doc = hit["_source"]
|
|
|
|
old_id = doc["id"]
|
|
m = re.search(r"-(\d+)$", old_id)
|
|
if m:
|
|
num = m.group(1) # "1"
|
|
base = old_id[: m.start()] # "X9hWwKEBp4aoUwS9R-3f"
|
|
|
|
if base not in mapping:
|
|
missing += 1
|
|
continue
|
|
|
|
doc["ref_id"] = old_id
|
|
doc["id"] = f"{mapping[base]}-{num}"
|
|
doc["merge_ids"] = [mapping[i] for i in doc["merge_ids"] if i in mapping]
|
|
doc["version_key"] = target_version
|
|
|
|
actions.append({"_index": target_index, "_id": doc["id"], "_source": doc})
|
|
|
|
if len(actions) >= batch_size:
|
|
total += flush(actions)
|
|
actions = []
|
|
|
|
total += flush(actions) # باقیماندهی آخر
|
|
print(f"copied {total}, skipped (no mapping) {missing}")
|
|
return total
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# count = create_index_mashruh(elastic)
|
|
count = create_index_group_mashruh(elastic)
|
|
print(f"{count} documents indexed.")
|
|
|
|
|
|
# cd D:\dataParsers\
|
|
# python -m app.update_ids_mashruh
|