first commit
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
from dotenv import load_dotenv
|
||||
import os, sys, re
|
||||
from app.common.elastic_helper import ElasticHelper
|
||||
from app.common.data_helper import DataHelper
|
||||
from pydantic import BaseModel
|
||||
from typing import Literal, List, Dict, Any, Optional
|
||||
from pathlib import Path
|
||||
from elasticsearch import helpers
|
||||
from elasticsearch.helpers import scan
|
||||
|
||||
load_dotenv(dotenv_path=r"D:\dataParsers\config.env")
|
||||
|
||||
|
||||
host = os.getenv("HOST_99")
|
||||
password = os.getenv("PASSWORD_99")
|
||||
|
||||
print(f"host {host} password {password}")
|
||||
elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic")
|
||||
|
||||
|
||||
def creat_index_majles_group_mashruh(
|
||||
elastic: ElasticHelper,
|
||||
input_path=Path(r"D:\dataParsers\data\finall_mashruh.json"),
|
||||
index_name="majles_group_mashruh",
|
||||
_field="ai_keywords",
|
||||
version_key="mis4-119b",
|
||||
batch_size=1000,
|
||||
):
|
||||
|
||||
actions = []
|
||||
total = 0
|
||||
success = 0
|
||||
failed = 0
|
||||
|
||||
print("=" * 80)
|
||||
print("START BULK EMBEDDING UPDATE")
|
||||
print(f"Index : {index_name}")
|
||||
print(f"Input path : {input_path}")
|
||||
print(f"Batch size : {batch_size}")
|
||||
print("=" * 80)
|
||||
|
||||
in_data = DataHelper.load_orjson(input_path)
|
||||
# in_data = list(input_path.rglob("*.json"))
|
||||
print(f"in_data {len(in_data)}")
|
||||
|
||||
############################################################
|
||||
query = {
|
||||
"_source": ["id"],
|
||||
"query": {
|
||||
"bool": {
|
||||
# "filter": [{"term": {"version_key": "base_v3"}}],
|
||||
"must": [{"exists": {"field": "owl"}}],
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
process_ids = set()
|
||||
# hit["_id"]
|
||||
# for hit in scan(
|
||||
# elastic.es, # اگر نام کلاینت داخل ElasticHelper چیز دیگری است، آن را جایگزین کنید
|
||||
# # index="majles_mj_qa_rule",
|
||||
# index="majles_mj_qa_context",
|
||||
# query=query,
|
||||
# size=1000,
|
||||
# )
|
||||
# )
|
||||
|
||||
print(f"process_ids {len(process_ids)} => ")
|
||||
# print(f"process_ids {len(process_ids)} => {list(process_ids)[1]}")
|
||||
|
||||
############################################################
|
||||
|
||||
for _id, data in in_data.items():
|
||||
|
||||
try:
|
||||
# data = DataHelper.load_orjson(j_path)
|
||||
|
||||
doc_id = _id
|
||||
# embedding = data["embed"]
|
||||
_data = []
|
||||
for item in data:
|
||||
_data.append(
|
||||
{
|
||||
"version_key": version_key,
|
||||
"main_type": item["_key"],
|
||||
"sub_type": item["type"],
|
||||
"label": item["keyword"],
|
||||
}
|
||||
)
|
||||
if doc_id in process_ids:
|
||||
# print(f"doc_id {doc_id}")
|
||||
continue
|
||||
# doc_id = re.sub(r"(_v3)(\d+)$", r"\1-\2", data["id"])
|
||||
# print(f"doc_id {doc_id}")
|
||||
# return
|
||||
|
||||
action = {
|
||||
"_op_type": "update",
|
||||
"_index": index_name,
|
||||
"_id": doc_id,
|
||||
"doc": {_field: _data},
|
||||
}
|
||||
|
||||
actions.append(action)
|
||||
total += 1
|
||||
|
||||
# Send batch
|
||||
if len(actions) >= batch_size:
|
||||
|
||||
for ok, result in helpers.streaming_bulk(
|
||||
elastic.es,
|
||||
actions,
|
||||
chunk_size=batch_size,
|
||||
raise_on_error=False,
|
||||
raise_on_exception=False,
|
||||
):
|
||||
if ok:
|
||||
success += 1
|
||||
else:
|
||||
failed += 1
|
||||
print("ERROR:", result)
|
||||
|
||||
print(
|
||||
f"processed={total:,} | "
|
||||
f"success={success:,} | "
|
||||
f"failed={failed:,}"
|
||||
)
|
||||
|
||||
actions.clear()
|
||||
|
||||
except Exception as e:
|
||||
failed += 1
|
||||
print(f" {type(e).__name__}: {e}")
|
||||
|
||||
# Remaining documents
|
||||
if actions:
|
||||
|
||||
for ok, result in helpers.streaming_bulk(
|
||||
elastic.es,
|
||||
actions,
|
||||
chunk_size=batch_size,
|
||||
raise_on_error=False,
|
||||
raise_on_exception=False,
|
||||
):
|
||||
if ok:
|
||||
success += 1
|
||||
else:
|
||||
failed += 1
|
||||
print("ERROR:", result)
|
||||
|
||||
print()
|
||||
print("=" * 80)
|
||||
print("BULK UPDATE FINISHED")
|
||||
print(f"Total : {total:,}")
|
||||
print(f"Success: {success:,}")
|
||||
print(f"Failed : {failed:,}")
|
||||
print("=" * 80)
|
||||
|
||||
|
||||
def creat_index_majles_context_section(
|
||||
elastic: ElasticHelper,
|
||||
input_path=Path(r"D:\dataParsers\data\finall_qanon_phase1.json"),
|
||||
index_name="majles_mj_qa_context",
|
||||
_field="ai_keywords",
|
||||
version_key="mis4-119b",
|
||||
batch_size=1000,
|
||||
):
|
||||
|
||||
actions = []
|
||||
total = 0
|
||||
success = 0
|
||||
failed = 0
|
||||
|
||||
print("=" * 80)
|
||||
print("START BULK EMBEDDING UPDATE")
|
||||
print(f"Index : {index_name}")
|
||||
print(f"Input path : {input_path}")
|
||||
print(f"Batch size : {batch_size}")
|
||||
print("=" * 80)
|
||||
|
||||
in_data = DataHelper.load_orjson(input_path)
|
||||
# in_data = list(input_path.rglob("*.json"))
|
||||
print(f"in_data {len(in_data)}")
|
||||
|
||||
############################################################
|
||||
query = {
|
||||
"_source": ["id"],
|
||||
"query": {
|
||||
"bool": {
|
||||
# "filter": [{"term": {"version_key": "base_v3"}}],
|
||||
"must": [{"exists": {"field": "owl"}}],
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
process_ids = set()
|
||||
# hit["_id"]
|
||||
# for hit in scan(
|
||||
# elastic.es, # اگر نام کلاینت داخل ElasticHelper چیز دیگری است، آن را جایگزین کنید
|
||||
# # index="majles_mj_qa_rule",
|
||||
# index="majles_mj_qa_context",
|
||||
# query=query,
|
||||
# size=1000,
|
||||
# )
|
||||
# )
|
||||
|
||||
print(f"process_ids {len(process_ids)} => ")
|
||||
# print(f"process_ids {len(process_ids)} => {list(process_ids)[1]}")
|
||||
|
||||
############################################################
|
||||
|
||||
for _id, data in in_data.items():
|
||||
|
||||
try:
|
||||
|
||||
doc_id = _id
|
||||
# embedding = data["embed"]
|
||||
_data = []
|
||||
for item in data:
|
||||
_data.append(
|
||||
{
|
||||
"version_key": version_key,
|
||||
"main_type": item["_key"],
|
||||
"sub_type": item["type"],
|
||||
"label": item["keyword"],
|
||||
}
|
||||
)
|
||||
if doc_id in process_ids:
|
||||
# print(f"doc_id {doc_id}")
|
||||
continue
|
||||
|
||||
action = {
|
||||
"_op_type": "update",
|
||||
"_index": index_name,
|
||||
"_id": doc_id,
|
||||
"doc": {_field: _data},
|
||||
}
|
||||
|
||||
actions.append(action)
|
||||
total += 1
|
||||
|
||||
# Send batch
|
||||
if len(actions) >= batch_size:
|
||||
|
||||
for ok, result in helpers.streaming_bulk(
|
||||
elastic.es,
|
||||
actions,
|
||||
chunk_size=batch_size,
|
||||
raise_on_error=False,
|
||||
raise_on_exception=False,
|
||||
):
|
||||
if ok:
|
||||
success += 1
|
||||
else:
|
||||
failed += 1
|
||||
print("ERROR:", result)
|
||||
|
||||
print(
|
||||
f"processed={total:,} | "
|
||||
f"success={success:,} | "
|
||||
f"failed={failed:,}"
|
||||
)
|
||||
|
||||
actions.clear()
|
||||
|
||||
except Exception as e:
|
||||
failed += 1
|
||||
print(f" {type(e).__name__}: {e}")
|
||||
|
||||
# Remaining documents
|
||||
if actions:
|
||||
|
||||
for ok, result in helpers.streaming_bulk(
|
||||
elastic.es,
|
||||
actions,
|
||||
chunk_size=batch_size,
|
||||
raise_on_error=False,
|
||||
raise_on_exception=False,
|
||||
):
|
||||
if ok:
|
||||
success += 1
|
||||
else:
|
||||
failed += 1
|
||||
print("ERROR:", result)
|
||||
|
||||
print()
|
||||
print("=" * 80)
|
||||
print("BULK UPDATE FINISHED")
|
||||
print(f"Total : {total:,}")
|
||||
print(f"Success: {success:,}")
|
||||
print(f"Failed : {failed:,}")
|
||||
print("=" * 80)
|
||||
|
||||
|
||||
# creat_index_majles_group_mashruh(elastic=elastic)
|
||||
creat_index_majles_context_section(elastic=elastic)
|
||||
|
||||
# cd D:\dataParsers\
|
||||
# python -m app.update_keywords
|
||||
Reference in New Issue
Block a user