You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
37 lines
1.1 KiB
# backend/suggest.py (Flask/FastAPI style pseudocode) |
|
from arango_client import arango |
|
from fastapi import APIRouter, FastAPI |
|
from typing import Dict, List |
|
import os |
|
|
|
router = APIRouter(prefix="/api") |
|
|
|
AQL = """ |
|
FOR d IN names_view |
|
SEARCH ANALYZER(d.namn IN TOKENS(@q, "names_ngram"), "names_ngram") |
|
FILTER STARTS_WITH(LOWER(d.namn), @q) |
|
LIMIT @limit |
|
RETURN {"name": d.namn, "_key": d._key} |
|
""" |
|
|
|
@router.get("/suggest") |
|
def suggest(q: str, limit: int = 8) -> List[Dict[str, str]]: |
|
"""Return mention suggestions with both display name and document key. |
|
|
|
Args: |
|
q: Partial name entered after an at-sign (already trimmed by the caller). |
|
limit: Maximum number of matches to return (defaults to 8). |
|
|
|
Returns: |
|
A list of dictionaries, each containing at least a ``name`` field and, |
|
when possible, the Arango ``_key`` so the frontend can bind filters. |
|
""" |
|
query = (q or "").strip() |
|
if len(query) < 3: |
|
return [] |
|
cursor = arango.db.aql.execute(AQL, bind_vars={"q": query.lower(), "limit": limit}) |
|
suggestions: list[dict] = list(cursor) |
|
return suggestions |
|
|
|
app = FastAPI() |
|
app.include_router(router)
|
|
|