From 882ad3b8cf8e31dc5b97cacf8799e4454596d089 Mon Sep 17 00:00:00 2001 From: "noemi.kallweit" Date: Mon, 10 Aug 2026 15:30:18 +0200 Subject: [PATCH] refactor(news): deduplicate topic reading logic --- service/src/main/inse/news/logic.py | 45 +++-------------------------- 1 file changed, 4 insertions(+), 41 deletions(-) diff --git a/service/src/main/inse/news/logic.py b/service/src/main/inse/news/logic.py index 7e24f020..da4ca3eb 100644 --- a/service/src/main/inse/news/logic.py +++ b/service/src/main/inse/news/logic.py @@ -27,7 +27,6 @@ TOPIC_NUM_CANDIDATES = 100 TOPIC_SIMILARITY = 0.5 TOPICS_INDEX = "aktuell_openalex_topics" -TOPICS_INDEX_MAX_PAIRS = 100 _SESSION_TOPIC_CACHE: dict[str, list] = {} @@ -289,8 +288,8 @@ async def _attach_topics(articles: list) -> None: article.topics = topics_per_article.get(article.id, []) -async def _topics_for_news_ids(news_ids: list) -> dict: - """Batched variant of get_article_topics_from_index for a result page.""" +async def _topics_for_news_ids(news_ids: list) -> dict[str, list[MatchedTopic]]: + """Reads the indexed OpenAlex topic matches for the given articles.""" try: response = await elasticsearch.search( index=TOPICS_INDEX, @@ -303,7 +302,7 @@ async def _topics_for_news_ids(news_ids: list) -> dict: logger.exception("Batched topic lookup for the news result page failed.") return {} - topics_per_article: dict[str, list] = {} + topics_per_article: dict[str, list[MatchedTopic]] = {} seen_per_article: dict[str, set] = {} for hit in response.get("hits", {}).get("hits", []): source = hit.get("_source", {}) @@ -526,7 +525,7 @@ async def _get_article_keywords( async def _get_article_topics(news_id: str, keywords: list) -> list: - indexed = await get_article_topics_from_index(news_id) + indexed = (await _topics_for_news_ids([news_id])).get(news_id, []) if indexed: return indexed @@ -541,42 +540,6 @@ async def _get_article_topics(news_id: str, keywords: list) -> list: return topics -async def get_article_topics_from_index(news_id: str) -> list: - try: - response = await elasticsearch.search( - index=TOPICS_INDEX, - query={"term": {"news_id.keyword": news_id}}, - source=["keyword", "topic_matches"], - size=TOPICS_INDEX_MAX_PAIRS, - cluster="inse", - ) - except Exception: - logger.exception(f"Topic-index lookup for {news_id} failed.") - return [] - - matched = [] - seen_ids = set() - for hit in response.get("hits", {}).get("hits", []): - source = hit.get("_source", {}) - keyword = source.get("keyword") - for match in source.get("topic_matches") or []: - topic_id = match.get("topic_id") - if not topic_id or topic_id in seen_ids: - continue - seen_ids.add(topic_id) - matched.append( - MatchedTopic( - id=topic_id, - display_name=match.get("topic_display_name"), - subfield_name=match.get("subfield"), - field_name=match.get("field"), - domain_name=match.get("domain"), - source_keyword=keyword, - ) - ) - return matched - - def _normalize_keywords(candidates: list) -> list: """Trims, dedups (case-insensitive) and caps the keywords from the LLM.""" seen = set()