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.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_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '')
|
||||
ELASTIC_PORT = os.environ.get('ELASTIC_PORT', 9200)
|
||||
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
|
||||
|
||||
|
||||
class SearchService:
|
||||
def __init__(self, index_name='search_index'):
|
||||
self.index_name = index_name
|
||||
self.disabled = False
|
||||
self.manager = Manager()
|
||||
self.client = None
|
||||
|
||||
|
@ -48,7 +51,7 @@ class SearchService:
|
|||
|
||||
except Exception as exc:
|
||||
logger.error(exc)
|
||||
self.disabled = True
|
||||
self.client = None
|
||||
|
||||
self.check_index()
|
||||
else:
|
||||
|
@ -56,12 +59,12 @@ class SearchService:
|
|||
|
||||
def info(self):
|
||||
if self.client:
|
||||
logger.info(f'{self.client}')
|
||||
logger.info(f' {self.client}')
|
||||
else:
|
||||
logger.info(' * Задайте переменные среды для подключения к серверу поиска')
|
||||
|
||||
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)
|
||||
|
||||
def create_index(self):
|
||||
|
@ -101,12 +104,13 @@ class SearchService:
|
|||
try:
|
||||
if self.client:
|
||||
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.open(index=self.index_name)
|
||||
except Exception as error:
|
||||
logger.warn(error)
|
||||
self.disabled = True
|
||||
logging.error(f' Ошибка поиска: {error}')
|
||||
|
||||
def put_mapping(self):
|
||||
mapping = {
|
||||
|
@ -121,8 +125,8 @@ class SearchService:
|
|||
|
||||
def check_index(self):
|
||||
if self.client:
|
||||
if not self.client.indices.exists(index=self.index_name) and not self.disabled:
|
||||
logger.debug(f'Creating {self.index_name} index')
|
||||
if not self.client.indices.exists(index=self.index_name):
|
||||
logger.debug(f' Создаём новый индекс: {self.index_name} ')
|
||||
self.create_index()
|
||||
self.put_mapping()
|
||||
else:
|
||||
|
@ -136,7 +140,9 @@ class SearchService:
|
|||
}
|
||||
}
|
||||
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()
|
||||
|
||||
def recreate_index(self):
|
||||
|
@ -145,9 +151,9 @@ class SearchService:
|
|||
self.check_index()
|
||||
|
||||
def index_post(self, shout):
|
||||
if not not self.disabled and self.client:
|
||||
if self.client:
|
||||
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)
|
||||
|
||||
def search_post(self, query, limit, offset):
|
||||
|
@ -156,7 +162,9 @@ class SearchService:
|
|||
'query': {'match': {'_all': query}},
|
||||
}
|
||||
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']
|
||||
|
||||
return [
|
||||
|
@ -177,11 +185,11 @@ async def search_text(text: str, limit: int = 50, offset: int = 0):
|
|||
try:
|
||||
# Use a key with a prefix to differentiate search results from other Redis data
|
||||
redis_key = f'search:{text}'
|
||||
if not search.disabled:
|
||||
if not search.client:
|
||||
# Use OpenSearchService.search_post method
|
||||
payload = search.search_post(text, limit, offset)
|
||||
# Use Redis as cache with TTL
|
||||
await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(payload))
|
||||
except Exception as e:
|
||||
logging.error(f'Error during search: {e}')
|
||||
logging.error(f' Ошибка поиска: {e}')
|
||||
return payload
|
||||
|
|
Loading…
Reference in New Issue
Block a user