From 4a6863c474513ebc07570e4d9ffb6596c57b2b66 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 29 Jan 2024 05:13:37 +0300 Subject: [PATCH] check-if-exists --- README.md | 2 +- services/search.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7afedac1..01038ad3 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ poetry run main.py ### search.py -Позволяет получать результаты пользовательских поисковых запросов в кешируемом виде от ElasticSearch с оценкой `score`, объединенные с запросами к базе данных, запрашиваем через GraphQL API `load_shouts_search`. Требует установка `ELASTIC_URL` (можно отдельными компонентами) и, опционально, обновляет индекс на старте если переменная `ELASTIC_REINDEX` задана. +Позволяет получать результаты пользовательских поисковых запросов в кешируемом виде от ElasticSearch с оценкой `score`, объединенные с запросами к базе данных, запрашиваем через GraphQL API `load_shouts_search`. Требует установка `ELASTIC_URL` (можно отдельными компонентами). ### notify.py diff --git a/services/search.py b/services/search.py index 945571c0..ce2981da 100644 --- a/services/search.py +++ b/services/search.py @@ -40,10 +40,8 @@ class SearchService: except Exception as exc: logger.error(exc) self.disabled = True - self.check_index() - if ELASTIC_REINDEX: - self.recreate_index() + self.check_index() def info(self): logging.info(f'{self.client}') @@ -108,6 +106,19 @@ class SearchService: logger.debug(f'Creating {self.index_name} index') self.create_index() self.put_mapping() + else: + # Check if the mapping is correct, and recreate the index if needed + mapping = self.client.indices.get_mapping(index=self.index_name) + expected_mapping = { + 'properties': { + 'body': {'type': 'text', 'analyzer': 'ru'}, + 'text': {'type': 'text'}, + 'author': {'type': 'text'}, + } + } + if mapping != expected_mapping: + logger.debug(f'Recreating {self.index_name} index due to incorrect mapping') + self.recreate_index() def recreate_index(self): self.delete_index()