Update unified search providers for unified contacts model (company queries contacts with type=company)
This commit is contained in:
@@ -181,7 +181,7 @@ async def index_entity(
|
|||||||
|
|
||||||
table_map = {
|
table_map = {
|
||||||
"contact": "contacts",
|
"contact": "contacts",
|
||||||
"company": "companies",
|
"company": "contacts",
|
||||||
"mail": "mails",
|
"mail": "mails",
|
||||||
"file": "files",
|
"file": "files",
|
||||||
"event": "calendar_entries",
|
"event": "calendar_entries",
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ async def index_company(ctx: dict[str, Any], company_id: str) -> None:
|
|||||||
try:
|
try:
|
||||||
eid = _parse_id(company_id)
|
eid = _parse_id(company_id)
|
||||||
result = await db.execute(
|
result = await db.execute(
|
||||||
text("SELECT tenant_id FROM companies WHERE id = :cid"),
|
text("SELECT tenant_id FROM contacts WHERE id = :cid"),
|
||||||
{"cid": eid},
|
{"cid": eid},
|
||||||
)
|
)
|
||||||
row = result.mappings().first()
|
row = result.mappings().first()
|
||||||
@@ -170,7 +170,7 @@ async def reindex(ctx: dict[str, Any], entity_type: str) -> None:
|
|||||||
|
|
||||||
table_map = {
|
table_map = {
|
||||||
"contact": "contacts",
|
"contact": "contacts",
|
||||||
"company": "companies",
|
"company": "contacts",
|
||||||
"mail": "mails",
|
"mail": "mails",
|
||||||
"file": "files",
|
"file": "files",
|
||||||
"event": "calendar_entries",
|
"event": "calendar_entries",
|
||||||
@@ -216,7 +216,7 @@ async def embedding_batch(ctx: dict[str, Any]) -> None:
|
|||||||
|
|
||||||
table_map = {
|
table_map = {
|
||||||
"contact": "contacts",
|
"contact": "contacts",
|
||||||
"company": "companies",
|
"company": "contacts",
|
||||||
"mail": "mails",
|
"mail": "mails",
|
||||||
"file": "files",
|
"file": "files",
|
||||||
"event": "calendar_entries",
|
"event": "calendar_entries",
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Company search provider."""
|
"""Company search provider — queries contacts with type='company'."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class CompanySearchProvider:
|
class CompanySearchProvider:
|
||||||
"""Search provider for Company entities."""
|
"""Search provider for Company entities (contacts with type='company')."""
|
||||||
|
|
||||||
entity_type = "company"
|
entity_type = "company"
|
||||||
|
|
||||||
@@ -24,13 +24,14 @@ class CompanySearchProvider:
|
|||||||
tenant_id: uuid.UUID,
|
tenant_id: uuid.UUID,
|
||||||
limit: int,
|
limit: int,
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""Full-text search on companies.search_tsv."""
|
"""Full-text search on contacts.search_tsv where type='company'."""
|
||||||
sql = text(
|
sql = text(
|
||||||
"""
|
"""
|
||||||
SELECT c.*, ts_rank(c.search_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
|
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
|
WHERE c.tenant_id = :tid
|
||||||
AND c.deleted_at IS NULL
|
AND c.deleted_at IS NULL
|
||||||
|
AND c.type = 'company'
|
||||||
AND c.search_tsv @@ to_tsquery('pg_catalog.german', :q)
|
AND c.search_tsv @@ to_tsquery('pg_catalog.german', :q)
|
||||||
ORDER BY rank DESC
|
ORDER BY rank DESC
|
||||||
LIMIT :lim
|
LIMIT :lim
|
||||||
@@ -50,13 +51,14 @@ class CompanySearchProvider:
|
|||||||
tenant_id: uuid.UUID,
|
tenant_id: uuid.UUID,
|
||||||
limit: int,
|
limit: int,
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""Semantic search on companies.embedding."""
|
"""Semantic search on contacts.embedding where type='company'."""
|
||||||
sql = text(
|
sql = text(
|
||||||
"""
|
"""
|
||||||
SELECT c.*, 1 - (c.embedding <=> cast(:emb AS vector)) AS score
|
SELECT c.*, 1 - (c.embedding <=> cast(:emb AS vector)) AS score
|
||||||
FROM companies c
|
FROM contacts c
|
||||||
WHERE c.tenant_id = :tid
|
WHERE c.tenant_id = :tid
|
||||||
AND c.deleted_at IS NULL
|
AND c.deleted_at IS NULL
|
||||||
|
AND c.type = 'company'
|
||||||
AND c.embedding IS NOT NULL
|
AND c.embedding IS NOT NULL
|
||||||
ORDER BY c.embedding <=> cast(:emb AS vector)
|
ORDER BY c.embedding <=> cast(:emb AS vector)
|
||||||
LIMIT :lim
|
LIMIT :lim
|
||||||
@@ -78,9 +80,10 @@ class CompanySearchProvider:
|
|||||||
"""Get text for embedding generation."""
|
"""Get text for embedding generation."""
|
||||||
sql = text(
|
sql = text(
|
||||||
"""
|
"""
|
||||||
SELECT name, description, industry
|
SELECT displayname, name, email_1, email_2, phone_1, website,
|
||||||
FROM companies
|
mailing_city, tags, vat_code
|
||||||
WHERE id = :eid AND tenant_id = :tid
|
FROM contacts
|
||||||
|
WHERE id = :eid AND tenant_id = :tid AND type = 'company'
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
result = await db.execute(sql, {"eid": entity_id, "tid": tenant_id})
|
result = await db.execute(sql, {"eid": entity_id, "tid": tenant_id})
|
||||||
@@ -88,27 +91,31 @@ class CompanySearchProvider:
|
|||||||
if not row:
|
if not row:
|
||||||
return ""
|
return ""
|
||||||
parts = [
|
parts = [
|
||||||
|
row.get("displayname", ""),
|
||||||
row.get("name", ""),
|
row.get("name", ""),
|
||||||
row.get("description", ""),
|
row.get("email_1", ""),
|
||||||
row.get("industry", ""),
|
row.get("phone_1", ""),
|
||||||
|
row.get("website", ""),
|
||||||
|
row.get("mailing_city", ""),
|
||||||
|
row.get("tags", ""),
|
||||||
]
|
]
|
||||||
return " ".join(str(p) for p in parts if p)
|
return " ".join(str(p) for p in parts if p)
|
||||||
|
|
||||||
def to_search_result(self, entity: object) -> dict[str, Any]:
|
def to_search_result(self, entity: object) -> dict[str, Any]:
|
||||||
"""Convert company to search result dict."""
|
"""Convert company to search result dict."""
|
||||||
if isinstance(entity, dict):
|
if isinstance(entity, dict):
|
||||||
name = entity.get("name", "")
|
name = entity.get("name", "") or entity.get("displayname", "")
|
||||||
description = entity.get("description", "") or ""
|
email = entity.get("email_1", "") or ""
|
||||||
entity_id = str(entity.get("id", ""))
|
entity_id = str(entity.get("id", ""))
|
||||||
else:
|
else:
|
||||||
name = getattr(entity, "name", "")
|
name = getattr(entity, "name", "") or getattr(entity, "displayname", "")
|
||||||
description = getattr(entity, "description", "") or ""
|
email = getattr(entity, "email_1", "") or ""
|
||||||
entity_id = str(getattr(entity, "id", ""))
|
entity_id = str(getattr(entity, "id", ""))
|
||||||
return {
|
return {
|
||||||
"entity_type": self.entity_type,
|
"entity_type": self.entity_type,
|
||||||
"entity_id": entity_id,
|
"entity_id": entity_id,
|
||||||
"title": name,
|
"title": name,
|
||||||
"snippet": description[:200],
|
"snippet": email[:200],
|
||||||
"score": 0.0,
|
"score": 0.0,
|
||||||
"data": {},
|
"data": {},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Contact search provider."""
|
"""Contact search provider — works with unified contacts table."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -9,13 +9,11 @@ from typing import Any
|
|||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.models.contact import Contact
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ContactSearchProvider:
|
class ContactSearchProvider:
|
||||||
"""Search provider for Contact entities."""
|
"""Search provider for Contact entities (all types)."""
|
||||||
|
|
||||||
entity_type = "contact"
|
entity_type = "contact"
|
||||||
|
|
||||||
@@ -80,7 +78,8 @@ class ContactSearchProvider:
|
|||||||
"""Get text for embedding generation."""
|
"""Get text for embedding generation."""
|
||||||
sql = text(
|
sql = text(
|
||||||
"""
|
"""
|
||||||
SELECT first_name, last_name, email, phone, mobile, notes
|
SELECT displayname, name, firstname, surname, email_1, email_2,
|
||||||
|
phone_1, phone_2, mailing_city, tags, contact_warning
|
||||||
FROM contacts
|
FROM contacts
|
||||||
WHERE id = :eid AND tenant_id = :tid
|
WHERE id = :eid AND tenant_id = :tid
|
||||||
"""
|
"""
|
||||||
@@ -90,32 +89,36 @@ class ContactSearchProvider:
|
|||||||
if not row:
|
if not row:
|
||||||
return ""
|
return ""
|
||||||
parts = [
|
parts = [
|
||||||
row.get("first_name", ""),
|
row.get("displayname", ""),
|
||||||
row.get("last_name", ""),
|
row.get("name", ""),
|
||||||
row.get("email", ""),
|
row.get("firstname", ""),
|
||||||
row.get("phone", ""),
|
row.get("surname", ""),
|
||||||
row.get("mobile", ""),
|
row.get("email_1", ""),
|
||||||
row.get("notes", ""),
|
row.get("email_2", ""),
|
||||||
|
row.get("phone_1", ""),
|
||||||
|
row.get("phone_2", ""),
|
||||||
|
row.get("mailing_city", ""),
|
||||||
|
row.get("tags", ""),
|
||||||
]
|
]
|
||||||
return " ".join(str(p) for p in parts if p)
|
return " ".join(str(p) for p in parts if p)
|
||||||
|
|
||||||
def to_search_result(self, entity: object) -> dict[str, Any]:
|
def to_search_result(self, entity: object) -> dict[str, Any]:
|
||||||
"""Convert contact to search result dict."""
|
"""Convert contact to search result dict."""
|
||||||
if isinstance(entity, dict):
|
if isinstance(entity, dict):
|
||||||
first = entity.get("first_name", "")
|
displayname = entity.get("displayname", "")
|
||||||
last = entity.get("last_name", "")
|
email = entity.get("email_1", "")
|
||||||
email = entity.get("email", "")
|
|
||||||
entity_id = str(entity.get("id", ""))
|
entity_id = str(entity.get("id", ""))
|
||||||
|
contact_type = entity.get("type", "")
|
||||||
else:
|
else:
|
||||||
first = getattr(entity, "first_name", "")
|
displayname = getattr(entity, "displayname", "")
|
||||||
last = getattr(entity, "last_name", "")
|
email = getattr(entity, "email_1", "")
|
||||||
email = getattr(entity, "email", "")
|
|
||||||
entity_id = str(getattr(entity, "id", ""))
|
entity_id = str(getattr(entity, "id", ""))
|
||||||
|
contact_type = getattr(entity, "type", "")
|
||||||
return {
|
return {
|
||||||
"entity_type": self.entity_type,
|
"entity_type": self.entity_type,
|
||||||
"entity_id": entity_id,
|
"entity_id": entity_id,
|
||||||
"title": f"{first} {last}".strip(),
|
"title": displayname,
|
||||||
"snippet": email or "",
|
"snippet": email or "",
|
||||||
"score": 0.0,
|
"score": 0.0,
|
||||||
"data": {},
|
"data": {"type": contact_type},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,7 +265,7 @@ async def search_stats(
|
|||||||
stats: dict = {}
|
stats: dict = {}
|
||||||
tables = {
|
tables = {
|
||||||
"contacts": "embedding",
|
"contacts": "embedding",
|
||||||
"companies": "embedding",
|
"contacts": "embedding",
|
||||||
"mails": "embedding",
|
"mails": "embedding",
|
||||||
"files": "embedding",
|
"files": "embedding",
|
||||||
"calendar_entries": "embedding",
|
"calendar_entries": "embedding",
|
||||||
|
|||||||
Reference in New Issue
Block a user