feat(author.py):addresolver for searching authors by text
All checks were successful
Deploy on push / deploy (push) Successful in 1m15s
All checks were successful
Deploy on push / deploy (push) Successful in 1m15s
This commit is contained in:
parent
fac43e5997
commit
93c00b3dd1
|
@ -20,7 +20,8 @@ from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.redis import redis
|
from services.redis import redis
|
||||||
from services.schema import mutation, query
|
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]
|
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)
|
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):
|
def get_author_id_from(slug="", user=None, author_id=None):
|
||||||
try:
|
try:
|
||||||
author_id = None
|
author_id = None
|
||||||
|
|
|
@ -4,7 +4,7 @@ type Query {
|
||||||
get_author_id(user: String!): Author
|
get_author_id(user: String!): Author
|
||||||
get_authors_all: [Author]
|
get_authors_all: [Author]
|
||||||
load_authors_by(by: AuthorsBy!, limit: Int, offset: Int): [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
|
# community
|
||||||
get_community: Community
|
get_community: Community
|
||||||
|
|
Loading…
Reference in New Issue
Block a user