This commit is contained in:
parent
2663d1cbc5
commit
e1a27b55cd
4
main.py
4
main.py
|
@ -16,7 +16,7 @@ from starlette.routing import Route
|
||||||
from resolvers.webhook import WebhookEndpoint
|
from resolvers.webhook import WebhookEndpoint
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
from services.schema import resolvers
|
from services.schema import resolvers
|
||||||
from services.search import SearchService
|
from services.search import search
|
||||||
from services.viewed import ViewedStorage
|
from services.viewed import ViewedStorage
|
||||||
from settings import DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
|
from settings import DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ async def start_up():
|
||||||
await ViewedStorage.init()
|
await ViewedStorage.init()
|
||||||
|
|
||||||
# start search service
|
# start search service
|
||||||
await SearchService.init()
|
search.info()
|
||||||
|
|
||||||
if MODE == 'development':
|
if MODE == 'development':
|
||||||
# pid file management
|
# pid file management
|
||||||
|
|
|
@ -11,7 +11,7 @@ from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.notify import notify_shout
|
from services.notify import notify_shout
|
||||||
from services.schema import mutation, query
|
from services.schema import mutation, query
|
||||||
from services.search import SearchService
|
from services.search import search
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_shouts_drafts')
|
@query.field('get_shouts_drafts')
|
||||||
|
@ -82,9 +82,6 @@ async def create_shout(_, info, inp):
|
||||||
|
|
||||||
# notifier
|
# notifier
|
||||||
await notify_shout(shout_dict, 'create')
|
await notify_shout(shout_dict, 'create')
|
||||||
|
|
||||||
# search service indexing
|
|
||||||
SearchService.elastic.index_post(shout)
|
|
||||||
return {'shout': shout_dict}
|
return {'shout': shout_dict}
|
||||||
|
|
||||||
|
|
||||||
|
@ -190,9 +187,9 @@ async def update_shout( # noqa: C901
|
||||||
|
|
||||||
if not publish:
|
if not publish:
|
||||||
await notify_shout(shout_dict, 'update')
|
await notify_shout(shout_dict, 'update')
|
||||||
|
if shout.visibility is ShoutVisibility.COMMUNITY.value or shout.visibility is ShoutVisibility.PUBLIC.value:
|
||||||
# search service indexing
|
# search service indexing
|
||||||
SearchService.elastic.index_post(shout)
|
search.index_post(shout)
|
||||||
|
|
||||||
return {'shout': shout_dict}
|
return {'shout': shout_dict}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ from resolvers.topic import get_random_topic
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.schema import query
|
from services.schema import query
|
||||||
from services.search import SearchService
|
from services.search import search_text
|
||||||
from services.viewed import ViewedStorage
|
from services.viewed import ViewedStorage
|
||||||
|
|
||||||
|
|
||||||
|
@ -312,7 +312,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 text and len(text) > 2:
|
||||||
results = await SearchService.search(text, limit, offset)
|
results = await search_text(text, limit, offset)
|
||||||
results_dict = {r['slug']: r for r in results}
|
results_dict = {r['slug']: r for r in results}
|
||||||
found_keys = list(results_dict.keys())
|
found_keys = list(results_dict.keys())
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
import asyncio
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from typing import List
|
|
||||||
|
|
||||||
from elasticsearch import Elasticsearch
|
from elasticsearch import Elasticsearch
|
||||||
|
|
||||||
from orm.shout import Shout
|
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,22 +21,18 @@ REDIS_TTL = 86400 # 1 day in seconds
|
||||||
|
|
||||||
|
|
||||||
class SearchService:
|
class SearchService:
|
||||||
lock = asyncio.Lock()
|
async def __init__(self, index_name='search_index'):
|
||||||
elastic = None
|
logging.info('Initializing SearchService')
|
||||||
|
|
||||||
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.elasticsearch_client = Elasticsearch(f'{ELASTIC_URL}', verify_certs=False)
|
self.elasticsearch_client = Elasticsearch(f'{ELASTIC_URL}', verify_certs=False)
|
||||||
|
|
||||||
if self.delete_index_on_startup:
|
|
||||||
self.delete_index()
|
|
||||||
|
|
||||||
self.check_index()
|
self.check_index()
|
||||||
|
|
||||||
if ELASTIC_REINDEX:
|
if ELASTIC_REINDEX:
|
||||||
self.recreate_index()
|
self.recreate_index()
|
||||||
|
|
||||||
|
def info(self):
|
||||||
|
logging.info(f'Initializing SearchService {self.elasticsearch_client}')
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
@ -127,25 +120,17 @@ class SearchService:
|
||||||
for hit in hits
|
for hit in hits
|
||||||
]
|
]
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def init():
|
|
||||||
self = SearchService
|
|
||||||
async with self.lock:
|
|
||||||
logging.info('Initializing SearchService')
|
|
||||||
try:
|
|
||||||
self.elastic = SearchService('shouts_index', False)
|
|
||||||
except Exception as exc:
|
|
||||||
logger.error(exc)
|
|
||||||
|
|
||||||
@staticmethod
|
search = SearchService()
|
||||||
async def search(text: str, limit: int = 50, offset: int = 0) -> List[Shout]:
|
|
||||||
|
|
||||||
|
async def search_text(text: str, limit: int = 50, offset: int = 0):
|
||||||
payload = []
|
payload = []
|
||||||
self = SearchService
|
|
||||||
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}'
|
||||||
# Use OpenSearchService.search_post method
|
# Use OpenSearchService.search_post method
|
||||||
payload = await self.elastic.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:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user