Group search results by owner; auto-resume stuck pending documents
This commit is contained in:
@@ -20,7 +20,7 @@ def _normalize_scope(text: str | None) -> str | None:
|
||||
|
||||
|
||||
def search_persons(query: str) -> list[dict]:
|
||||
"""Search persons by name, keeping separate result rows per search scope."""
|
||||
"""Search persons by name. One row per person, aggregating all their search scopes."""
|
||||
norm = normalize_arabic(query.strip())
|
||||
pattern = f"%{norm}%"
|
||||
raw_pattern = f"%{query.strip()}%"
|
||||
@@ -30,9 +30,9 @@ def search_persons(query: str) -> list[dict]:
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT p.*,
|
||||
{scope_expr} AS search_scope,
|
||||
COUNT(DISTINCT pr.id) AS property_count,
|
||||
COUNT(DISTINCT d.id) AS document_count
|
||||
COUNT(DISTINCT d.id) AS document_count,
|
||||
GROUP_CONCAT(DISTINCT {scope_expr}) AS search_scopes_raw
|
||||
FROM persons p
|
||||
LEFT JOIN documents d ON d.person_id = p.id
|
||||
LEFT JOIN properties pr ON pr.document_id = d.id
|
||||
@@ -41,14 +41,13 @@ def search_persons(query: str) -> list[dict]:
|
||||
OR p.father_name LIKE ?
|
||||
OR p.first_name LIKE ?
|
||||
OR p.family_name LIKE ?
|
||||
GROUP BY p.id, {scope_expr}
|
||||
GROUP BY p.id
|
||||
ORDER BY
|
||||
CASE WHEN p.first_name_norm = ? THEN 0
|
||||
WHEN p.family_name_norm = ? THEN 0
|
||||
ELSE 1 END,
|
||||
p.first_name,
|
||||
p.family_name,
|
||||
search_scope
|
||||
p.family_name
|
||||
""",
|
||||
(pattern, pattern, raw_pattern, raw_pattern, raw_pattern, norm, norm),
|
||||
).fetchall()
|
||||
@@ -56,7 +55,17 @@ def search_persons(query: str) -> list[dict]:
|
||||
results = []
|
||||
for row in rows:
|
||||
person = dict(row)
|
||||
person["search_scope"] = _normalize_scope(person.get("search_scope"))
|
||||
raw = person.pop("search_scopes_raw", None) or ""
|
||||
scopes = [s.strip() for s in raw.split(",") if s and s.strip()]
|
||||
# Deduplicate while preserving order
|
||||
seen = set()
|
||||
unique_scopes = []
|
||||
for s in scopes:
|
||||
if s not in seen:
|
||||
seen.add(s)
|
||||
unique_scopes.append(s)
|
||||
person["search_scopes"] = unique_scopes
|
||||
person["search_scope"] = "، ".join(unique_scopes) if unique_scopes else None
|
||||
results.append(person)
|
||||
return results
|
||||
|
||||
|
||||
Reference in New Issue
Block a user