AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(highlight): tag whole words and every query term in a fragment (#1948) (#1949)
xerj-org552145c49687
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rsindex 775defc4c..1391f358e 100644--- a/engine/crates/xerj-engine/src/index.rs+++ b/engine/crates/xerj-engine/src/index.rs@@ -36464,7 +36464,9 @@ fn apply_highlight(hits: Vec<Hit>, hl: &HighlightRequest, query: &QueryNode) ->// so matches can be translated back to original-text char// boundaries (fixes the multibyte highlight SIGABRT).let (text_lower, lower_map) = build_lower_offset_map(&text);- let has_any = query_terms.iter().any(|t| text_lower.contains(t.as_str()));+ let has_any = query_terms+ .iter()+ .any(|t| next_term_match(&text_lower, t, 0).is_some());if !has_any {continue;}@@ -36650,8 +36652,7 @@ fn collect_highlight_terms(query: &QueryNode, out: &mut Vec<String>) {match query {QueryNode::Match { query, .. }| QueryNode::MatchPhrase { query, .. }- | QueryNode::MultiMatch { query, .. }- | QueryNode::QueryString { query, .. } => {+ | QueryNode::MultiMatch { query, .. } => {for tok in query.split_whitespace() {let tok = tok.trim_matches(|c: char| !c.is_alphanumeric())@@ -36661,6 +36662,22 @@ fn collect_highlight_terms(query: &QueryNode, out: &mut Vec<String>) {}}}+ QueryNode::QueryString { query, .. } => {+ for tok in query.split_whitespace() {+ // `wild*` keeps its star so it highlights `wildcard` as a+ // prefix match; every other term matches whole words only.+ let prefix = tok+ .trim_start_matches(|c: char| !c.is_alphanumeric())+ .trim_end_matches(|c: char| !c.is_alphanumeric() && c != '*')+ .ends_with('*');+ let tok = tok+ .trim_matches(|c: char| !c.is_alphanumeric())+ .to_lowercase();+ if !tok.is_empty() {+ out.push(if prefix { format!("{tok}*") } else { tok });+ }+ }+ }QueryNode::Term { value, .. } => {if let Some(s) = value.as_str() {out.push(s.to_lowercase());@@ -36685,6 +36702,112 @@ fn collect_highlight_terms(query: &QueryNode, out: &mut Vec<String>) {}}+/// A character that continues a word, for highlight boundaries.+fn is_word_char(c: char) -> bool {+ c.is_alphanumeric() || c == '_'+}++/// The next occurrence of highlight `term` in `text_lower` at or after byte+/// `from` that stands as a whole word: the characters on either side are not+/// word characters. A term ending in `*` (a `query_string` prefix) needs a+/// boundary before it only and extends to the end of the word it starts.+///+/// Plain substring search (#1948) highlighted `or` inside `for`, `no` inside `not`+/// and `OR` inside `SECTOR`, and a phrase's short words then claimed the+/// fragment away from the phrase itself.+fn next_term_match(text_lower: &str, term: &str, from: usize) -> Option<(usize, usize)> {+ let (needle, prefix) = match term.strip_suffix('*') {+ Some(stem) => (stem, true),+ None => (term, false),+ };+ if needle.is_empty() || from > text_lower.len() {+ return None;+ }+ let mut start = from;+ while let Some(pos) = text_lower[start..].find(needle) {+ let abs = start + pos;+ let mut end = abs + needle.len();+ let before_ok = !text_lower[..abs]+ .chars()+ .next_back()+ .is_some_and(is_word_char);+ if prefix {+ end += text_lower[end..]+ .char_indices()+ .find(|&(_, c)| !is_word_char(c))+ .map(|(i, _)| i)+ .unwrap_or(text_lower.len() - end);+ }+ let after_ok = prefix || !text_lower[end..].chars().next().is_some_and(is_word_char);+ if before_ok && after_ok {+ return Some((abs, end));+ }+ // Step one character, not one byte, past this candidate.+ start = abs + text_lower[abs..].chars().next().map_or(1, char::len_utf8);+ }+ None+}++/// Wrap every whole-word occurrence of every term inside `text[win_start..+/// win_end]` with `pre`/`post`. Overlapping spans merge into one tag pair.+#[allow(clippy::too_many_arguments)]+fn tag_terms_in_window(+ text: &str,+ text_lower: &str,+ lower_map: &[(usize, usize, usize)],+ terms: &[String],+ win_start: usize,+ win_end: usize,+ pre: &str,+ post: &str,+) -> String {+ let orig_len = text.len();+ // Start the scan at the window, not at the top of a large document.+ let window_lower_start = lower_map+ .get(lower_map.partition_point(|e| e.1 < win_start))+ .map_or(text_lower.len(), |e| e.0);+ let mut spans: Vec<(usize, usize)> = Vec::new();+ for term in terms {+ let mut from = window_lower_start;+ while let Some((ls, le)) = next_term_match(text_lower, term, from) {+ let (oa, ob) = lower_span_to_orig(lower_map, orig_len, ls, le);+ if oa >= win_end {+ break;+ }+ if oa >= win_start && ob <= win_end && ob > oa {+ spans.push((oa, ob));+ }+ from = le.max(ls + 1);+ }+ }+ spans.sort_unstable();+ let mut out = String::with_capacity(win_end - win_start + spans.len() * 9);+ let mut cursor = win_start;+ let mut open: Option<(usize, usize)> = None;+ let flush = |out: &mut String, cursor: &mut usize, (s, e): (usize, usize)| {+ out.push_str(&text[*cursor..s]);+ out.push_str(pre);+ out.push_str(&text[s..e]);+ out.push_str(post);+ *cursor = e;+ };+ for (s, e) in spans {+ match open {+ Some((os, oe)) if s <= oe => open = Some((os, oe.max(e))),+ Some(span) => {+ flush(&mut out, &mut cursor, span);+ open = Some((s, e));+ }+ None => open = Some((s, e)),+ }+ }+ if let Some(span) = open {+ flush(&mut out, &mut cursor, span);+ }+ out.push_str(&text[cursor..win_end]);+ out+}+/// Build highlighted fragments for a single field value.////// `text_lower` must be `text.to_lowercase()` (pre-computed by the caller to@@ -36764,15 +36887,14 @@ fn highlight_full_text(continue;}let mut start = 0;- while let Some(pos) = text_lower[start..].find(term.as_str()) {- let abs = start + pos;+ while let Some((ls, le)) = next_term_match(text_lower, term, start) {// Translate the lowercased-text match offsets back to original// text char boundaries before recording the span.- let (oa, ob) = lower_span_to_orig(lower_map, orig_len, abs, abs + term.len());+ let (oa, ob) = lower_span_to_orig(lower_map, orig_len, ls, le);if ob > oa {spans.push((oa, ob));}- start = abs + term.len();+ start = le.max(ls + 1);}}spans.sort_by_key(|&(s, _)| s);@@ -36838,14 +36960,13 @@ fn build_highlight_fragments(// highlighting and downloaded whole documents instead.let mut per_term = 0usize;let per_term_cap = (num_frags * 4).max(4);- while let Some(pos) = text_lower[start..].find(term.as_str()) {- let abs = start + pos;- let (oa, ob) = lower_span_to_orig(lower_map, orig_len, abs, abs + term.len());+ while let Some((ls, le)) = next_term_match(text_lower, term, start) {+ let (oa, ob) = lower_span_to_orig(lower_map, orig_len, ls, le);if ob > oa {match_positions.push((oa, ob));- lower_positions.push((abs, abs + term.len()));+ lower_positions.push((ls, le));}- start = abs + term.len();+ start = le.max(ls + 1);per_term += 1;if per_term >= per_term_cap {break; // enough candidates FOR THIS TERM; keep scanning others@@ -36883,7 +37004,10 @@ fn build_highlight_fragments(.find(|&i| text_lower.is_char_boundary(i)).unwrap_or(lower_len);let window = &text_lower[lo..hi];- let distinct = terms.iter().filter(|t| window.contains(t.as_str())).count();+ let distinct = terms+ .iter()+ .filter(|t| next_term_match(window, t, 0).is_some())+ .count();(distinct, orig)}).collect();@@ -36919,7 +37043,7 @@ fn build_highlight_fragments(let mut fragments: Vec<String> = Vec::new();let mut last_win_end: usize = 0;- for (byte_start, byte_end) in &match_positions {+ for (byte_start, _) in &match_positions {if fragments.len() >= num_frags {break;}@@ -36941,28 +37065,16 @@ fn build_highlight_fragments(}last_win_end = win_end;- // Clamp match offsets to the window.- let match_start = (*byte_start).max(win_start).min(win_end);- let match_end = (*byte_end).max(win_start).min(win_end);-- // Build the fragment: prefix + pre-tag + matched span + post-tag + suffix.+ // Build the fragment: every query term inside the window is tagged,+ // not only the match the window is centred on, so a phrase reads as+ // its words tagged in place (original casing kept).let mut frag = String::with_capacity(frag_size + pre.len() + post.len() + 6);if win_start > 0 {frag.push_str("...");}- // Text before the match (within the window).- if match_start > win_start {- // Use the original text casing for the context surrounding the match.- frag.push_str(&text[win_start..match_start]);- }- frag.push_str(pre);- // The matched span (from original text, not lowercased).- frag.push_str(&text[match_start..match_end]);- frag.push_str(post);- // Text after the match (within the window).- if match_end < win_end {- frag.push_str(&text[match_end..win_end]);- }+ frag.push_str(&tag_terms_in_window(+ text, text_lower, lower_map, terms, win_start, win_end, pre, post,+ ));if win_end < total_bytes {frag.push_str("...");}@@ -56622,6 +56734,45 @@ mod highlight_multibyte_tests {);}+ /// #1948: a phrase query highlighted `or` inside `for` and `no` inside+ /// `not`, and only the one match each fragment was centred on, so the+ /// phrase itself came back untagged.+ #[test]+ fn highlight_tags_whole_words_and_every_term_in_the_fragment() {+ let text = "Pricing is per-node for Professional, custom for Enterprise. \+ No per-doc, per-vector, or per-query fees. Binary delivery \+ does not require a sector license.";+ let (lower, map) = build_lower_offset_map(text);+ let mut terms: Vec<String> = ["no", "per-doc", "per-vector", "or", "per-query", "fees"]+ .iter()+ .map(|t| t.to_string())+ .collect();+ terms.sort();+ let frags = build_highlight_fragments(text, &lower, &map, &terms, "<em>", "</em>", 150, 1);+ assert_eq!(frags.len(), 1, "{frags:?}");+ let f = &frags[0];+ assert!(+ f.contains("<em>No</em> <em>per-doc</em>, <em>per-vector</em>, <em>or</em> <em>per-query</em> <em>fees</em>"),+ "every phrase word is tagged in place: {f}"+ );+ assert!(!f.contains("f<em>or</em>"), "no match inside `for`: {f}");+ assert!(!f.contains("<em>no</em>t"), "no match inside `not`: {f}");++ let full = highlight_full_text(text, &lower, &map, &terms, "<em>", "</em>");+ assert!(+ !full.contains("f<em>or</em>") && !full.contains("<em>no</em>t"),+ "{full}"+ );+ assert!(!full.contains("sect<em>or</em>"), "{full}");++ // A `query_string` prefix term still reaches into the word.+ let (lower, map) = build_lower_offset_map("a wildcard and wild cats");+ let prefix = vec!["wild*".to_string()];+ let full = highlight_full_text("a wildcard and wild cats", &lower, &map, &prefix, "[", "]");+ assert_eq!(full, "a [wildcard] and [wild] cats");+ assert_eq!(next_term_match("rewild", "wild*", 0), None);+ }+#[test]fn fragment_highlight_after_multibyte_does_not_panic() {let text = "İstanbul cafe";