person overview: enrich person search topics with descriptions
This commit is contained in:
@@ -32,6 +32,7 @@ from inse.persons.model import (
|
||||
CustomAuthorsListResponse,
|
||||
CareerMetrics,
|
||||
)
|
||||
from inse.works.logic import enrich_topic_descriptions
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
INDEX_NAME = "openalex-authors"
|
||||
@@ -728,6 +729,10 @@ async def advanced_search_persons(
|
||||
)
|
||||
results.append(custom_author)
|
||||
|
||||
await enrich_topic_descriptions(
|
||||
[topic for author in results for topic in author.topics or []]
|
||||
)
|
||||
|
||||
return CustomAuthorsListResponse(
|
||||
results=results, meta=Meta(count=total_count, page=page, per_page=page_size)
|
||||
)
|
||||
|
||||
@@ -207,39 +207,38 @@ async def get_work(
|
||||
return elements[0]
|
||||
|
||||
|
||||
async def _enrich_work_topic_descriptions(works: List[Work]) -> None:
|
||||
"""Fill in topic descriptions missing from raw work documents.
|
||||
async def enrich_topic_descriptions(topics: List) -> None:
|
||||
"""Batch-fill missing descriptions on dehydrated topics.
|
||||
|
||||
OpenAlex work and author documents carry dehydrated topics without a
|
||||
description, so we batch-load the details from the openalex-topics index.
|
||||
"""
|
||||
topic_ids = set()
|
||||
pending = [topic for topic in topics if topic.id and not topic.description]
|
||||
|
||||
for work in works:
|
||||
for topic in work.topics or []:
|
||||
if topic.id and not topic.description:
|
||||
topic_ids.add(topic.id)
|
||||
|
||||
primary = work.primary_topic
|
||||
if primary and primary.id and not primary.description:
|
||||
topic_ids.add(primary.id)
|
||||
|
||||
if not topic_ids:
|
||||
if not pending:
|
||||
return
|
||||
|
||||
details = await _get_topic_details(list(topic_ids))
|
||||
details = await _get_topic_details(list({topic.id for topic in pending}))
|
||||
|
||||
if not details:
|
||||
return
|
||||
|
||||
for work in works:
|
||||
for topic in work.topics or []:
|
||||
info = details.get(topic.id)
|
||||
if info and not topic.description:
|
||||
topic.description = info.get("description")
|
||||
for topic in pending:
|
||||
info = details.get(topic.id)
|
||||
if info:
|
||||
topic.description = info.get("description")
|
||||
|
||||
primary = work.primary_topic
|
||||
if primary and primary.id:
|
||||
info = details.get(primary.id)
|
||||
if info and not primary.description:
|
||||
primary.description = info.get("description")
|
||||
|
||||
async def _enrich_work_topic_descriptions(works: List[Work]) -> None:
|
||||
topics = []
|
||||
|
||||
for work in works:
|
||||
topics.extend(work.topics or [])
|
||||
|
||||
if work.primary_topic:
|
||||
topics.append(work.primary_topic)
|
||||
|
||||
await enrich_topic_descriptions(topics)
|
||||
|
||||
|
||||
async def _get_topic_details(topic_ids: List[str]) -> dict[str, dict]:
|
||||
|
||||
Reference in New Issue
Block a user