diff --git a/all_elastic_steps.py b/all_elastic_steps.py index 4d6c86e..7b663a1 100644 --- a/all_elastic_steps.py +++ b/all_elastic_steps.py @@ -85,7 +85,7 @@ path_back = 'G:/Project2/111' #elastic.backupIndexToZipfile(path_back, "mj_qa_qanon") # elastic.backupIndexToZipfile(path_back, "mj_qa_section") -elastic.backupIndexToZipfile(path_back, "mj_qa_section", '', {}, True, [], ["pos_tags", "chunk_tags", "version_info"]) +#elastic.backupIndexToZipfile(path_back, "mj_qa_section", '', {}, True, [], ["pos_tags", "chunk_tags", "version_info"]) #elastic.backupIndexToZipfile(path_back, "mj_qa_section_ai") #elastic.backupIndexToZipfile(path_back, "mj_rg_section") #elastic.backupIndexToZipfile(path_back, "mj_qa_opinion") diff --git a/elastic_helper.py b/elastic_helper.py index 2b53851..4318ff5 100644 --- a/elastic_helper.py +++ b/elastic_helper.py @@ -420,6 +420,43 @@ class ElasticHelper(): print(self.es.cat.count(index=index_name, format="json")) print(" END Of Import to elastic ", index_name ,"\n") + + def iterateJsonFile(self, file_path, isZip=True, limit_pack = -1): + if not os.path.exists(file_path): + print("file zip:" , file_path , " not exist iterateJsonFile " ) + return + + if isZip : + fileNo = 0 + with zipfile.ZipFile(file_path, 'r') as zObject: + fileNo +=1 + print("="*10, " zip fileNo: " , fileNo ," iterateJsonFile - | File Numbers:" ,len(zObject.namelist()) , "=" * 10) + + packNo = 0 + self.counter = 0 + for filename in zObject.namelist(): + packNo += 1 + if limit_pack != -1 : + if packNo > limit_pack : + print('limit_data iterateJsonFile ', limit_pack) + break + + print("index iterateJsonFile :", '=>' , filename ) + with zObject.open(filename) as file: + data = json.loads(file.read()) + # Yield each entry + # yield data + yield from ({"source": hit["_source"], "id": hit["_id"]} for hit in data) + else : + with open(filename, 'r', encoding='utf-8') as file: + data = json.loads(file.read()) + # Yield each entry + # yield from (hit for hit in data) + #return data + yield from ({"source": hit["_source"], "id": hit["_id"]} for hit in data) + + + def es_iterate_all_documents(self, index, body="", pagesize=250, scroll_timeout="25m", **kwargs): diff --git a/temp_codes.py b/temp_codes.py index f884db3..2edc3d8 100644 --- a/temp_codes.py +++ b/temp_codes.py @@ -5,16 +5,17 @@ from text_normalizer import TextNormalizer from general_functions import * from elastic_helper import ElasticHelper from create_relation_tags import * +from temp_refactor_gn_dataset import * load_dotenv(dotenv_path='config.env') -host=os.getenv('HOST_160') -password=os.getenv('HOST_160_PASS') +# host=os.getenv('HOST_160') +# password=os.getenv('HOST_160_PASS') -# host=os.getenv('HOST_112') -# password=os.getenv('HOST_112_PASS') +host=os.getenv('HOST_112') +password=os.getenv('HOST_112_PASS') # host=os.getenv('HOST_60') # password=os.getenv('HOST_60_PASS') @@ -31,6 +32,17 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) #------------------------------------------------------- ## +#elastic.backupIndexToZipfile("G:/Project2/111", "gn_dataset") + +moveDataset1(elastic, "G:/Project2/111/gn_dataset_14031125.zip", "gn_dataset") + + +## کد نمونه بازیابی از فایل +#path_back = 'G:/Project2/111' +#elastic.restorFileToElastic(path_back, 'gn_dataset_14031125.zip', 'majles', True, 'mj_qa_section') + + + # elastic.backupIndexToZipfile("G:/Project2/111", "mj_qa_section") #elastic.backupIndexToZipfile("G:/Project2/111", "mj_qa_section_ai") #elastic.restorFileToElastic("G:/Project2/111", "mj_qa_section_ai", 'majles', True, "mj_qa_section") @@ -47,8 +59,8 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) #------------------------------------------------------- ## -ai_url_chunk = os.getenv("ai_url_chunk") -fill_chunk_tags(elastic, ai_url_chunk, "mj_qa_section") +#ai_url_chunk = os.getenv("ai_url_chunk") +#fill_chunk_tags(elastic, ai_url_chunk, "mj_qa_section") #------------------------------------------------------- @@ -63,7 +75,6 @@ fill_chunk_tags(elastic, ai_url_chunk, "mj_qa_section") # print(response.text) # print(response) - # payload={ # "text":"طرفین به منظور اتمام مطالعات و احداث نیروگاه‌های جریانی ارس و تأسیسات مربوطه، منطقه مشترک ساختمانی و مطالعاتی را تعیین و امنیت این منطقه را در طول مدت اجراء و بهره‌برداری تأمین می‌نمایند. " # } diff --git a/temp_refactor_gn_dataset.py b/temp_refactor_gn_dataset.py new file mode 100644 index 0000000..0858180 --- /dev/null +++ b/temp_refactor_gn_dataset.py @@ -0,0 +1,46 @@ +import os +from text_normalizer import TextNormalizer +# from general import * +from general_functions import * +from elastic_helper import ElasticHelper + + + +def moveDataset1(elastic, file, index_name_o): + try: + try: + list = elastic.iterateJsonFile(file) + except Exception as e: + save_error(0, e) + + count = 0 + text_normal = TextNormalizer() + + for mentry in list: + count += 1 + #print(mentry) + + entry = mentry["source"] + id = entry.get("id", "") + domain = entry.get("domain", "") + + if domain == 'تغییرات قانون': + continue + + #"value":"استخراج کلیدواژه 11 هزارتایی" + + print(id) + #print(entry["content"]) + + # data = {} + # # data["chunk_keywords"]= chunk_keywords + # update = False + # eid = id + # try: + # resp = elastic.update_index_doc(update, index_name_o, eid, data) + + # except Exception as e: + # save_error(id, e) + + except Exception as e: + save_error(id, e) \ No newline at end of file