Skip to content

Commit

Permalink
get_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Nov 26, 2023
1 parent 6213edc commit d9498f6
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ impl Index {

(vocab, postings)
}

pub fn get_docs(&self, term: &str) -> Option<&Vec<usize>> {
self.vocab.get(term).and_then(|i| self.postings.get(i))
}
}

#[cfg(test)]
Expand All @@ -57,13 +61,10 @@ mod tests {
let i = Index::new("test_data/index");

assert!(i.vocab.get("apple").is_some());
assert!(i.vocab.get("park").is_some());

let apple_idx = i.vocab.get("apple").unwrap();
let binary_idx = i.vocab.get("binary").unwrap();
assert!(i.vocab.get("binary").is_some());

let apple_postings = i.postings.get(apple_idx).unwrap();
let binary_postings = i.postings.get(binary_idx).unwrap();
let apple_postings = i.get_docs("apple").unwrap();
let binary_postings = i.get_docs("binary").unwrap();

assert!(apple_postings.len() == 2);
assert!(binary_postings.len() == 1);
Expand Down

0 comments on commit d9498f6

Please sign in to comment.