Skip to content

Commit

Permalink
Fix is_child_of function not considering the root facet. (#2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Jun 12, 2023
1 parent 3546e7f commit 3a82ef2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/collector/facet_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ fn facet_depth(facet_bytes: &[u8]) -> usize {
/// ]);
/// }
///
/// {
/// let mut facet_collector = FacetCollector::for_field("facet");
/// facet_collector.add_facet("/");
/// let facet_counts = searcher.search(&AllQuery, &facet_collector)?;
///
/// // This lists all of the facet counts
/// let facets: Vec<(&Facet, u64)> = facet_counts
/// .get("/")
/// .collect();
/// assert_eq!(facets, vec![
/// (&Facet::from("/category"), 4),
/// (&Facet::from("/lang"), 4)
/// ]);
/// }
///
/// Ok(())
/// }
/// # assert!(example().is_ok());
Expand Down Expand Up @@ -285,6 +300,9 @@ fn is_child_facet(parent_facet: &[u8], possible_child_facet: &[u8]) -> bool {
if !possible_child_facet.starts_with(parent_facet) {
return false;
}
if parent_facet.is_empty() {
return true;
}
possible_child_facet.get(parent_facet.len()).copied() == Some(0u8)
}

Expand Down Expand Up @@ -789,6 +807,15 @@ mod tests {
);
Ok(())
}

#[test]
fn is_child_facet() {
assert!(super::is_child_facet(&b"foo"[..], &b"foo\0bar"[..]));
assert!(super::is_child_facet(&b""[..], &b"foo\0bar"[..]));
assert!(super::is_child_facet(&b""[..], &b"foo"[..]));
assert!(!super::is_child_facet(&b"foo\0bar"[..], &b"foo"[..]));
assert!(!super::is_child_facet(&b"foo"[..], &b"foobar\0baz"[..]));
}
}

#[cfg(all(test, feature = "unstable"))]
Expand Down

0 comments on commit 3a82ef2

Please sign in to comment.