Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(search): make email address domains and local parts searchable (#4898)
gbirman0eb783868f6b
diff --git a/crates/opensearch_client/src/search/emails.rs b/crates/opensearch_client/src/search/emails.rsindex c8ec2ad070..32c885d36a 100644--- a/crates/opensearch_client/src/search/emails.rs+++ b/crates/opensearch_client/src/search/emails.rs@@ -18,10 +18,26 @@ impl SearchQueryConfig for EmailSearchConfig {const ENTITY_INDEX: OpenSearchEntityType = OpenSearchEntityType::Emails;}-/// Keyword-indexed email-address fields. Matched explicitly: a bare word-/// only matches an address with that exact local-part (`alice` → `alice@*`),-/// not addresses that merely start with the characters.-const EMAIL_KEYWORD_FIELDS: &[&str] = &["sender", "reply_to", "recipients", "cc", "bcc"];+/// Email-address fields matched explicitly, never as a character prefix.+/// The raw keyword-indexed addresses plus their `.parts` sub-fields, which+/// the `email_parts` analyzer splits into positioned segment tokens+/// (`[email protected]` indexes `jane`, `doe`, `mail`, `foo`, `com`).+/// A bare word matches only a whole segment, so `ali` won't match+/// `[email protected]` and `immipart` won't match `immipartner.com`.+/// Dotted terms like `foo.com` are quoted into phrases and match any+/// in-order adjacent run of segments.+const EMAIL_KEYWORD_FIELDS: &[&str] = &[+ "sender",+ "reply_to",+ "recipients",+ "cc",+ "bcc",+ "sender.parts",+ "reply_to.parts",+ "recipients.parts",+ "cc.parts",+ "bcc.parts",+];/// Text-analyzed fields. Matched as a prefix so `scri` matches `script`.const EMAIL_TEXT_FIELDS: &[&str] = &[@@ -33,25 +49,29 @@ const EMAIL_TEXT_FIELDS: &[&str] = &["bcc_names",];-/// Builds the simple_query_string over the keyword email-address fields.-/// Single-word terms become `(term | term@*)` — an exact token match OR a-/// local-part match on the address. This deliberately does NOT use a trailing-/// `*` on the bare term, so partial local-parts don't leak across addresses-/// (e.g. `ali` won't match `[email protected]` via this group).-/// Email-like terms (containing `@`) and multi-word terms (quoted phrases-/// from `split_search_terms`) are wrapped in quotes to force phrase matching.-/// Without the quotes, `default_operator: "AND"` would turn `(reply test)`-/// into `reply AND test` and match each token independently anywhere in the-/// field instead of as an adjacent phrase. The standard analyzer also-/// tokenizes `[email protected]` into `[alice, example, com]`, so phrase-/// matching is what keeps the address from matching across unrelated tokens.+/// Builds the simple_query_string over the email-address fields.+/// Single-word terms become `(term | term@*)` — an exact segment-token match+/// on the `.parts` sub-fields OR an exact-local-part match on the raw+/// address. The `term@*` pattern is redundant with `.parts` once the index+/// carries the sub-fields but keeps the query compatible with an index that+/// predates them. This deliberately does NOT use a trailing `*` on the bare+/// term, so partial segments don't leak across addresses (e.g. `ali` won't+/// match `[email protected]` via this group).+/// Terms containing `@`, `.`, or spaces (quoted phrases from+/// `split_search_terms`) are wrapped in quotes to force phrase matching. On+/// the `.parts` sub-fields the `email_parts` analyzer splits such terms into+/// segment tokens, so the phrase requires an in-order adjacent run —+/// `mail.foo.com`, `foo.com`, and full addresses all match `[email protected]`,+/// but reordered or gapped segments don't. Without the quotes,+/// `default_operator: "AND"` would match each segment independently anywhere+/// in the field./// The email pattern is lowercased because email addresses are case-insensitive./// Terms are ANDed together with `+`.fn build_keyword_query_string(terms: &[String]) -> String {terms.iter().map(|term| {- if term.contains('@') || term.contains(' ') {+ if term.contains('@') || term.contains('.') || term.contains(' ') {format!("\"{}\"", term)} else {format!("({} | {}@*)", term, term.to_lowercase())diff --git a/crates/opensearch_client/src/search/emails/test.rs b/crates/opensearch_client/src/search/emails/test.rsindex 8b7ac3a274..91649d0698 100644--- a/crates/opensearch_client/src/search/emails/test.rs+++ b/crates/opensearch_client/src/search/emails/test.rs@@ -34,6 +34,12 @@ fn test_build_keyword_query_string_mixed_single_and_multi_word() {assert_eq!(result, "(Teo | teo@*) + \"hello world\"");}+#[test]+fn test_build_keyword_query_string_dotted_term_uses_phrase() {+ let result = build_keyword_query_string(&["example.com".to_string()]);+ assert_eq!(result, "\"example.com\"");+}+#[test]fn test_build_keyword_query_string_email_term_uses_phrase() {let result = build_keyword_query_string(&["[email protected]".to_string()]);@@ -187,6 +193,11 @@ fn test_email_search_args_build_injects_simple_query_string() -> anyhow::Result<assert!(keyword_fields.contains(&serde_json::json!("recipients")));assert!(keyword_fields.contains(&serde_json::json!("cc")));assert!(keyword_fields.contains(&serde_json::json!("bcc")));+ assert!(keyword_fields.contains(&serde_json::json!("sender.parts")));+ assert!(keyword_fields.contains(&serde_json::json!("reply_to.parts")));+ assert!(keyword_fields.contains(&serde_json::json!("recipients.parts")));+ assert!(keyword_fields.contains(&serde_json::json!("cc.parts")));+ assert!(keyword_fields.contains(&serde_json::json!("bcc.parts")));assert!(!keyword_fields.contains(&serde_json::json!("subject")));let text_sqs = should@@ -206,6 +217,7 @@ fn test_email_search_args_build_injects_simple_query_string() -> anyhow::Result<assert!(text_fields.contains(&serde_json::json!("sender_name")));assert!(text_fields.contains(&serde_json::json!("recipient_names")));assert!(!text_fields.contains(&serde_json::json!("sender")));+ assert!(!text_fields.contains(&serde_json::json!("sender.parts")));Ok(())}@@ -282,7 +294,7 @@ fn test_build_bool_query() -> anyhow::Result<()> {{"simple_query_string": {"default_operator": "AND",- "fields": ["sender", "reply_to", "recipients", "cc", "bcc"],+ "fields": ["sender", "reply_to", "recipients", "cc", "bcc", "sender.parts", "reply_to.parts", "recipients.parts", "cc.parts", "bcc.parts"],"query": "(test | test@*)"}},diff --git a/crates/opensearch_client/src/search/unified/test.rs b/crates/opensearch_client/src/search/unified/test.rsindex 67bc2e865b..671e650689 100644--- a/crates/opensearch_client/src/search/unified/test.rs+++ b/crates/opensearch_client/src/search/unified/test.rs@@ -674,7 +674,7 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {{"simple_query_string": {"default_operator": "AND",- "fields": ["sender", "reply_to", "recipients", "cc", "bcc"],+ "fields": ["sender", "reply_to", "recipients", "cc", "bcc", "sender.parts", "reply_to.parts", "recipients.parts", "cc.parts", "bcc.parts"],"query": "(test | test@*)"}},diff --git a/infra/stacks/opensearch/helpers/constants.ts b/infra/stacks/opensearch/helpers/constants.tsindex d3ca7c9cd1..deb8d9490b 100644--- a/infra/stacks/opensearch/helpers/constants.ts+++ b/infra/stacks/opensearch/helpers/constants.ts@@ -13,7 +13,7 @@ export const PROJECTS_ALIAS = 'projects';export const CHANNELS_INDEX = 'channels_v2';export const CHATS_INDEX = 'chats_v2';export const DOCUMENTS_INDEX = 'documents_v2';-export const EMAILS_INDEX = 'emails_v1';+export const EMAILS_INDEX = 'emails_v2';export const CALL_RECORDS_INDEX = 'call_records_v2';export const PROJECTS_INDEX = 'projects_v1';diff --git a/infra/stacks/opensearch/helpers/scripts/create_indices.ts b/infra/stacks/opensearch/helpers/scripts/create_indices.tsindex 8e3e1e6a60..30bb304f70 100644--- a/infra/stacks/opensearch/helpers/scripts/create_indices.ts+++ b/infra/stacks/opensearch/helpers/scripts/create_indices.ts@@ -424,10 +424,44 @@ const PROJECTS_BODY = {},};+// Splits addresses into positioned segment tokens for the `.parts`+// sub-fields: `[email protected]` -> [jane, doe, mail, foo, com].+// Same-width space replacements keep highlight offsets aligned, and the+// positions make dotted query phrases require in-order adjacent segments.+const EMAIL_PARTS_ANALYSIS = {+ char_filter: {+ email_separators: {+ type: 'mapping',+ mappings: ['@ => \\u0020', '. => \\u0020', '+ => \\u0020'],+ },+ },+ analyzer: {+ email_parts: {+ type: 'custom',+ char_filter: ['email_separators'],+ tokenizer: 'standard',+ filter: ['lowercase'],+ },+ },+};++const EMAIL_ADDRESS_FIELD = {+ type: 'keyword',+ index: true,+ doc_values: true,+ fields: {+ parts: {+ type: 'text',+ analyzer: 'email_parts',+ },+ },+};+const EMAIL_BODY = {settings: {...SHARD_SETTINGS,refresh_interval: '2s',+ analysis: EMAIL_PARTS_ANALYSIS,},mappings: {dynamic: 'false',@@ -440,31 +474,11 @@ const EMAIL_BODY = {index: true,doc_values: true,},- sender: {- type: 'keyword',- index: true,- doc_values: true,- },- reply_to: {- type: 'keyword',- index: true,- doc_values: true,- },- recipients: {- type: 'keyword',- index: true,- doc_values: true,- },- cc: {- type: 'keyword',- index: true,- doc_values: true,- },- bcc: {- type: 'keyword',- index: true,- doc_values: true,- },+ sender: EMAIL_ADDRESS_FIELD,+ reply_to: EMAIL_ADDRESS_FIELD,+ recipients: EMAIL_ADDRESS_FIELD,+ cc: EMAIL_ADDRESS_FIELD,+ bcc: EMAIL_ADDRESS_FIELD,sender_name: {type: 'text',analyzer: 'standard',