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.

fix(search): precise email-address highlighting (#2628)

gbirman6d425737c022
diff --git a/rust/cloud-storage/opensearch_client/src/search/emails.rs b/rust/cloud-storage/opensearch_client/src/search/emails.rs
index 4e91c43fdf..03de3f2f75 100644
--- a/rust/cloud-storage/opensearch_client/src/search/emails.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/emails.rs
@@ -36,13 +36,19 @@ const EMAIL_SIMPLE_QUERY_FIELDS: &[&str] = &[
/// Single-word terms become `(term | term@*)` so they match both text fields
/// and keyword email-address fields. Multi-word terms (containing spaces) skip
/// the `@*` pattern since email addresses never contain spaces.
+/// Email-like terms (containing `@`) are wrapped in quotes to force phrase
+/// matching on analyzed text fields — otherwise the standard analyzer tokenizes
+/// `[email protected]` into `[hutch, macro, com]`, causing `macro.com` to be
+/// highlighted inside unrelated addresses like `[email protected]`.
/// The email pattern is lowercased because email addresses are case-insensitive.
/// Terms are ANDed together with `+`.
fn build_simple_query_string(terms: &[String]) -> String {
terms
.iter()
.map(|term| {
- if term.contains(' ') {
+ if term.contains('@') {
+ format!("\"{}\"", term)
+ } else if term.contains(' ') {
format!("({})", term)
} else {
format!("({} | {}@*)", term, term.to_lowercase())
diff --git a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs b/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
index f451ba68e1..becea69c1a 100644
--- a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
@@ -31,6 +31,18 @@ fn test_build_simple_query_string_mixed_single_and_multi_word() {
assert_eq!(result, "(Teo | teo@*) + (hello world)");
}
+#[test]
+fn test_build_simple_query_string_email_term_uses_phrase() {
+ let result = build_simple_query_string(&["[email protected]".to_string()]);
+ assert_eq!(result, "\"[email protected]\"");
+}
+
+#[test]
+fn test_build_simple_query_string_email_term_mixed_with_word() {
+ let result = build_simple_query_string(&["[email protected]".to_string(), "review".to_string()]);
+ assert_eq!(result, "\"[email protected]\" + (review | review@*)");
+}
+
#[test]
fn test_email_search_args_build_injects_simple_query_string() -> anyhow::Result<()> {
let builder: EmailQueryBuilder = EmailSearchArgs {
diff --git a/rust/cloud-storage/opensearch_client/src/search/model/mod.rs b/rust/cloud-storage/opensearch_client/src/search/model/mod.rs
index ac06ea3435..8b3877e2ac 100644
--- a/rust/cloud-storage/opensearch_client/src/search/model/mod.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/model/mod.rs
@@ -44,6 +44,16 @@ const OPEN_TAG: &str = "<macro_em>";
const CLOSE_TAG: &str = "</macro_em>";
const CHARS_BEFORE_HIGHLIGHT: usize = 200;
+/// Merges adjacent highlight spans separated only by `@` into a single span.
+/// The `plain` highlighter wraps each matched token individually, so a hit on
+/// `[email protected]` comes back as
+/// `<macro_em>hutch</macro_em>@<macro_em>macro.com</macro_em>`. Merging across
+/// `@` renders it as `<macro_em>[email protected]</macro_em>`, which reads as
+/// one email address.
+pub(crate) fn merge_highlights_across_at(fragment: &str) -> String {
+ fragment.replace(&format!("{CLOSE_TAG}@{OPEN_TAG}"), "@")
+}
+
fn normalize_highlight_fragment(fragment: &str) -> String {
let stripped: String = fragment
.chars()
@@ -73,8 +83,8 @@ fn normalize_highlight_fragment(fragment: &str) -> String {
})
.collect();
- let trimmed = normalized.trim();
- window_around_highlight(trimmed, MAX_VISIBLE_FRAGMENT_CHARS)
+ let merged = merge_highlights_across_at(normalized.trim());
+ window_around_highlight(&merged, MAX_VISIBLE_FRAGMENT_CHARS)
}
/// Returns a window of `max_chars` visible characters around the first
diff --git a/rust/cloud-storage/opensearch_client/src/search/model/test.rs b/rust/cloud-storage/opensearch_client/src/search/model/test.rs
index fb664f1912..282da3af0e 100644
--- a/rust/cloud-storage/opensearch_client/src/search/model/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/model/test.rs
@@ -130,6 +130,44 @@ fn find_tag_aware_byte_offset_handles_utf8() {
assert_eq!(&s[offset..], "é");
}
+#[test]
+fn merge_highlights_across_at_joins_email_tokens() {
+ let input = "sent to <macro_em>hutch</macro_em>@<macro_em>macro.com</macro_em> today";
+ let result = merge_highlights_across_at(input);
+ assert_eq!(result, "sent to <macro_em>[email protected]</macro_em> today");
+}
+
+#[test]
+fn merge_highlights_across_at_leaves_standalone_highlights() {
+ let input = "say <macro_em>hello</macro_em> world";
+ let result = merge_highlights_across_at(input);
+ assert_eq!(result, "say <macro_em>hello</macro_em> world");
+}
+
+#[test]
+fn merge_highlights_across_at_leaves_plain_at_symbol() {
+ let input = "contact us @ <macro_em>macro</macro_em>";
+ let result = merge_highlights_across_at(input);
+ assert_eq!(result, "contact us @ <macro_em>macro</macro_em>");
+}
+
+#[test]
+fn merge_highlights_across_at_multiple_pairs() {
+ let input = "<macro_em>a</macro_em>@<macro_em>b.com</macro_em> and <macro_em>c</macro_em>@<macro_em>d.org</macro_em>";
+ let result = merge_highlights_across_at(input);
+ assert_eq!(
+ result,
+ "<macro_em>[email protected]</macro_em> and <macro_em>[email protected]</macro_em>"
+ );
+}
+
+#[test]
+fn normalize_merges_email_highlight_tokens() {
+ let input = "hi <macro_em>hutch</macro_em>@<macro_em>macro.com</macro_em> thanks";
+ let result = normalize_highlight_fragment(input);
+ assert_eq!(result, "hi <macro_em>[email protected]</macro_em> thanks");
+}
+
#[test]
fn window_real_world_email_fragment() {
let input = "Hi Gabriel, We often need to copy email to other tools — our notes, todo list, or Slack. In Gmail and Outlook, this is error prone and tedious. In Superhuman Mail, it is accurate and instantaneous: Hit Cmd+C (Mac) or Ctrl+C (Windows) to copy the current message. Hit it again to copy the conversation. Hit Cmd+V (Mac) or Ctrl+V (Windows) to paste wherever you want. You can even paste straight into Superhuman Mail! No selecting, no dragging, no fuss. I'd love to hear what you think: please reply and say <macro_em>hello</macro_em> :) Speak soon, Rahul";