Initial upload: snippet-manager with CI/CD workflow
This commit is contained in:
28
snip/search/engine.py
Normal file
28
snip/search/engine.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
"""FTS5 search engine for snippets."""
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from snip.db.database import Database
|
||||||
|
|
||||||
|
|
||||||
|
class SearchEngine:
|
||||||
|
def __init__(self, db: Database):
|
||||||
|
self.db = db
|
||||||
|
|
||||||
|
def search(
|
||||||
|
self,
|
||||||
|
query: str,
|
||||||
|
limit: int = 50,
|
||||||
|
language: str | None = None,
|
||||||
|
tag: str | None = None,
|
||||||
|
) -> list[dict[str, Any]]:
|
||||||
|
"""Search snippets using FTS5."""
|
||||||
|
return self.db.search_snippets(query, limit=limit, language=language, tag=tag)
|
||||||
|
|
||||||
|
def highlight(self, text: str, query: str) -> str:
|
||||||
|
"""Add highlighting markers around matched terms."""
|
||||||
|
terms = query.split()
|
||||||
|
result = text
|
||||||
|
for term in terms:
|
||||||
|
result = result.replace(term, f"**{term}**")
|
||||||
|
return result
|
||||||
Reference in New Issue
Block a user