Fix panic when phrase contains only one stop word and nothing else

This commit is contained in:
Samyak S Sarnayak 2022-10-13 23:54:49 +05:30
parent bb9ce3c5c5
commit 2aa11afb87
No known key found for this signature in database
GPG Key ID: 365873F2F0C6153B
2 changed files with 14 additions and 0 deletions

View File

@ -422,6 +422,11 @@ pub fn resolve_phrase(ctx: &dyn Context, phrase: &[Option<String>]) -> Result<Ro
let mut candidates = RoaringBitmap::new(); let mut candidates = RoaringBitmap::new();
let mut first_iter = true; let mut first_iter = true;
let winsize = phrase.len().min(3); let winsize = phrase.len().min(3);
if phrase.is_empty() {
return Ok(candidates);
}
for win in phrase.windows(winsize) { for win in phrase.windows(winsize) {
// Get all the documents with the matching distance for each word pairs. // Get all the documents with the matching distance for each word pairs.
let mut bitmaps = Vec::with_capacity(winsize.pow(2)); let mut bitmaps = Vec::with_capacity(winsize.pow(2));

View File

@ -32,4 +32,13 @@ fn test_phrase_search_with_stop_words() {
let result = search.execute().unwrap(); let result = search.execute().unwrap();
// 1 document should match // 1 document should match
assert_eq!(result.documents_ids.len(), 1); assert_eq!(result.documents_ids.len(), 1);
// test for a single stop word only, no other search terms
let mut search = Search::new(&txn, &index);
search.query("\"the\"");
search.limit(10);
search.authorize_typos(false);
search.terms_matching_strategy(TermsMatchingStrategy::All);
let result = search.execute().unwrap();
assert_eq!(result.documents_ids.len(), 0);
} }