From 48468f5ae5430cc6219452f360bebcf245aade0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Poyraz=20K=C3=BC=C3=A7=C3=BCkarslan?= <83272398+PoyrazK@users.noreply.github.com> Date: Sat, 2 May 2026 21:42:26 +0300 Subject: [PATCH 1/2] fix(index): extract magic numbers 50 and 30 to named constants Replace hardcoded context window sizes in extract_highlight() with named constants HIGHLIGHT_PRE_CONTEXT_CHARS and HIGHLIGHT_POST_CONTEXT_CHARS to improve readability and avoid magic numbers. --- rust/crates/cloudsearch-index/src/lib.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rust/crates/cloudsearch-index/src/lib.rs b/rust/crates/cloudsearch-index/src/lib.rs index 6a9910e..6b2edf1 100644 --- a/rust/crates/cloudsearch-index/src/lib.rs +++ b/rust/crates/cloudsearch-index/src/lib.rs @@ -1820,6 +1820,11 @@ fn hash_doc_id(id: &str) -> u64 { hasher.finish() } +/// Number of characters to include before the matched term in a highlight fragment. +const HIGHLIGHT_PRE_CONTEXT_CHARS: usize = 50; +/// Number of characters to include after the matched term in a highlight fragment. +const HIGHLIGHT_POST_CONTEXT_CHARS: usize = 30; + /// Extract highlight fragments from a document's text fields using position data. #[allow(clippy::cast_possible_truncation)] fn extract_highlight( @@ -1856,13 +1861,13 @@ fn extract_highlight( if pos >= text.len() { continue; } - // Extract window around match: 50 chars before, matched term, 30 after - let pre_start = pos.saturating_sub(50); + // Extract window around match: pre chars before, matched term, post chars after + let pre_start = pos.saturating_sub(HIGHLIGHT_PRE_CONTEXT_CHARS); let pre = &text[pre_start..pos]; let term_match = &text[pos..pos.saturating_add(term.len()).min(text.len())]; let post_end = - pos.saturating_add(term.len() + 30).min(text.len()); + pos.saturating_add(term.len() + HIGHLIGHT_POST_CONTEXT_CHARS).min(text.len()); let post = if pos.saturating_add(term.len()) < post_end { &text[pos.saturating_add(term.len())..post_end] } else { From 6488dace67a269e641c9de135bc34e07846ab85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Poyraz=20K=C3=BC=C3=A7=C3=BCkarslan?= <83272398+PoyrazK@users.noreply.github.com> Date: Sat, 2 May 2026 22:06:20 +0300 Subject: [PATCH 2/2] style: apply rustfmt --- rust/crates/cloudsearch-index/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/crates/cloudsearch-index/src/lib.rs b/rust/crates/cloudsearch-index/src/lib.rs index 6b2edf1..db2c4d4 100644 --- a/rust/crates/cloudsearch-index/src/lib.rs +++ b/rust/crates/cloudsearch-index/src/lib.rs @@ -1866,8 +1866,9 @@ fn extract_highlight( let pre = &text[pre_start..pos]; let term_match = &text[pos..pos.saturating_add(term.len()).min(text.len())]; - let post_end = - pos.saturating_add(term.len() + HIGHLIGHT_POST_CONTEXT_CHARS).min(text.len()); + let post_end = pos + .saturating_add(term.len() + HIGHLIGHT_POST_CONTEXT_CHARS) + .min(text.len()); let post = if pos.saturating_add(term.len()) < post_end { &text[pos.saturating_add(term.len())..post_end] } else {