From d8b7d8bc2eb00bc96ec5a74ac7cb81ea2a76502e Mon Sep 17 00:00:00 2001 From: "noemi.kallweit" Date: Mon, 10 Aug 2026 15:06:39 +0200 Subject: [PATCH] fix(keyword-search): add fuzzy search fallback for news --- service/src/main/inse/news/logic.py | 28 +++++++++++-------- .../news-results/news-results.component.html | 19 ++++++++++--- .../news-results/news-results.component.scss | 12 +++++++- .../news-results/news-results.component.ts | 22 ++++++++++++++- .../resolvers/keywords-news-page.resolver.ts | 6 +++- 5 files changed, 69 insertions(+), 18 deletions(-) diff --git a/service/src/main/inse/news/logic.py b/service/src/main/inse/news/logic.py index f46417e5..7e24f020 100644 --- a/service/src/main/inse/news/logic.py +++ b/service/src/main/inse/news/logic.py @@ -153,12 +153,24 @@ async def search_news_by_keywords( would leave every multi-keyword search empty. Articles matching more of them simply rank higher. + A fuzzy search without any resolved topic ids falls back to the raw + keywords. Once there are topic ids the fallback is off: a topic search that matches no article + stays empty instead of silently returning keyword hits. + By default results stay ranked by match strength (Elasticsearch's `_score`). Passing `sort_order` ('asc'/'desc') overrides that with a plain date sort, same as the plain news list. """ + wanted_topic_ids: set = set() if fuzzy: - should = await _topic_id_should_clauses(topic_ids, filter_topic_ids) + wanted_topic_ids = { + topic_id + for topic_id in (topic_ids or []) + (filter_topic_ids or []) + if topic_id + } + + if wanted_topic_ids: + should = await _topic_id_should_clauses(wanted_topic_ids) else: should = _keyword_should_clauses(keywords) @@ -189,7 +201,7 @@ async def search_news_by_keywords( size=page_size, offset=(page - 1) * page_size, sort=sort, - highlight=highlight if not fuzzy else None, + highlight=None if wanted_topic_ids else highlight, ) news_page = await _build_page(response, page, page_size) @@ -212,19 +224,13 @@ def _keyword_should_clauses(keywords: list | None) -> list: ] -async def _topic_id_should_clauses( - topic_ids: list | None, filter_topic_ids: list | None -) -> list: +async def _topic_id_should_clauses(topic_ids: set) -> list: """Turns matched OpenAlex topics into id clauses, boosted by hit count. Articles that match several of the searched topics get a higher boost, so Elasticsearch ranks them above articles that only match one. """ - wanted = {tid for tid in (topic_ids or []) + (filter_topic_ids or []) if tid} - if not wanted: - return [] - - topics_per_article = await _news_ids_by_topic_ids(wanted) + topics_per_article = await _news_ids_by_topic_ids(topic_ids) if not topics_per_article: return [] @@ -715,7 +721,7 @@ async def _get_postproc_labels(news_id: str) -> tuple[bool | None, list, list, s return relevant, authors, keywords, hit.get("_id") -async def _run_search(query: dict, source: list, size: int, offset: int, sort, highlight: dict = None) -> dict: +async def _run_search(query: dict, source: list, size: int, offset: int, sort, highlight: dict | None = None) -> dict: try: return await elasticsearch.search( index="aktuell", diff --git a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html index 24c82696..2e594732 100644 --- a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html +++ b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html @@ -119,12 +119,23 @@ @if (!hasSearchInput()) {
- @if (fuzzySearch()) { - - Keines der gewählten Keywords konnte einem Thema zugeordnet werden. + Keine Keywords ausgewählt. +
+ } @else if (total() === 0) { +
+ @if (hasNarrowingFilters()) { + Keine Nachrichten für die aktuellen Filter. + + Setzen Sie die Filter zurück, um mehr Ergebnisse zu sehen. + + } @else if (searchedByTopics()) { + Zu diesen Themen gibt es keine Nachrichten. + + Campus Aktuell deckt nicht jedes Forschungsthema ab. Schalten Sie die + Fuzzy Search aus, um direkt nach den Keywords zu suchen. } @else { - Keine Keywords ausgewählt. + Keine Nachrichten zu diesen Keywords gefunden. }
} @else { diff --git a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss index 6c6b9fc5..0fd9f342 100644 --- a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss +++ b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss @@ -169,7 +169,17 @@ .no-results { display: flex; - justify-content: center; + flex-direction: column; + align-items: center; + gap: 0.5rem; padding: 2rem; color: var(--sg-text-muted); + text-align: center; + + &__hint { + max-width: 34rem; + color: var(--sg-text-soft); + font-size: 0.8125rem; + line-height: 1.5; + } } diff --git a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts index a191135f..2bc16551 100644 --- a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts +++ b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts @@ -103,7 +103,7 @@ export class KeywordsToNewsResultsComponent { readonly hasSearchInput = computed(() => this.fuzzySearch() - ? this.state.topicIds().length > 0 + ? this.state.topicIds().length > 0 || this.state.keywords().length > 0 : this.state.keywords().length > 0, ); @@ -122,6 +122,26 @@ export class KeywordsToNewsResultsComponent { ); }); + readonly hasNarrowingFilters = computed(() => { + const params = this.queryParamMap(); + return ( + !!params.get(keywordsNewsQueryParamKeys.relevance) || + !!params.get(keywordsNewsQueryParamKeys.yearFrom) || + !!params.get(keywordsNewsQueryParamKeys.yearTo) || + this.selectedTopicIds().length > 0 + ); + }); + + readonly searchedByTopics = computed(() => { + if (!this.fuzzySearch()) { + return false; + } + const baseTopicIds = (this.queryParamMap().get('topics') ?? '') + .split(',') + .filter((value) => value.trim()); + return baseTopicIds.length > 0 || this.selectedTopicIds().length > 0; + }); + titleTmp = viewChild.required>('titleTmp'); dateTmp = viewChild.required>('dateTmp'); relevanceTmp = viewChild.required>('relevanceTmp'); diff --git a/webapp/src/app/resolvers/keywords-news-page.resolver.ts b/webapp/src/app/resolvers/keywords-news-page.resolver.ts index 12ea788c..a347122c 100644 --- a/webapp/src/app/resolvers/keywords-news-page.resolver.ts +++ b/webapp/src/app/resolvers/keywords-news-page.resolver.ts @@ -32,7 +32,11 @@ export const keywordsNewsPageResolver: ResolveFn = (route) => { const filterTopicIds = params.getAll(keywordsNewsQueryParamKeys.topicId); const fuzzy = params.get('fuzzy') === 'true'; - if (fuzzy ? topicIds.length === 0 : keywords.length === 0) { + const canSearch = fuzzy + ? topicIds.length > 0 || keywords.length > 0 + : keywords.length > 0; + + if (!canSearch) { return of(emptyPage(page + 1, size)); }