This commit is contained in:
parent
2f2fa346ed
commit
ae9e025959
|
@ -311,7 +311,7 @@ async def load_shouts_feed(_, info, options):
|
||||||
|
|
||||||
@query.field('load_shouts_search')
|
@query.field('load_shouts_search')
|
||||||
async def load_shouts_search(_, _info, text, limit=50, offset=0):
|
async def load_shouts_search(_, _info, text, limit=50, offset=0):
|
||||||
if text and len(text) > 2:
|
if isinstance(text, str) and len(text) > 2:
|
||||||
return await search_text(text, limit, offset)
|
return await search_text(text, limit, offset)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ from opensearchpy import OpenSearch
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
|
|
||||||
|
|
||||||
|
os_logger = logging.getLogger(name='opensearch')
|
||||||
|
os_logger.setLevel(logging.INFO)
|
||||||
logger = logging.getLogger('\t[services.search]\t')
|
logger = logging.getLogger('\t[services.search]\t')
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
@ -46,7 +48,7 @@ class SearchService:
|
||||||
logger.info(' Клиент OpenSearch.org подключен')
|
logger.info(' Клиент OpenSearch.org подключен')
|
||||||
self.check_index()
|
self.check_index()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.error(exc)
|
logger.error(f' {exc}')
|
||||||
self.client = None
|
self.client = None
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
|
@ -104,7 +106,7 @@ class SearchService:
|
||||||
finally:
|
finally:
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
else:
|
else:
|
||||||
logger.debug('..')
|
logger.debug(' ..')
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
logging.warning(f' {error}')
|
logging.warning(f' {error}')
|
||||||
|
|
||||||
|
@ -154,22 +156,26 @@ class SearchService:
|
||||||
logger.debug(f' Индексируем пост {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(self, query, limit, offset):
|
async def search(self, text, limit, offset):
|
||||||
logger.debug(f' Ищем: {query}')
|
logger.debug(f' Ищем: {text}')
|
||||||
search_body = {
|
search_body = {
|
||||||
'query': {'match': {'_all': query}},
|
'query': {'match': {'_all': text}},
|
||||||
}
|
}
|
||||||
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 [
|
results = [
|
||||||
{
|
{
|
||||||
**hit['_source'],
|
**hit['_source'],
|
||||||
'score': hit['_score'],
|
'score': hit['_score'],
|
||||||
}
|
}
|
||||||
for hit in hits
|
for hit in hits
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Use Redis as cache with TTL
|
||||||
|
redis_key = f'search:{text}'
|
||||||
|
await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(results))
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
@ -178,14 +184,7 @@ search_service = SearchService()
|
||||||
|
|
||||||
async def search_text(text: str, limit: int = 50, offset: int = 0):
|
async def search_text(text: str, limit: int = 50, offset: int = 0):
|
||||||
payload = []
|
payload = []
|
||||||
try:
|
if search_service.client:
|
||||||
# Use a key with a prefix to differentiate search results from other Redis data
|
|
||||||
redis_key = f'search:{text}'
|
|
||||||
if not search_service.client:
|
|
||||||
# Use OpenSearchService.search_post method
|
# Use OpenSearchService.search_post method
|
||||||
payload = search_service.search(text, limit, offset)
|
payload = search_service.search(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' Ошибка поиска: {e}')
|
|
||||||
return payload
|
return payload
|
||||||
|
|
Loading…
Reference in New Issue
Block a user