fix: highlight full knowledge search terms
This commit is contained in:
@@ -657,6 +657,9 @@ def _merge_content_search_rows(rows, keyword: str, chunk_section_ids: set[int])
|
||||
for matched in _matched_in(str(row["title"] or ""), str(row["content"] or ""), keyword):
|
||||
if matched not in current["matchedIn"]:
|
||||
current["matchedIn"].append(matched)
|
||||
for term in _highlight_terms(str(row["title"] or ""), str(row["content"] or ""), keyword):
|
||||
if term not in current["highlightTerms"]:
|
||||
current["highlightTerms"].append(term)
|
||||
current["itemType"] = _primary_item_type(current["itemTypes"])
|
||||
return list(grouped.values())
|
||||
|
||||
@@ -688,6 +691,7 @@ def _content_search_item(row, keyword: str, has_chunks: bool) -> dict:
|
||||
"title": title,
|
||||
"snippet": _snippet(f"{title}\n{content}", keyword),
|
||||
"matchedIn": _matched_in(title, content, keyword),
|
||||
"highlightTerms": _highlight_terms(title, content, keyword),
|
||||
"itemTypes": [row["item_type"]],
|
||||
"hasChunks": has_chunks,
|
||||
"isCurrentVersion": True,
|
||||
@@ -712,6 +716,28 @@ def _matched_in(title: str, content: str, keyword: str) -> list[str]:
|
||||
return result or ["content"]
|
||||
|
||||
|
||||
def _highlight_terms(title: str, content: str, keyword: str) -> list[str]:
|
||||
keyword = keyword.strip()
|
||||
if not keyword:
|
||||
return []
|
||||
text = f"{title}\n{content}".lower()
|
||||
terms = [keyword]
|
||||
if keyword.lower() not in text:
|
||||
terms.extend(_keyword_parts(keyword))
|
||||
result: list[str] = []
|
||||
for term in sorted({item.strip() for item in terms if item.strip()}, key=len, reverse=True):
|
||||
if term.lower() in text and term not in result:
|
||||
result.append(term)
|
||||
return result or [keyword]
|
||||
|
||||
|
||||
def _keyword_parts(keyword: str) -> list[str]:
|
||||
parts = [part for part in keyword.replace(",", " ").replace(",", " ").split() if part]
|
||||
if parts:
|
||||
return parts
|
||||
return list(keyword) if len(keyword) <= 8 else []
|
||||
|
||||
|
||||
def _snippet(text: str, keyword: str, radius: int = 90) -> str:
|
||||
compact = " ".join(text.split())
|
||||
if not compact:
|
||||
|
||||
Reference in New Issue
Block a user