From 903ee594a39942f302c6e88dc0a03777c4a8603b Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Tue, 9 Jun 2026 18:55:24 +0200 Subject: [PATCH 1/2] count primary topics via ES aggregation instead of full works --- service/src/main/inse/works/logic.py | 110 +++++++++++++++++++-------- 1 file changed, 79 insertions(+), 31 deletions(-) diff --git a/service/src/main/inse/works/logic.py b/service/src/main/inse/works/logic.py index ddf63707..b1ec3310 100644 --- a/service/src/main/inse/works/logic.py +++ b/service/src/main/inse/works/logic.py @@ -5,6 +5,7 @@ Business logic for publication works. """ import logging +from time import time from typing import List import httpx @@ -14,6 +15,7 @@ from starlette.status import HTTP_502_BAD_GATEWAY from starlette.status import HTTP_404_NOT_FOUND from starlette.status import HTTP_503_SERVICE_UNAVAILABLE +from inse import config from inse.integrations import elasticsearch from inse.models.work import Work from inse.models.work_page import WorkPage @@ -100,34 +102,76 @@ async def get_primary_topic_counts( author_id: str, client: OpenAlexWorksSearchClient | None = None, ) -> List[TopicCount]: - works = await get_all_author_works(author_id, client=client) + topic_counts = _get_primary_topic_counts_from_es(author_id) - counts = {} + topic_ids = [item["id"] for item in topic_counts if item["id"]] + details = _get_topic_details(topic_ids) - for work in works.results or []: - primary_topic = work.primary_topic - if not primary_topic or not primary_topic.display_name: + result = [] + + for item in topic_counts: + info = details.get(item["id"], {}) + name = info.get("display_name") + if not name: continue - name = primary_topic.display_name - if name not in counts: - counts[name] = {"id": primary_topic.id, "name": name, "count": 0} - counts[name]["count"] += 1 - - items = sorted(counts.values(), key=lambda item: item["count"], reverse=True) - - topic_ids = [item["id"] for item in items if item["id"]] - descriptions = _get_topic_descriptions(topic_ids) - - return [ - TopicCount( - id=item["id"], - name=item["name"], - count=item["count"], - description=descriptions.get(item["id"]), + result.append( + TopicCount( + id=item["id"], + name=name, + count=item["count"], + description=info.get("description"), + ) ) - for item in items - ] + + return result + + +def _get_primary_topic_counts_from_es(author_id: str) -> list[dict]: + + indexed_author_id = f"https://openalex.org/{author_id.strip()}" + + body = { + "size": 0, + "query": { + "nested": { + "path": "authorships", + "query": { + "bool": { + "filter": [ + {"term": {"authorships.author.id": indexed_author_id}}, + ], + }, + }, + }, + }, + "aggs": { + "primary_topics": { + "terms": {"field": "primary_topic.id", "size": 1000}, + }, + }, + } + + try: + response = elasticsearch.search( + index=config.settings.openalex_works_search_index, + body=body, + ) + except httpx.HTTPError as exc: + logger.exception("OpenAlex primary topic aggregation request failed.") + raise HTTPException( + status_code=HTTP_502_BAD_GATEWAY, + detail="OpenAlex works search request failed.", + ) from exc + + aggregations = response.get("aggregations", {}) + buckets = aggregations.get("primary_topics", {}).get("buckets", []) + + counts = [] + for bucket in buckets: + counts.append({"id": bucket["key"], "count": bucket["doc_count"]}) + + return counts async def get_work( @@ -156,7 +200,7 @@ async def get_work( return elements[0] -def _get_topic_descriptions(topic_ids: List[str]) -> dict[str, str]: +def _get_topic_details(topic_ids: List[str]) -> dict[str, dict]: if not topic_ids: return {} @@ -164,22 +208,26 @@ def _get_topic_descriptions(topic_ids: List[str]) -> dict[str, str]: response = elasticsearch.search( index="openalex-topics", query={"terms": {"id": topic_ids}}, - source=["id", "description"], + source=["id", "display_name", "description"], size=len(topic_ids), ) except httpx.HTTPError: - logger.exception("Could not load topic descriptions from Elasticsearch.") + logger.exception("Could not load topic details from Elasticsearch.") return {} - descriptions = {} + details = {} for hit in response.get("hits", {}).get("hits", []): source = hit.get("_source", {}) topic_id = source.get("id") - description = source.get("description") - if topic_id and description: - descriptions[topic_id] = description + if not topic_id: + continue - return descriptions + details[topic_id] = { + "display_name": source.get("display_name"), + "description": source.get("description"), + } + + return details def _limit_works_by_complete_years( -- 2.56.1 From fc123d81aa49c8bc5f805f1987fcc3a554da15b2 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Tue, 9 Jun 2026 20:18:28 +0200 Subject: [PATCH 2/2] replace * marker with paper icon --- .../person-interesting.component.html | 10 ++++++++-- .../person-interesting.component.scss | 18 ++++++++++++++++++ .../person-interesting.component.ts | 3 ++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/webapp/src/app/modules/person-interesting/person-interesting.component.html b/webapp/src/app/modules/person-interesting/person-interesting.component.html index 34c35799..92f4d52a 100644 --- a/webapp/src/app/modules/person-interesting/person-interesting.component.html +++ b/webapp/src/app/modules/person-interesting/person-interesting.component.html @@ -2,7 +2,10 @@

Interessant für

@if (tags().length > 0) { - * Anzahl Publikationen zum Thema + + article + Anzahl Publikationen zum Thema + }
@@ -16,7 +19,10 @@ [disabled]="!tag.id" (click)="onTagClick(tag)"> {{ tag.label }} - {{ tag.count }}* + + {{ tag.count }} + article + } } @else { diff --git a/webapp/src/app/modules/person-interesting/person-interesting.component.scss b/webapp/src/app/modules/person-interesting/person-interesting.component.scss index efedb965..178b12b0 100644 --- a/webapp/src/app/modules/person-interesting/person-interesting.component.scss +++ b/webapp/src/app/modules/person-interesting/person-interesting.component.scss @@ -73,6 +73,7 @@ flex: 0 0 auto; align-items: center; justify-content: center; + gap: 2px; min-width: 18px; height: 18px; padding: 0 5px; @@ -83,6 +84,13 @@ font-weight: 700; } +.topic-count .count-icon { + width: 13px; + height: 13px; + font-size: 13px; + line-height: 1; +} + .purple { --topic-border: #d8cafe; --topic-bg: #f6f0ff; @@ -114,10 +122,20 @@ } .interest-footnote { + display: inline-flex; + align-items: center; + gap: 4px; color: var(--sg-text-muted); font-size: 11px; } +.interest-footnote .footnote-icon { + width: 14px; + height: 14px; + font-size: 14px; + line-height: 1; +} + .interest-tags .interest-empty { padding: 9px 16px; border: 1px solid #e2e8f0; diff --git a/webapp/src/app/modules/person-interesting/person-interesting.component.ts b/webapp/src/app/modules/person-interesting/person-interesting.component.ts index 0e981037..d137caa6 100644 --- a/webapp/src/app/modules/person-interesting/person-interesting.component.ts +++ b/webapp/src/app/modules/person-interesting/person-interesting.component.ts @@ -1,5 +1,6 @@ import { Component, input, computed, output } from '@angular/core'; import { MatTooltipModule } from '@angular/material/tooltip'; +import { MatIconModule } from '@angular/material/icon'; import { CommonModule } from '@angular/common'; import { TopicCount } from '../../api'; @@ -16,7 +17,7 @@ const colors = ['purple', 'blue', 'green', 'orange', 'pink']; @Component({ selector: 'app-person-interesting', - imports: [CommonModule, MatTooltipModule], + imports: [CommonModule, MatTooltipModule, MatIconModule], templateUrl: './person-interesting.component.html', styleUrl: './person-interesting.component.scss', }) -- 2.56.1