From 93c00b3dd1aa216e663dcd204d2877aac21ecf22 Mon Sep 17 00:00:00 2001 From: Stepan Vladovskiy Date: Tue, 29 Apr 2025 17:45:37 -0300 Subject: [PATCH] feat(author.py):addresolver for searching authors by text --- resolvers/author.py | 42 +++++++++++++++++++++++++++++++++++++++++- schema/query.graphql | 2 +- services/search.py | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 91bad2e5..7f7c9c3e 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -20,7 +20,8 @@ from services.auth import login_required from services.db import local_session from services.redis import redis from services.schema import mutation, query -from utils.logger import root_logger as logger +from services.search import search_service +from utils.logger import logger, root_logger as root_logger DEFAULT_COMMUNITIES = [1] @@ -301,6 +302,45 @@ async def load_authors_by(_, _info, by, limit, offset): return await get_authors_with_stats(limit, offset, by) +@query.field("load_authors_search") +async def load_authors_search_resolver(_, text: str, limit: int = 10, offset: int = 0): + """Resolver for searching authors by text.""" + logger.info(f"Executing load_authors_search for text: '{text}', limit: {limit}, offset: {offset}") + + # Get author IDs from search engine (already sorted by relevance) + search_results = await search_service.search_authors(text, limit, offset) + + if not search_results: + logger.info(f"No authors found in search for '{text}'") + return [] + + author_ids = [result.get("id") for result in search_results if result.get("id")] + if not author_ids: + logger.warning(f"Search for '{text}' returned results but no valid IDs.") + return [] + + logger.info(f"Search returned {len(author_ids)} author IDs: {author_ids}") + + # Fetch full author objects from DB + with local_session() as session: + # Simple query to get authors by IDs - no need for stats here + authors_query = select(Author).filter(Author.id.in_(author_ids)) + db_authors = session.execute(authors_query).scalars().all() + + if not db_authors: + logger.warning(f"No authors found in DB for IDs: {author_ids}") + return [] + + # Create a dictionary for quick lookup + authors_dict = {str(author.id): author for author in db_authors} + + # Keep the order from search results (maintains the relevance sorting) + ordered_authors = [authors_dict[author_id] for author_id in author_ids if author_id in authors_dict] + + logger.info(f"Returning {len(ordered_authors)} authors matching search order.") + return ordered_authors + + def get_author_id_from(slug="", user=None, author_id=None): try: author_id = None diff --git a/schema/query.graphql b/schema/query.graphql index 96055bc6..6ab85e75 100644 --- a/schema/query.graphql +++ b/schema/query.graphql @@ -4,7 +4,7 @@ type Query { get_author_id(user: String!): Author get_authors_all: [Author] load_authors_by(by: AuthorsBy!, limit: Int, offset: Int): [Author] - # search_authors(what: String!): [Author] + load_authors_search(text: String!, limit: Int, offset: Int): [Author!] # Search for authors by name or bio # community get_community: Community diff --git a/services/search.py b/services/search.py index 90cf20e2..c912e98b 100644 --- a/services/search.py +++ b/services/search.py @@ -514,7 +514,7 @@ class SearchService: # Convert author dict to list author_docs_list = list(author_docs.values()) - + # Log indexing started message logger.info("indexing started...")