fix(keyword-search): add fuzzy search fallback for news
This commit is contained in:
@@ -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",
|
||||
|
||||
+15
-4
@@ -119,12 +119,23 @@
|
||||
|
||||
@if (!hasSearchInput()) {
|
||||
<div class="no-results">
|
||||
@if (fuzzySearch()) {
|
||||
<span>
|
||||
Keines der gewählten Keywords konnte einem Thema zugeordnet werden.
|
||||
<span>Keine Keywords ausgewählt.</span>
|
||||
</div>
|
||||
} @else if (total() === 0) {
|
||||
<div class="no-results">
|
||||
@if (hasNarrowingFilters()) {
|
||||
<span>Keine Nachrichten für die aktuellen Filter.</span>
|
||||
<span class="no-results__hint">
|
||||
Setzen Sie die Filter zurück, um mehr Ergebnisse zu sehen.
|
||||
</span>
|
||||
} @else if (searchedByTopics()) {
|
||||
<span>Zu diesen Themen gibt es keine Nachrichten.</span>
|
||||
<span class="no-results__hint">
|
||||
Campus Aktuell deckt nicht jedes Forschungsthema ab. Schalten Sie die
|
||||
Fuzzy Search aus, um direkt nach den Keywords zu suchen.
|
||||
</span>
|
||||
} @else {
|
||||
<span>Keine Keywords ausgewählt.</span>
|
||||
<span>Keine Nachrichten zu diesen Keywords gefunden.</span>
|
||||
}
|
||||
</div>
|
||||
} @else {
|
||||
|
||||
+11
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+21
-1
@@ -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<TemplateRef<any>>('titleTmp');
|
||||
dateTmp = viewChild.required<TemplateRef<any>>('dateTmp');
|
||||
relevanceTmp = viewChild.required<TemplateRef<any>>('relevanceTmp');
|
||||
|
||||
@@ -32,7 +32,11 @@ export const keywordsNewsPageResolver: ResolveFn<NewsPage> = (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));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user