search-result-schema-fix-5
All checks were successful
deploy / deploy (push) Successful in 1m29s

This commit is contained in:
Untone 2023-12-19 15:42:46 +03:00
parent 56cdd4e0f9
commit b502c581f7

View File

@ -19,10 +19,11 @@ class SearchService:
@staticmethod @staticmethod
async def search(text: str, limit: int = 50, offset: int = 0) -> List[Shout]: async def search(text: str, limit: int = 50, offset: int = 0) -> List[Shout]:
payload = []
try: try:
payload = await redis.execute("GET", text) cached = await redis.execute("GET", text)
if not payload: if not cached:
async with SearchService.lock: async with SearchService.lock:
# Use aiohttp to send a request to ElasticSearch # Use aiohttp to send a request to ElasticSearch
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
@ -33,8 +34,9 @@ class SearchService:
await redis.execute("SET", text, json.dumps(payload)) # use redis as cache await redis.execute("SET", text, json.dumps(payload)) # use redis as cache
else: else:
logging.error(f"[services.search] response: {response.status} {await response.text()}") logging.error(f"[services.search] response: {response.status} {await response.text()}")
else:
payload = json.loads(cached)
except Exception as e: except Exception as e:
logging.error(f"[services.search] Error during search: {e}") logging.error(f"[services.search] Error during search: {e}")
payload = []
return payload[offset : offset + limit] return payload[offset : offset + limit]