This commit is contained in:
parent
8a88a98b53
commit
56bf5b2874
|
@ -11,19 +11,22 @@ from services.rediscache import redis
|
||||||
logger = logging.getLogger('\t[services.search]\t')
|
logger = logging.getLogger('\t[services.search]\t')
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
ELASTIC_HOST = os.environ.get('ELASTIC_HOST', '').replace('https://', '').replace('http://', '')
|
ELASTIC_HOST = (
|
||||||
|
os.environ.get('ELASTIC_HOST', '').replace('https://', '').replace('http://', '')
|
||||||
|
)
|
||||||
ELASTIC_USER = os.environ.get('ELASTIC_USER', '')
|
ELASTIC_USER = os.environ.get('ELASTIC_USER', '')
|
||||||
ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '')
|
ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '')
|
||||||
ELASTIC_PORT = os.environ.get('ELASTIC_PORT', 9200)
|
ELASTIC_PORT = os.environ.get('ELASTIC_PORT', 9200)
|
||||||
ELASTIC_AUTH = f'{ELASTIC_USER}:{ELASTIC_PASSWORD}' if ELASTIC_USER else ''
|
ELASTIC_AUTH = f'{ELASTIC_USER}:{ELASTIC_PASSWORD}' if ELASTIC_USER else ''
|
||||||
ELASTIC_URL = os.environ.get('ELASTIC_URL', f'https://{ELASTIC_AUTH}@{ELASTIC_HOST}:{ELASTIC_PORT}')
|
ELASTIC_URL = os.environ.get(
|
||||||
|
'ELASTIC_URL', f'https://{ELASTIC_AUTH}@{ELASTIC_HOST}:{ELASTIC_PORT}'
|
||||||
|
)
|
||||||
REDIS_TTL = 86400 # 1 day in seconds
|
REDIS_TTL = 86400 # 1 day in seconds
|
||||||
|
|
||||||
|
|
||||||
class SearchService:
|
class SearchService:
|
||||||
def __init__(self, index_name='search_index'):
|
def __init__(self, index_name='search_index'):
|
||||||
self.index_name = index_name
|
self.index_name = index_name
|
||||||
self.disabled = False
|
|
||||||
self.manager = Manager()
|
self.manager = Manager()
|
||||||
self.client = None
|
self.client = None
|
||||||
|
|
||||||
|
@ -48,7 +51,7 @@ class SearchService:
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.error(exc)
|
logger.error(exc)
|
||||||
self.disabled = True
|
self.client = None
|
||||||
|
|
||||||
self.check_index()
|
self.check_index()
|
||||||
else:
|
else:
|
||||||
|
@ -61,7 +64,7 @@ class SearchService:
|
||||||
logger.info(' * Задайте переменные среды для подключения к серверу поиска')
|
logger.info(' * Задайте переменные среды для подключения к серверу поиска')
|
||||||
|
|
||||||
def delete_index(self):
|
def delete_index(self):
|
||||||
if not self.disabled and self.client:
|
if self.client:
|
||||||
self.client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
self.client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
||||||
|
|
||||||
def create_index(self):
|
def create_index(self):
|
||||||
|
@ -101,12 +104,13 @@ class SearchService:
|
||||||
try:
|
try:
|
||||||
if self.client:
|
if self.client:
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self.client.indices.create(index=self.index_name, body=index_settings)
|
self.client.indices.create(
|
||||||
|
index=self.index_name, body=index_settings
|
||||||
|
)
|
||||||
self.client.indices.close(index=self.index_name)
|
self.client.indices.close(index=self.index_name)
|
||||||
self.client.indices.open(index=self.index_name)
|
self.client.indices.open(index=self.index_name)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
logger.warn(error)
|
logging.error(f' Ошибка поиска: {error}')
|
||||||
self.disabled = True
|
|
||||||
|
|
||||||
def put_mapping(self):
|
def put_mapping(self):
|
||||||
mapping = {
|
mapping = {
|
||||||
|
@ -121,8 +125,8 @@ class SearchService:
|
||||||
|
|
||||||
def check_index(self):
|
def check_index(self):
|
||||||
if self.client:
|
if self.client:
|
||||||
if not self.client.indices.exists(index=self.index_name) and not self.disabled:
|
if not self.client.indices.exists(index=self.index_name):
|
||||||
logger.debug(f'Creating {self.index_name} index')
|
logger.debug(f' Создаём новый индекс: {self.index_name} ')
|
||||||
self.create_index()
|
self.create_index()
|
||||||
self.put_mapping()
|
self.put_mapping()
|
||||||
else:
|
else:
|
||||||
|
@ -136,7 +140,9 @@ class SearchService:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if mapping != expected_mapping:
|
if mapping != expected_mapping:
|
||||||
logger.debug(f'Recreating {self.index_name} index due to incorrect mapping')
|
logger.debug(
|
||||||
|
f' Пересоздаём индекс {self.index_name} из-за неправильной структуры данных'
|
||||||
|
)
|
||||||
self.recreate_index()
|
self.recreate_index()
|
||||||
|
|
||||||
def recreate_index(self):
|
def recreate_index(self):
|
||||||
|
@ -145,9 +151,9 @@ class SearchService:
|
||||||
self.check_index()
|
self.check_index()
|
||||||
|
|
||||||
def index_post(self, shout):
|
def index_post(self, shout):
|
||||||
if not not self.disabled and self.client:
|
if self.client:
|
||||||
id_ = str(shout.id)
|
id_ = str(shout.id)
|
||||||
logger.debug(f'Indexing post id {id_}')
|
logger.debug(f' Индексируем пост {id_}')
|
||||||
self.client.index(index=self.index_name, id=id_, body=shout)
|
self.client.index(index=self.index_name, id=id_, body=shout)
|
||||||
|
|
||||||
def search_post(self, query, limit, offset):
|
def search_post(self, query, limit, offset):
|
||||||
|
@ -156,7 +162,9 @@ class SearchService:
|
||||||
'query': {'match': {'_all': query}},
|
'query': {'match': {'_all': query}},
|
||||||
}
|
}
|
||||||
if self.client:
|
if self.client:
|
||||||
search_response = self.client.search(index=self.index_name, body=search_body, size=limit, from_=offset)
|
search_response = self.client.search(
|
||||||
|
index=self.index_name, body=search_body, size=limit, from_=offset
|
||||||
|
)
|
||||||
hits = search_response['hits']['hits']
|
hits = search_response['hits']['hits']
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -177,11 +185,11 @@ async def search_text(text: str, limit: int = 50, offset: int = 0):
|
||||||
try:
|
try:
|
||||||
# Use a key with a prefix to differentiate search results from other Redis data
|
# Use a key with a prefix to differentiate search results from other Redis data
|
||||||
redis_key = f'search:{text}'
|
redis_key = f'search:{text}'
|
||||||
if not search.disabled:
|
if not search.client:
|
||||||
# Use OpenSearchService.search_post method
|
# Use OpenSearchService.search_post method
|
||||||
payload = search.search_post(text, limit, offset)
|
payload = search.search_post(text, limit, offset)
|
||||||
# Use Redis as cache with TTL
|
# Use Redis as cache with TTL
|
||||||
await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(payload))
|
await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(payload))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f'Error during search: {e}')
|
logging.error(f' Ошибка поиска: {e}')
|
||||||
return payload
|
return payload
|
||||||
|
|
Loading…
Reference in New Issue
Block a user