This commit is contained in:
parent
2c2932caeb
commit
8ff1949170
|
@ -45,7 +45,7 @@ poetry run main.py
|
||||||
|
|
||||||
### search.py
|
### search.py
|
||||||
|
|
||||||
Позволяет получать результаты пользовательских поисковых запросов в кешируемом виде от [нашего сервера](https://search.discours.io) ElasticSearch с оценкой `score`, объединенные с запросами к базе данных, запрашиваем через GraphQL API `load_shouts_search`.
|
Позволяет получать результаты пользовательских поисковых запросов в кешируемом виде от ElasticSearch с оценкой `score`, объединенные с запросами к базе данных, запрашиваем через GraphQL API `load_shouts_search`. Требует установка `ELASTIC_URL` (можно отдельными компонентами) и, опционально, обновляет индекс на старте если переменная `ELASTIC_REINDEX` задана.
|
||||||
|
|
||||||
### notify.py
|
### notify.py
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,8 @@ from typing import List
|
||||||
|
|
||||||
from elasticsearch import Elasticsearch
|
from elasticsearch import Elasticsearch
|
||||||
|
|
||||||
from orm.shout import Shout # Adjust the import as needed
|
from orm.shout import Shout
|
||||||
from services.rediscache import redis # Adjust the import as needed
|
from services.rediscache import redis
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('[services.search] ')
|
logger = logging.getLogger('[services.search] ')
|
||||||
|
@ -18,10 +18,15 @@ 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 = f'https://{ELASTIC_AUTH}@{ELASTIC_HOST}:{ELASTIC_PORT}'
|
ELASTIC_URL = os.environ.get('ELASTIC_URL', f'https://{ELASTIC_AUTH}@{ELASTIC_HOST}:{ELASTIC_PORT}')
|
||||||
|
ELASTIC_REINDEX = os.environ.get('ELASTIC_REINDEX', '')
|
||||||
|
REDIS_TTL = 86400 # 1 day in seconds
|
||||||
|
|
||||||
|
|
||||||
class OpenSearchService:
|
class SearchService:
|
||||||
|
lock = asyncio.Lock()
|
||||||
|
elastic = None
|
||||||
|
|
||||||
def __init__(self, index_name, delete_index_on_startup):
|
def __init__(self, index_name, delete_index_on_startup):
|
||||||
self.index_name = index_name
|
self.index_name = index_name
|
||||||
self.delete_index_on_startup = delete_index_on_startup
|
self.delete_index_on_startup = delete_index_on_startup
|
||||||
|
@ -32,6 +37,9 @@ class OpenSearchService:
|
||||||
|
|
||||||
self.check_index()
|
self.check_index()
|
||||||
|
|
||||||
|
if ELASTIC_REINDEX:
|
||||||
|
self.recreate_index()
|
||||||
|
|
||||||
def delete_index(self):
|
def delete_index(self):
|
||||||
self.elasticsearch_client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
self.elasticsearch_client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
||||||
|
|
||||||
|
@ -63,10 +71,7 @@ class OpenSearchService:
|
||||||
},
|
},
|
||||||
'mappings': {
|
'mappings': {
|
||||||
'properties': {
|
'properties': {
|
||||||
'body': {
|
'body': {'type': 'text', 'analyzer': 'ru'},
|
||||||
'type': 'text',
|
|
||||||
'analyzer': 'ru',
|
|
||||||
},
|
|
||||||
'text': {'type': 'text'},
|
'text': {'type': 'text'},
|
||||||
'author': {'type': 'text'},
|
'author': {'type': 'text'},
|
||||||
}
|
}
|
||||||
|
@ -80,10 +85,7 @@ class OpenSearchService:
|
||||||
def put_mapping(self):
|
def put_mapping(self):
|
||||||
mapping = {
|
mapping = {
|
||||||
'properties': {
|
'properties': {
|
||||||
'body': {
|
'body': {'type': 'text', 'analyzer': 'ru'},
|
||||||
'type': 'text',
|
|
||||||
'analyzer': 'ru',
|
|
||||||
},
|
|
||||||
'text': {'type': 'text'},
|
'text': {'type': 'text'},
|
||||||
'author': {'type': 'text'},
|
'author': {'type': 'text'},
|
||||||
}
|
}
|
||||||
|
@ -97,20 +99,19 @@ class OpenSearchService:
|
||||||
self.create_index()
|
self.create_index()
|
||||||
self.put_mapping()
|
self.put_mapping()
|
||||||
|
|
||||||
|
def recreate_index(self):
|
||||||
|
self.delete_index()
|
||||||
|
self.check_index()
|
||||||
|
|
||||||
def index_post(self, shout):
|
def index_post(self, shout):
|
||||||
id_ = str(shout.id)
|
id_ = str(shout.id)
|
||||||
logger.debug(f'Indexing post id {id_}')
|
logger.debug(f'Indexing post id {id_}')
|
||||||
|
|
||||||
self.elasticsearch_client.index(index=self.index_name, id=id_, body=shout)
|
self.elasticsearch_client.index(index=self.index_name, id=id_, body=shout)
|
||||||
|
|
||||||
def search_post(self, query, limit, offset):
|
def search_post(self, query, limit, offset):
|
||||||
logger.debug(f'Search query = {query}, limit = {limit}')
|
logger.debug(f'query: {query}')
|
||||||
search_body = {
|
search_body = {
|
||||||
'query': {
|
'query': {'match': {'_all': query}},
|
||||||
'match': {
|
|
||||||
'_all': query,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
search_response = self.elasticsearch_client.search(
|
search_response = self.elasticsearch_client.search(
|
||||||
|
@ -126,18 +127,13 @@ class OpenSearchService:
|
||||||
for hit in hits
|
for hit in hits
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class SearchService:
|
|
||||||
lock = asyncio.Lock()
|
|
||||||
elastic = None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def init():
|
async def init():
|
||||||
self = SearchService
|
self = SearchService
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
logging.info('Initializing SearchService')
|
logging.info('Initializing SearchService')
|
||||||
try:
|
try:
|
||||||
self.elastic = OpenSearchService('shouts_index', False)
|
self.elastic = SearchService('shouts_index', False)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.error(exc)
|
logger.error(exc)
|
||||||
|
|
||||||
|
@ -146,16 +142,12 @@ class SearchService:
|
||||||
payload = []
|
payload = []
|
||||||
self = SearchService
|
self = SearchService
|
||||||
try:
|
try:
|
||||||
# TODO: add ttl for redis cached search results
|
# Use a key with a prefix to differentiate search results from other Redis data
|
||||||
cached = await redis.execute('GET', text)
|
redis_key = f'search:{text}'
|
||||||
if not cached:
|
# Use OpenSearchService.search_post method
|
||||||
async with self.lock:
|
payload = await self.elastic.search_post(text, limit, offset)
|
||||||
# Use OpenSearchService.search_post method
|
# Use Redis as cache with TTL
|
||||||
payload = await self.elastic.search_post(text, limit, offset)
|
await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(payload))
|
||||||
# Use Redis as cache
|
|
||||||
await redis.execute('SET', text, json.dumps(payload))
|
|
||||||
elif isinstance(cached, str):
|
|
||||||
payload = json.loads(cached)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f'Error during search: {e}')
|
logging.error(f'Error during search: {e}')
|
||||||
return payload
|
return payload
|
||||||
|
|
|
@ -61,6 +61,12 @@ class ViewedStorage:
|
||||||
if os.path.exists(VIEWS_FILEPATH):
|
if os.path.exists(VIEWS_FILEPATH):
|
||||||
file_timestamp = os.path.getctime(VIEWS_FILEPATH)
|
file_timestamp = os.path.getctime(VIEWS_FILEPATH)
|
||||||
self.start_date = datetime.fromtimestamp(file_timestamp).strftime('%Y-%m-%d')
|
self.start_date = datetime.fromtimestamp(file_timestamp).strftime('%Y-%m-%d')
|
||||||
|
now_date = datetime.now().strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
if now_date == self.start_date:
|
||||||
|
logger.info(' * Данные актуализованы!')
|
||||||
|
else:
|
||||||
|
logger.info(f' * Миграция проводилась: {self.start_date}')
|
||||||
|
|
||||||
# Запуск фоновой задачи
|
# Запуск фоновой задачи
|
||||||
asyncio.create_task(self.worker())
|
asyncio.create_task(self.worker())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user