search dummy service

This commit is contained in:
tonyrewin 2022-10-04 03:32:29 +03:00
parent e16265a0c3
commit d678729ffc
3 changed files with 30 additions and 18 deletions

View File

@ -14,6 +14,7 @@ from resolvers.topics import topic_follow, topic_unfollow
from services.stat.viewed import ViewedStorage from services.stat.viewed import ViewedStorage
from services.zine.shoutauthor import ShoutAuthorStorage from services.zine.shoutauthor import ShoutAuthorStorage
from services.zine.shoutscache import ShoutsCache from services.zine.shoutscache import ShoutsCache
from services.zine import SearchService
@mutation.field("incrementView") @mutation.field("incrementView")
@ -98,24 +99,14 @@ async def get_shout_by_slug(_, info, slug):
@query.field("searchQuery") @query.field("searchQuery")
async def get_search_results(_, _info, searchtext, offset, limit): async def get_search_results(_, _info, searchtext, offset, limit):
# TODO: remove the copy of searchByTopics shouts = SearchService.search(searchtext)
# with search ranking query # TODO: sort and filter types for search service
with local_session() as session: for s in shouts:
shouts = ( shout = s.dict()
session.query(Shout) for a in shout['authors']:
.join(ShoutTopic) a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
.where(and_(ShoutTopic.topic.in_(searchtext), Shout.publishedAt.is_not(None))) s.stat.relevance = 1 # FIXME: expecting search engine rated relevance
.order_by(desc(Shout.publishedAt)) return shouts[offset : offset + limit]
.limit(limit)
.offset(offset)
)
for s in shouts:
shout = s.dict()
for a in shout['authors']:
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
s.stat.relevance = 1 # FIXME: expecting search engine rated relevance
return shouts
@query.field("shoutsByAuthors") @query.field("shoutsByAuthors")

View File

@ -3,6 +3,7 @@ from services.stat.reacted import ReactedStorage
from services.auth.roles import RoleStorage from services.auth.roles import RoleStorage
from services.auth.users import UserStorage from services.auth.users import UserStorage
from services.zine.topics import TopicStorage from services.zine.topics import TopicStorage
from services.search import SearchService
from base.orm import local_session from base.orm import local_session
@ -14,4 +15,5 @@ async def storages_init():
RoleStorage.init(session) RoleStorage.init(session)
UserStorage.init(session) UserStorage.init(session)
TopicStorage.init(session) TopicStorage.init(session)
SearchService.init(session)
session.commit() session.commit()

19
services/search.py Normal file
View File

@ -0,0 +1,19 @@
import asyncio
from orm.shout import Shout
class SearchService:
lock = asyncio.Lock()
cache = {}
@staticmethod
def init(session):
self = SearchService
async with self.lock:
self.cache = {}
@staticmethod
async def search(text) -> [Shout]:
self = SearchService
async with self.lock:
return [] # TODO: implement getting shouts list