diff --git a/backend/appsettings/src/backup.py b/backend/appsettings/src/backup.py index a69da037..702acd99 100644 --- a/backend/appsettings/src/backup.py +++ b/backend/appsettings/src/backup.py @@ -7,6 +7,7 @@ Functionality: import json import os +import re import zipfile from datetime import datetime @@ -101,8 +102,7 @@ class ElasticBackup: def post_bulk_restore(self, file_name): """send bulk to es""" - file_path = os.path.join(self.CACHE_DIR, file_name) - with open(file_path, "r", encoding="utf-8") as f: + with open(file_name, "r", encoding="utf-8") as f: data = f.read() if not data.strip(): @@ -256,7 +256,7 @@ class BackupCallback: for document in self.source: document_id = document["_id"] - es_index = document["_index"] + es_index = re.sub(r"_v\d+$", "", document["_index"]) # remove _v action = {"index": {"_index": es_index, "_id": document_id}} source = document["_source"] bulk_list.append(json.dumps(action)) diff --git a/backend/appsettings/src/index_setup.py b/backend/appsettings/src/index_setup.py index a8ad5e02..e39ab9e2 100644 --- a/backend/appsettings/src/index_setup.py +++ b/backend/appsettings/src/index_setup.py @@ -55,6 +55,8 @@ class ElasticIndex: """check if index already exists and return mapping if it does""" response, status_code = ElasticWrap(self.index_namespace).get() exists = status_code == 200 + if not exists: + return False, False index_key = f"{self.index_namespace}" current_version = self.get_current_version() @@ -357,6 +359,9 @@ class ElasticIndexWrap: index_name, _, _ = self._config_split(index) print(f"[ta_{index_name}] reset elastic index") handler = ElasticIndex(index_name) + if not handler.exists: + continue + current_version = handler.get_current_version() handler.delete_index(by_version=current_version)