From be9d06d9078a3b3951eac84890efb2793518c75c Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Sat, 10 Jun 2023 09:44:09 +0200 Subject: [PATCH] Fix is_child_of function not considering the root facet. --- src/collector/facet_collector.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/collector/facet_collector.rs b/src/collector/facet_collector.rs index 6861a9faa0..2364e7beed 100644 --- a/src/collector/facet_collector.rs +++ b/src/collector/facet_collector.rs @@ -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()); @@ -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) } @@ -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"))]