Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat: optimize names search (#1412)

gbirman22a8caa9fd09
diff --git a/rust/cloud-storage/email_contact_search/.sqlx/query-e57d9f9da7bf3d99431ca7a4d074674f46d21e94aa1dd747a6095b7b369faff1.json b/rust/cloud-storage/email_contact_search/.sqlx/query-e57d9f9da7bf3d99431ca7a4d074674f46d21e94aa1dd747a6095b7b369faff1.json
deleted file mode 100644
index 6acada9fae..0000000000
--- a/rust/cloud-storage/email_contact_search/.sqlx/query-e57d9f9da7bf3d99431ca7a4d074674f46d21e94aa1dd747a6095b7b369faff1.json
+++ /dev/null
@@ -1,56 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH paginated_threads AS (\n SELECT t.id, t.latest_non_spam_message_ts\n FROM email_threads t\n WHERE t.link_id = (SELECT id FROM email_links WHERE macro_id = $1)\n AND t.latest_non_spam_message_ts IS NOT NULL\n AND (\n $4::timestamptz IS NULL\n OR (t.latest_non_spam_message_ts, t.id) < ($4, $5)\n )\n AND EXISTS (\n -- Check for sender matches\n SELECT 1\n FROM email_messages m\n JOIN email_contacts c ON c.id = m.from_contact_id\n WHERE m.thread_id = t.id\n AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR m.from_name ILIKE $2)\n\n UNION ALL\n\n -- Check for recipient matches\n SELECT 1\n FROM email_messages m\n JOIN email_message_recipients mr ON mr.message_id = m.id\n JOIN email_contacts c ON c.id = mr.contact_id\n WHERE m.thread_id = t.id\n AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR mr.name ILIKE $2)\n )\n ORDER BY t.latest_non_spam_message_ts DESC, t.id DESC\n LIMIT $3\n )\n SELECT\n pt.id as \"thread_id!\",\n pt.latest_non_spam_message_ts as \"updated_at!\",\n matches.message_id as \"message_id!\",\n matches.contact_name as \"contact_name?\",\n matches.contact_email as \"contact_email!\",\n matches.contact_type as \"contact_type!\"\n FROM paginated_threads pt\n CROSS JOIN LATERAL (\n -- Sender matches: check contact.name, contact.email_address, and message.from_name\n SELECT DISTINCT\n m.id as message_id,\n COALESCE(m.from_name, c.name) as contact_name,\n c.email_address as contact_email,\n 'FROM'::text as contact_type\n FROM email_messages m\n JOIN email_contacts c ON c.id = m.from_contact_id\n WHERE m.thread_id = pt.id\n AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR m.from_name ILIKE $2)\n\n UNION\n\n -- Recipient matches: check contact.name, contact.email_address, and recipient.name\n SELECT DISTINCT\n m.id as message_id,\n COALESCE(mr.name, c.name) as contact_name,\n c.email_address as contact_email,\n mr.recipient_type::text as contact_type\n FROM email_messages m\n JOIN email_message_recipients mr ON mr.message_id = m.id\n JOIN email_contacts c ON c.id = mr.contact_id\n WHERE m.thread_id = pt.id\n AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR mr.name ILIKE $2)\n ) matches(message_id, contact_name, contact_email, contact_type)\n ORDER BY pt.latest_non_spam_message_ts DESC, pt.id DESC\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "thread_id!",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "updated_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 2,
- "name": "message_id!",
- "type_info": "Uuid"
- },
- {
- "ordinal": 3,
- "name": "contact_name?",
- "type_info": "Varchar"
- },
- {
- "ordinal": 4,
- "name": "contact_email!",
- "type_info": "Varchar"
- },
- {
- "ordinal": 5,
- "name": "contact_type!",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Text",
- "Int8",
- "Timestamptz",
- "Uuid"
- ]
- },
- "nullable": [
- false,
- true,
- null,
- null,
- null,
- null
- ]
- },
- "hash": "e57d9f9da7bf3d99431ca7a4d074674f46d21e94aa1dd747a6095b7b369faff1"
-}
diff --git a/rust/cloud-storage/email_contact_search/.sqlx/query-f6c4c0739b4275d5c705ea9e73f267bf7e2653c727748974be3cd7f05a36d6b5.json b/rust/cloud-storage/email_contact_search/.sqlx/query-f6c4c0739b4275d5c705ea9e73f267bf7e2653c727748974be3cd7f05a36d6b5.json
new file mode 100644
index 0000000000..2e148a0557
--- /dev/null
+++ b/rust/cloud-storage/email_contact_search/.sqlx/query-f6c4c0739b4275d5c705ea9e73f267bf7e2653c727748974be3cd7f05a36d6b5.json
@@ -0,0 +1,56 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH paginated_threads AS (\n SELECT t.id, t.latest_non_spam_message_ts\n FROM email_threads t\n WHERE t.link_id = (SELECT id FROM email_links WHERE macro_id = $1)\n AND t.latest_non_spam_message_ts IS NOT NULL\n AND (\n $4::timestamptz IS NULL\n OR (t.latest_non_spam_message_ts, t.id) < ($4, $5)\n )\n AND EXISTS (\n -- Check sender contact name\n SELECT 1\n FROM email_messages m\n JOIN email_contacts c ON c.id = m.from_contact_id\n WHERE m.thread_id = t.id AND c.name ILIKE $2\n\n UNION ALL\n\n -- Check sender email address\n SELECT 1\n FROM email_messages m\n JOIN email_contacts c ON c.id = m.from_contact_id\n WHERE m.thread_id = t.id AND c.email_address ILIKE $2\n\n UNION ALL\n\n -- Check message from_name\n SELECT 1\n FROM email_messages m\n WHERE m.thread_id = t.id AND m.from_name ILIKE $2\n\n UNION ALL\n\n -- Check recipient contact name\n SELECT 1\n FROM email_messages m\n JOIN email_message_recipients mr ON mr.message_id = m.id\n JOIN email_contacts c ON c.id = mr.contact_id\n WHERE m.thread_id = t.id AND c.name ILIKE $2\n\n UNION ALL\n\n -- Check recipient email address\n SELECT 1\n FROM email_messages m\n JOIN email_message_recipients mr ON mr.message_id = m.id\n JOIN email_contacts c ON c.id = mr.contact_id\n WHERE m.thread_id = t.id AND c.email_address ILIKE $2\n\n UNION ALL\n\n -- Check recipient name\n SELECT 1\n FROM email_messages m\n JOIN email_message_recipients mr ON mr.message_id = m.id\n WHERE m.thread_id = t.id AND mr.name ILIKE $2\n )\n ORDER BY t.latest_non_spam_message_ts DESC, t.id DESC\n LIMIT $3\n )\n SELECT\n pt.id as \"thread_id!\",\n pt.latest_non_spam_message_ts as \"updated_at!\",\n matches.message_id as \"message_id!\",\n matches.contact_name as \"contact_name?\",\n matches.contact_email as \"contact_email!\",\n matches.contact_type as \"contact_type!\"\n FROM paginated_threads pt\n CROSS JOIN LATERAL (\n -- Sender matches: check contact.name, contact.email_address, and message.from_name\n SELECT DISTINCT\n m.id as message_id,\n COALESCE(m.from_name, c.name) as contact_name,\n c.email_address as contact_email,\n 'FROM'::text as contact_type\n FROM email_messages m\n JOIN email_contacts c ON c.id = m.from_contact_id\n WHERE m.thread_id = pt.id\n AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR m.from_name ILIKE $2)\n\n UNION\n\n -- Recipient matches: check contact.name, contact.email_address, and recipient.name\n SELECT DISTINCT\n m.id as message_id,\n COALESCE(mr.name, c.name) as contact_name,\n c.email_address as contact_email,\n mr.recipient_type::text as contact_type\n FROM email_messages m\n JOIN email_message_recipients mr ON mr.message_id = m.id\n JOIN email_contacts c ON c.id = mr.contact_id\n WHERE m.thread_id = pt.id\n AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR mr.name ILIKE $2)\n ) matches(message_id, contact_name, contact_email, contact_type)\n ORDER BY pt.latest_non_spam_message_ts DESC, pt.id DESC\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "thread_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "updated_at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "message_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "contact_name?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 4,
+ "name": "contact_email!",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 5,
+ "name": "contact_type!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text",
+ "Int8",
+ "Timestamptz",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ null,
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "f6c4c0739b4275d5c705ea9e73f267bf7e2653c727748974be3cd7f05a36d6b5"
+}
diff --git a/rust/cloud-storage/email_contact_search/src/lib.rs b/rust/cloud-storage/email_contact_search/src/lib.rs
index 05505ead0a..dd3f05bf89 100644
--- a/rust/cloud-storage/email_contact_search/src/lib.rs
+++ b/rust/cloud-storage/email_contact_search/src/lib.rs
@@ -122,22 +122,52 @@ pub async fn search_email_contacts<'a>(
OR (t.latest_non_spam_message_ts, t.id) < ($4, $5)
)
AND EXISTS (
- -- Check for sender matches
+ -- Check sender contact name
SELECT 1
FROM email_messages m
JOIN email_contacts c ON c.id = m.from_contact_id
- WHERE m.thread_id = t.id
- AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR m.from_name ILIKE $2)
+ WHERE m.thread_id = t.id AND c.name ILIKE $2
UNION ALL
- -- Check for recipient matches
+ -- Check sender email address
+ SELECT 1
+ FROM email_messages m
+ JOIN email_contacts c ON c.id = m.from_contact_id
+ WHERE m.thread_id = t.id AND c.email_address ILIKE $2
+
+ UNION ALL
+
+ -- Check message from_name
+ SELECT 1
+ FROM email_messages m
+ WHERE m.thread_id = t.id AND m.from_name ILIKE $2
+
+ UNION ALL
+
+ -- Check recipient contact name
+ SELECT 1
+ FROM email_messages m
+ JOIN email_message_recipients mr ON mr.message_id = m.id
+ JOIN email_contacts c ON c.id = mr.contact_id
+ WHERE m.thread_id = t.id AND c.name ILIKE $2
+
+ UNION ALL
+
+ -- Check recipient email address
SELECT 1
FROM email_messages m
JOIN email_message_recipients mr ON mr.message_id = m.id
JOIN email_contacts c ON c.id = mr.contact_id
- WHERE m.thread_id = t.id
- AND (c.name ILIKE $2 OR c.email_address ILIKE $2 OR mr.name ILIKE $2)
+ WHERE m.thread_id = t.id AND c.email_address ILIKE $2
+
+ UNION ALL
+
+ -- Check recipient name
+ SELECT 1
+ FROM email_messages m
+ JOIN email_message_recipients mr ON mr.message_id = m.id
+ WHERE m.thread_id = t.id AND mr.name ILIKE $2
)
ORDER BY t.latest_non_spam_message_ts DESC, t.id DESC
LIMIT $3
diff --git a/rust/cloud-storage/name_search/.sqlx/query-f0754932b58807334f28d76f583f743662b7195d0c14a8e425acee712d510542.json b/rust/cloud-storage/name_search/.sqlx/query-f0754932b58807334f28d76f583f743662b7195d0c14a8e425acee712d510542.json
deleted file mode 100644
index 3a32075c4d..0000000000
--- a/rust/cloud-storage/name_search/.sqlx/query-f0754932b58807334f28d76f583f743662b7195d0c14a8e425acee712d510542.json
+++ /dev/null
@@ -1,47 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH oldest_messages AS (\n SELECT DISTINCT ON (thread_id)\n thread_id,\n subject\n FROM email_messages\n WHERE link_id IN (\n SELECT id FROM email_links WHERE macro_id = $1\n )\n AND (thread_id = ANY($2) OR $3)\n ORDER BY thread_id, internal_date_ts ASC\n )\n SELECT\n t.id as entity_id,\n om.subject as \"name!\",\n regexp_replace(\n om.subject,\n $8,\n '<macro_em>\\1</macro_em>',\n 'gi'\n ) as name_highlighted,\n t.latest_non_spam_message_ts as updated_at\n FROM email_threads t\n INNER JOIN oldest_messages om ON om.thread_id = t.id\n WHERE t.link_id IN (\n SELECT id FROM email_links WHERE macro_id = $1\n )\n AND om.subject ILIKE $4\n AND (\n $6::timestamptz IS NULL\n OR (t.latest_non_spam_message_ts, t.id) < ($6, $7)\n )\n ORDER BY t.latest_non_spam_message_ts DESC, t.id DESC\n LIMIT $5\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "entity_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "name!",
- "type_info": "Text"
- },
- {
- "ordinal": 2,
- "name": "name_highlighted",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "updated_at",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "UuidArray",
- "Bool",
- "Text",
- "Int8",
- "Timestamptz",
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- false,
- true,
- null,
- true
- ]
- },
- "hash": "f0754932b58807334f28d76f583f743662b7195d0c14a8e425acee712d510542"
-}
diff --git a/rust/cloud-storage/name_search/.sqlx/query-fa30f66ffec23b15ed938b6b3b8f8d5b57776d44c6c9528e2b686d3b6d660238.json b/rust/cloud-storage/name_search/.sqlx/query-fa30f66ffec23b15ed938b6b3b8f8d5b57776d44c6c9528e2b686d3b6d660238.json
new file mode 100644
index 0000000000..4df9398fb5
--- /dev/null
+++ b/rust/cloud-storage/name_search/.sqlx/query-fa30f66ffec23b15ed938b6b3b8f8d5b57776d44c6c9528e2b686d3b6d660238.json
@@ -0,0 +1,47 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH user_link_ids AS (\n SELECT id FROM email_links WHERE macro_id = $1\n ),\n matching_threads AS (\n SELECT DISTINCT thread_id\n FROM email_messages em\n WHERE em.link_id IN (SELECT id FROM user_link_ids)\n AND (thread_id = ANY($2) OR $3)\n AND em.subject ILIKE $4\n ),\n oldest_messages AS (\n SELECT DISTINCT ON (thread_id)\n thread_id,\n subject\n FROM email_messages\n WHERE thread_id IN (SELECT thread_id FROM matching_threads)\n ORDER BY thread_id, internal_date_ts ASC\n )\n SELECT\n t.id as entity_id,\n om.subject as \"name!\",\n regexp_replace(\n om.subject,\n $8,\n '<macro_em>\\1</macro_em>',\n 'gi'\n ) as name_highlighted,\n t.latest_non_spam_message_ts as updated_at\n FROM email_threads t\n INNER JOIN oldest_messages om ON om.thread_id = t.id\n WHERE t.link_id IN (SELECT id FROM user_link_ids)\n AND (\n $6::timestamptz IS NULL\n OR (t.latest_non_spam_message_ts, t.id) < ($6, $7)\n )\n ORDER BY t.latest_non_spam_message_ts DESC, t.id DESC\n LIMIT $5\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "entity_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "name!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name_highlighted",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "UuidArray",
+ "Bool",
+ "Text",
+ "Int8",
+ "Timestamptz",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ null,
+ true
+ ]
+ },
+ "hash": "fa30f66ffec23b15ed938b6b3b8f8d5b57776d44c6c9528e2b686d3b6d660238"
+}
diff --git a/rust/cloud-storage/name_search/src/email.rs b/rust/cloud-storage/name_search/src/email.rs
index 3c09a0bcc7..102f37bce6 100644
--- a/rust/cloud-storage/name_search/src/email.rs
+++ b/rust/cloud-storage/name_search/src/email.rs
@@ -105,15 +105,22 @@ async fn owner_search<'a>(
let rows = sqlx::query!(
r#"
- WITH oldest_messages AS (
+ WITH user_link_ids AS (
+ SELECT id FROM email_links WHERE macro_id = $1
+ ),
+ matching_threads AS (
+ SELECT DISTINCT thread_id
+ FROM email_messages em
+ WHERE em.link_id IN (SELECT id FROM user_link_ids)
+ AND (thread_id = ANY($2) OR $3)
+ AND em.subject ILIKE $4
+ ),
+ oldest_messages AS (
SELECT DISTINCT ON (thread_id)
thread_id,
subject
FROM email_messages
- WHERE link_id IN (
- SELECT id FROM email_links WHERE macro_id = $1
- )
- AND (thread_id = ANY($2) OR $3)
+ WHERE thread_id IN (SELECT thread_id FROM matching_threads)
ORDER BY thread_id, internal_date_ts ASC
)
SELECT
@@ -128,10 +135,7 @@ async fn owner_search<'a>(
t.latest_non_spam_message_ts as updated_at
FROM email_threads t
INNER JOIN oldest_messages om ON om.thread_id = t.id
- WHERE t.link_id IN (
- SELECT id FROM email_links WHERE macro_id = $1
- )
- AND om.subject ILIKE $4
+ WHERE t.link_id IN (SELECT id FROM user_link_ids)
AND (
$6::timestamptz IS NULL
OR (t.latest_non_spam_message_ts, t.id) < ($6, $7)
diff --git a/rust/cloud-storage/name_search/src/email/test.rs b/rust/cloud-storage/name_search/src/email/test.rs
index 67f9bf9f68..0b66a98cfe 100644
--- a/rust/cloud-storage/name_search/src/email/test.rs
+++ b/rust/cloud-storage/name_search/src/email/test.rs
@@ -404,7 +404,7 @@ async fn test_search_email_subjects_searches_oldest_message(
// 2. Middle (2024-02-02): "Re: Re: Monthly Invoice - December"
// 3. Newest (2024-12-02): "Re: Re: Re: Payment Processed"
- // Searching for "Monthly Invoice" should find the thread because the oldest message matches
+ // Searching for "Monthly Invoice" should find the thread because a message matches
let response = search_email_subjects(
&pool,
&user_id,
@@ -422,7 +422,7 @@ async fn test_search_email_subjects_searches_oldest_message(
.eq("22222222-2222-2222-2222-222222222222")
}));
- // Searching for "Payment Processed" should NOT find it because oldest message doesn't match
+ // Searching for "Payment Processed" should also find it because we search all messages in the thread
let response = search_email_subjects(
&pool,
&user_id,
@@ -434,7 +434,7 @@ async fn test_search_email_subjects_searches_oldest_message(
)
.await?;
- assert!(!response.items.iter().any(|r| {
+ assert!(response.items.iter().any(|r| {
r.entity_id
.to_string()
.eq("22222222-2222-2222-2222-222222222222")