Update unified search providers for unified contacts model (company queries contacts with type=company)

This commit is contained in:
Agent Zero
2026-07-19 21:32:24 +02:00
parent 3177daf47f
commit c670846cbd
5 changed files with 50 additions and 40 deletions
@@ -1,4 +1,4 @@
"""Company search provider."""
"""Company search provider — queries contacts with type='company'."""
from __future__ import annotations
@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
class CompanySearchProvider:
"""Search provider for Company entities."""
"""Search provider for Company entities (contacts with type='company')."""
entity_type = "company"
@@ -24,13 +24,14 @@ class CompanySearchProvider:
tenant_id: uuid.UUID,
limit: int,
) -> list[dict[str, Any]]:
"""Full-text search on companies.search_tsv."""
"""Full-text search on contacts.search_tsv where type='company'."""
sql = text(
"""
SELECT c.*, ts_rank(c.search_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
FROM companies c
FROM contacts c
WHERE c.tenant_id = :tid
AND c.deleted_at IS NULL
AND c.type = 'company'
AND c.search_tsv @@ to_tsquery('pg_catalog.german', :q)
ORDER BY rank DESC
LIMIT :lim
@@ -50,13 +51,14 @@ class CompanySearchProvider:
tenant_id: uuid.UUID,
limit: int,
) -> list[dict[str, Any]]:
"""Semantic search on companies.embedding."""
"""Semantic search on contacts.embedding where type='company'."""
sql = text(
"""
SELECT c.*, 1 - (c.embedding <=> cast(:emb AS vector)) AS score
FROM companies c
FROM contacts c
WHERE c.tenant_id = :tid
AND c.deleted_at IS NULL
AND c.type = 'company'
AND c.embedding IS NOT NULL
ORDER BY c.embedding <=> cast(:emb AS vector)
LIMIT :lim
@@ -78,9 +80,10 @@ class CompanySearchProvider:
"""Get text for embedding generation."""
sql = text(
"""
SELECT name, description, industry
FROM companies
WHERE id = :eid AND tenant_id = :tid
SELECT displayname, name, email_1, email_2, phone_1, website,
mailing_city, tags, vat_code
FROM contacts
WHERE id = :eid AND tenant_id = :tid AND type = 'company'
"""
)
result = await db.execute(sql, {"eid": entity_id, "tid": tenant_id})
@@ -88,27 +91,31 @@ class CompanySearchProvider:
if not row:
return ""
parts = [
row.get("displayname", ""),
row.get("name", ""),
row.get("description", ""),
row.get("industry", ""),
row.get("email_1", ""),
row.get("phone_1", ""),
row.get("website", ""),
row.get("mailing_city", ""),
row.get("tags", ""),
]
return " ".join(str(p) for p in parts if p)
def to_search_result(self, entity: object) -> dict[str, Any]:
"""Convert company to search result dict."""
if isinstance(entity, dict):
name = entity.get("name", "")
description = entity.get("description", "") or ""
name = entity.get("name", "") or entity.get("displayname", "")
email = entity.get("email_1", "") or ""
entity_id = str(entity.get("id", ""))
else:
name = getattr(entity, "name", "")
description = getattr(entity, "description", "") or ""
name = getattr(entity, "name", "") or getattr(entity, "displayname", "")
email = getattr(entity, "email_1", "") or ""
entity_id = str(getattr(entity, "id", ""))
return {
"entity_type": self.entity_type,
"entity_id": entity_id,
"title": name,
"snippet": description[:200],
"snippet": email[:200],
"score": 0.0,
"data": {},
}