From ccade533587665160ca057e0e075d5f1d889023b Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Wed, 8 Jul 2026 14:24:25 +0200 Subject: [PATCH] add citation count --- service/src/main/inse/ranking/logic.py | 19 +++++++++++++++++++ service/src/main/inse/ranking/model.py | 3 +++ 2 files changed, 22 insertions(+) diff --git a/service/src/main/inse/ranking/logic.py b/service/src/main/inse/ranking/logic.py index fed2713a..72d14b68 100644 --- a/service/src/main/inse/ranking/logic.py +++ b/service/src/main/inse/ranking/logic.py @@ -95,6 +95,14 @@ def _build_query(keywords: List[str], operator: KeywordOperator, uni_id: str) -> "size": 1, }, }, + "to_work": { + "reverse_nested": {}, + "aggs": { + "citations": { + "sum": {"field": "cited_by_count"}, + }, + }, + }, }, }, }, @@ -111,11 +119,22 @@ def _display_name_from_bucket(author_bucket: dict) -> str: return "" +def _citations_from_bucket(author_bucket: dict) -> int: + """Reads the summed citations out of the reverse_nested sub-aggregation.""" + citations_value = ( + author_bucket.get("to_work", {}) + .get("citations", {}) + .get("value", 0) + ) + return int(citations_value) + + def _bucket_to_ranked_author(author_bucket: dict) -> RankedAuthor: return RankedAuthor( author_id=author_bucket.get("key", ""), display_name=_display_name_from_bucket(author_bucket), works_count=author_bucket.get("doc_count", 0), + citations_count=_citations_from_bucket(author_bucket), ) diff --git a/service/src/main/inse/ranking/model.py b/service/src/main/inse/ranking/model.py index 5dbfea96..21670e74 100644 --- a/service/src/main/inse/ranking/model.py +++ b/service/src/main/inse/ranking/model.py @@ -40,3 +40,6 @@ class RankedAuthor(BaseModel): author_id: str = Field(description="The OpenAlex ID of the author.") display_name: str = Field(description="The name of the author.") works_count: int = Field(description="Number of matching works by this author.") + citations_count: int = Field( + description="Total citations across this author's matching works.", + )