import mariadb from dotenv import load_dotenv import os import datetime import json import uuid import pandas as pd import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) from app.common.elastic_helper import ElasticHelper from app.common.general_functions import * from app.common.text_normalizer import TextNormalizer def import_from_jahat_entities(elastic, index_name_i, index_name_o): body={} try: list_ = elastic.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) data = list(list_) total = len(data) count = 0 bulk_data = [] total_count = 0 for mentry in data: count += 1 entry = mentry["source"] _id = entry.get("id", "") type_id = entry.get("type_id", 0) # type_id = 1 # مسائل اولیه # type_id = 2 # مساله # type_id = 3 # پاسخ # فعلا مساله ها را وارد می کنیم if not type_id in [1,2] : continue title = entry.get("title", "") files = entry.get("files", []) product = entry.get("product", "") description = entry.get("description", "") text = entry.get("text", "") method = entry.get("method", "") target = entry.get("target", "") summary = entry.get("summary", "") links = entry.get("links", []) subject = entry.get("subject", []) # { # "order": 0, # "id": 884, # "title": "نظام سلامت" # }, # { # "order": 0, # "id": 888, # "title": "کشاورزی" # } crition = entry.get("crition", []) # "properties":{ # "id": { "type": "keyword" }, # "entity_id": { "type": "keyword" }, # "entity_title": { "type": "keyword" }, # "description": { "type": "text" } # } questions = entry.get("questions", []) # "properties":{ # "id": { "type": "keyword" }, # "title": { "type": "text" }, # } notes = entry.get("notes", []) # "properties":{ # "id": { "type": "keyword" }, # "text": { "type": "text" }, # "resource": { "type": "text" }, # "description": { "type": "text" }, # "link": { "type": "text" }, # "file": { "type": "keyword" } # } necessity = entry.get("necessity", []) # "properties":{ # "id": { "type": "keyword" }, # "description": { "type": "text" }, # "file": { "type": "keyword" }, # "title": { # "type": "text", # "fields": { # "keyword": { # "type": "keyword", # "ignore_above": 256 # } # } # } # entities = entry.get("entities", []) #--- فیلدهای خالی یا بی ارزش # html = entry.get("html", "") # keywords = entry.get("keywords", []) # tags = entry.get("tags", []) # critionvalid = entry.get("critionvalid", []) # validation = entry.get("validation", []) # products = entry.get("products", []) # state_id = entry.get("state_id", 0) # Weight = entry.get("Weight", 0) # score = entry.get("score", 0) # date_edit = entry.get("date_edit", "") time_edit = entry.get("time_edit", "") date_create = entry.get("date_create", "") user_edit = entry.get("user_edit", 0) user_create = entry.get("user_create", 0) user_log = entry.get("user_log", []) file_main = {} files_new = [] for file in files : if "file" in file : file["link"] = file["file"] del file["file"] if not file_main and is_image_file(file["link"]) : file_main = file else : files_new.append(file) relations = [] if crition : for item in crition : if not item["entity_title"] : continue title = item["entity_title"].replace("* ", "").strip() new = { "id": item["entity_id"], "title": title, "relation_type": "معیار صحت" } if "description" in item : new["description"] = item["description"] relations.append(new) sections = [] if notes : child_order = 0 for item in notes : child_order = child_order + 1 sections.append({ "id" : _id + "_no" + str(child_order), "section_type" : "نکات", "child_order" : child_order, "title" : item["title"], "description" : item["description"] }) if necessity : child_order = 0 for item in necessity : child_order = child_order + 1 sections.append({ "id" : _id + "_nc" + str(child_order), "section_type" : "ضرورت", "child_order" : child_order, "title" : item["title"], "file" : item["file"], "description" : item["description"] }) if description : sections.append({ "id" : _id + "_de", "section_type" : "بیان", "description" : description }) if text : sections.append({ "id" : _id + "_tx", "section_type" : "توضیح", "description" : text }) if product : sections.append({ "id" : _id + "_pr", "section_type" : "محصول", "description" : product }) if method : sections.append({ "id" : _id + "_mt", "section_type" : "روش", "description" : method }) if target : sections.append({ "id" : _id + "_ta", "section_type" : "هدف", "description" : target }) if summary : sections.append({ "id" : _id + "_sm", "section_type" : "چکیده", "description" : summary }) # subject ?? data = { "id" : _id, "main_type" : "مساله", "other_type" : "جهادعلمی", "title" : title, "file_main" : file_main, "files" : files_new, "sections" : sections, "relations" : relations, "links" : links, "user_logs" : user_log, "user_create" : user_create, "user_edit" : user_edit, "time_create" : date_create, "time_edit" : time_edit, } bulk_data.append({"_id": _id, "_source": data}) if len(bulk_data) > 100: total_count += len(bulk_data) print("=" * 10, "to_elastic , total=" + str(total_count)) elastic.importBulkList(bulk_data, index_name_o) bulk_data = [] # break 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 = [] def import_from_crition_entities(elastic, index_name_i, index_name_o): body={} try: list_ = elastic.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) data = list(list_) total = len(data) count = 0 bulk_data = [] total_count = 0 for mentry in data: count += 1 entry = mentry["source"] _id = entry.get("id", "") type_id = entry.get("type_id", 4) type_id = 4 # معیار title = entry.get("title", "") files = entry.get("files", []) if isinstance(files, dict) : files = [files] unfavorite = entry.get("unfavorite", "") if not isinstance(unfavorite, list) : unfavorite = unfavorite.split("•") favorite = entry.get("favorite", "") if not isinstance(favorite, list) : favorite = favorite.split("•") text = entry.get("text", "") description = entry.get("description", "") summary = entry.get("summary", "") result = entry.get("result", {}) # { # "type": "پیامدهای ساختاری-نهادی در هر منطقه", # "title": "ساختارهای توسعه حضور آزادانه، آگاهانه، مسئولانه و احسان و ایثارپایۀ مردم، با محوریت علماء دینی، و ارتباط صمیمانه و محبت‌آمیز با حاکمیت در مدیریت تحقق پیشرفت و سبک زندگی اسلامی‌ـ‌ایرانی شکل ‌گرفته و نهادینه شده است (چرخه حضور)." # } links = entry.get("links", []) fish = entry.get("fish", []) # { # "text": "کار جهادی باید هدفمند، درست متوجه به آرمانها و هوشمندانه و عاقلانه و دشمن‌شکن باشد. یعنی به همان معنائی که ما مبارزه ‌را در اصطلاح معمولی به کار میبریم: «دارم مبارزه میکنم؛ این یک مبارزه است.» این، یک تعبیر مصطلح است. در جهاد، این معنا ‌هست.", # "resource": "بیانات در بازدید از پژوهشکده رویان", # "description": "", # "date_text": "25/04/1386" # } subject = entry.get("subject", []) # { # "order": 0, # "id": 884, # "title": "نظام سلامت" # }, # { # "order": 0, # "id": 888, # "title": "کشاورزی" # } # entities = entry.get("entities", []) #--- فیلدهای خالی یا بی ارزش # html = entry.get("html", "") # keywords = entry.get("keywords", []) # tags = entry.get("tags", []) # date_edit = entry.get("date_edit", "") time_edit = entry.get("time_edit", "") date_create = entry.get("date_create", "") user_edit = entry.get("user_edit", 0) user_create = entry.get("user_create", 0) user_log = entry.get("user_log", []) file_main = {} files_new = [] for file in files : if "file" in file : file["link"] = file["file"] del file["file"] if not file_main and is_image_file(file["link"]) : file_main = file else : files_new.append(file) relations = [] sections = [] title_new = "" if favorite : child_order = 0 for item in favorite : item = item.replace("•", "").strip(" *•") if not item : continue if not title_new : title_new = item child_order = child_order + 1 sections.append({ "id" : _id + "_fa" + str(child_order), "section_type" : "مطلوب", "child_order" : child_order, "title" : item, }) if unfavorite : child_order = 0 for item in unfavorite : item = item.replace("•", "").strip(" *•") if not item : continue if not title_new : title_new = item child_order = child_order + 1 sections.append({ "id" : _id + "_uf" + str(child_order), "section_type" : "نامطلوب", "child_order" : child_order, "title" : item, }) dict_title = {} if fish : child_order = 0 for item in fish : child_order = child_order + 1 title = "" if "resource" in item and item["resource"]: title += item["resource"] if "date_text" in item and item["date_text"]: title += " " + item["date_text"] title = title.strip() dict_title[title] = 1 desc = "" if "text" in item and item["text"]: desc += item["text"] if "description" in item and item["description"]: desc += "\n" + item["description"] sections.append({ "id" : _id + "_fs" + str(child_order), "section_type" : "منبع", "child_order" : child_order, "title" : title, "description" : desc, }) if description : sections.append({ "id" : _id + "_de", "section_type" : "بیان", "description" : description }) if text : sections.append({ "id" : _id + "_tx", "section_type" : "توضیح", "description" : text }) if summary : sections.append({ "id" : _id + "_sm", "section_type" : "چکیده", "description" : summary }) # اصلاح عنوان خودکار قبلی if title in dict_title and title_new : title = title_new # subject ?? data = { "id" : _id, "main_type" : "معیار", "other_type" : "جهادعلمی", "title" : title, "files" : files_new, "file_main" : file_main, "sections" : sections, "relations" : relations, "links" : links, "user_logs" : user_log, "user_create" : user_create, "user_edit" : user_edit, "time_create" : date_create, "time_edit" : time_edit, } bulk_data.append({"_id": _id, "_source": data}) if len(bulk_data) > 100: total_count += len(bulk_data) print("=" * 10, "to_elastic , total=" + str(total_count)) elastic.importBulkList(bulk_data, index_name_o, True) bulk_data = [] # break 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 = [] load_dotenv(dotenv_path="config.env") host = os.getenv("HOST_60") password = os.getenv("HOST_60_PASS") # host = os.getenv("HOST_133") # password = os.getenv("HOST_133_PASS") # print(host) # print(password) path_base_map = "G:/Project2/tavasi_160/src/repo/_other/" # path_base_map = ( # "G:\Project2\tavasi_60\src\repo\_other/" # ) elastic = ElasticHelper(host, password, "elastic", path_base_map) text_normalize = TextNormalizer() elastic.createIndex( 'gn_teamwork', 'general', 'gn_teamwork' ) import_from_jahat_entities(elastic, "mn_jahat", "gn_teamwork") import_from_crition_entities(elastic, "mn_crition", "gn_teamwork") # .\.env\Scripts\activate # python -m app.monir.monir_teamwork