diff --git a/resolvers/zine.py b/resolvers/zine.py index 9ac1ac8a..80208939 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -14,6 +14,7 @@ from resolvers.topics import topic_follow, topic_unfollow from services.stat.viewed import ViewedStorage from services.zine.shoutauthor import ShoutAuthorStorage from services.zine.shoutscache import ShoutsCache +from services.zine import SearchService @mutation.field("incrementView") @@ -98,24 +99,14 @@ async def get_shout_by_slug(_, info, slug): @query.field("searchQuery") async def get_search_results(_, _info, searchtext, offset, limit): - # TODO: remove the copy of searchByTopics - # with search ranking query - with local_session() as session: - shouts = ( - session.query(Shout) - .join(ShoutTopic) - .where(and_(ShoutTopic.topic.in_(searchtext), Shout.publishedAt.is_not(None))) - .order_by(desc(Shout.publishedAt)) - .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 + shouts = SearchService.search(searchtext) + # TODO: sort and filter types for search service + 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[offset : offset + limit] @query.field("shoutsByAuthors") diff --git a/services/main.py b/services/main.py index 4f4c824f..b6a2bf37 100644 --- a/services/main.py +++ b/services/main.py @@ -3,6 +3,7 @@ from services.stat.reacted import ReactedStorage from services.auth.roles import RoleStorage from services.auth.users import UserStorage from services.zine.topics import TopicStorage +from services.search import SearchService from base.orm import local_session @@ -14,4 +15,5 @@ async def storages_init(): RoleStorage.init(session) UserStorage.init(session) TopicStorage.init(session) + SearchService.init(session) session.commit() diff --git a/services/search.py b/services/search.py new file mode 100644 index 00000000..6e03a853 --- /dev/null +++ b/services/search.py @@ -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