batch career metrics for search results

This commit is contained in:
2026-06-19 13:35:17 +02:00
parent 6e3c5107f1
commit c62f284866
+126 -11
View File
@@ -188,8 +188,17 @@ def _fetch_works_and_topics(author_id: str) -> tuple[list, list]:
return works, topics
def _get_openalex_author_url(author_id: str) -> str:
author_id = author_id.strip()
if author_id.startswith("https://openalex.org/"):
return author_id
return f"https://openalex.org/{author_id}"
def _get_publication_years_for_author(author_id: str) -> list[int]:
author_url = f"https://openalex.org/{author_id.strip()}"
author_url = _get_openalex_author_url(author_id)
body = {
"size": 0,
@@ -237,6 +246,7 @@ def _get_publication_years_for_author(author_id: str) -> list[int]:
return publication_years
def _classify_career_stage(active_years: int) -> str:
if active_years <= 0:
return "unknown"
@@ -297,11 +307,108 @@ def _compute_career_metrics(publication_years: list[int]) -> CareerMetrics:
)
def _get_publication_years_for_authors(author_ids: list[str]) -> dict[str, list[int]]:
author_ids = list(dict.fromkeys(author_id for author_id in author_ids if author_id))
if not author_ids:
return {}
author_url_by_id = {
author_id: _get_openalex_author_url(author_id) for author_id in author_ids
}
body = {
"size": 0,
"query": {
"nested": {
"path": "authorships",
"query": {
"terms": {
"authorships.author.id": list(author_url_by_id.values())
},
},
},
},
"aggs": {
"authors": {
"filters": {
"filters": {
author_id: {
"nested": {
"path": "authorships",
"query": {
"term": {
"authorships.author.id": author_url
},
},
},
}
for author_id, author_url in author_url_by_id.items()
},
},
"aggs": {
"publication_years": {
"terms": {
"field": "publication_year",
"size": 1000,
},
},
},
},
},
}
try:
response = elasticsearch.search(
index=config.settings.openalex_works_search_index,
body=body,
)
except httpx.TimeoutException:
logger.warning(
"Batch career metrics Elasticsearch request timed out. "
"Returning empty career metrics for this search page."
)
return {author_id: [] for author_id in author_ids}
publication_years_by_author = {author_id: [] for author_id in author_ids}
author_buckets = (
response.get("aggregations", {})
.get("authors", {})
.get("buckets", {})
)
for author_id, author_bucket in author_buckets.items():
year_buckets = (
author_bucket.get("publication_years", {})
.get("buckets", [])
)
for year_bucket in year_buckets:
year = year_bucket.get("key")
count = year_bucket.get("doc_count", 0)
if isinstance(year, int):
publication_years_by_author[author_id].extend([year] * count)
return publication_years_by_author
def get_career_metrics_for_author(author_id: str) -> CareerMetrics:
publication_years = _get_publication_years_for_author(author_id)
return _compute_career_metrics(publication_years)
def get_career_metrics_for_authors(author_ids: list[str]) -> dict[str, CareerMetrics]:
publication_years_by_author = _get_publication_years_for_authors(author_ids)
return {
author_id: _compute_career_metrics(
publication_years_by_author.get(author_id, [])
)
for author_id in author_ids
}
def _build_papers(works: list) -> list:
papers = []
@@ -686,18 +793,26 @@ def advanced_search_persons(
raw_data = util.get_persons_data(hit)
custom_author = _enrich_author_data(raw_data)
if custom_author:
if include_career_metrics:
start_time = time.time()
custom_author.career_metrics = get_career_metrics_for_author(
custom_author.id
)
elapsed_time = time.time() - start_time
logger.info(
f"Career metrics calculation for author {custom_author.id} took "
f"{elapsed_time:.2f} seconds"
)
results.append(custom_author)
if include_career_metrics and results:
author_ids = [custom_author.id for custom_author in results if custom_author.id]
start_time = time.time()
career_metrics_by_author = get_career_metrics_for_authors(author_ids)
elapsed_time = time.time() - start_time
logger.info(
f"Batch career metrics calculation for {len(author_ids)} authors took "
f"{elapsed_time:.2f} seconds"
)
for custom_author in results:
if custom_author.id:
custom_author.career_metrics = career_metrics_by_author.get(
custom_author.id, CareerMetrics()
)
return CustomAuthorsListResponse(
results=results, meta=Meta(count=total_count, page=page, per_page=page_size)
)