Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: constrain number of tokens we scan/need before generating a preview #342

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions crates/spyglass/src/api/handler/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ use std::collections::HashSet;
use std::time::SystemTime;
use tracing::instrument;

/// Max number of tokens we'll look at for matches before stopping.
const MAX_HIGHLIGHT_SCAN: usize = 10_000;
/// Max number of matches we need to generate a decent preview.
const MAX_HIGHLIGHT_MATCHES: usize = 5;

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct WordRange {
start: usize,
Expand Down Expand Up @@ -66,19 +71,28 @@ fn generate_highlight_preview(index: &Searcher, query: &str, content: &str) -> S
.map(|s| s.to_string())
.collect::<Vec<_>>();

let matched_indices = content
.split_whitespace()
.enumerate()
.filter(|(_, w)| {
let normalized = tokenizer
.token_stream(w)
.next()
.map(|t| t.text.clone())
.unwrap_or_else(|| w.to_string());
terms.contains(&normalized)
})
.map(|(idx, _)| idx)
.collect::<Vec<_>>();
let mut matched_indices = Vec::new();
let mut num_tokens_scanned = 0;
for (idx, w) in content.split_whitespace().enumerate() {
num_tokens_scanned += 1;

let normalized = tokenizer
.token_stream(w)
.next()
.map(|t| t.text.clone())
.unwrap_or_else(|| w.to_string());
if terms.contains(&normalized) {
matched_indices.push(idx);
}

if matched_indices.len() > MAX_HIGHLIGHT_MATCHES {
break;
}

if num_tokens_scanned > MAX_HIGHLIGHT_SCAN {
break;
}
}

// Create word ranges from the indices
let mut ranges: Vec<WordRange> = Vec::new();
Expand Down