web search done right
[webgraph] a not-filter inside an or-filter mistakenly filtered everything
Mikkel Denkerb4a68a7385cf
diff --git a/crates/core/src/webgraph/query/filter/not.rs b/crates/core/src/webgraph/query/filter/not.rsindex c1fd6419..a0916809 100644--- a/crates/core/src/webgraph/query/filter/not.rs+++ b/crates/core/src/webgraph/query/filter/not.rs@@ -95,7 +95,7 @@ mod tests {use crate::{webgraph::{- query::{FullForwardlinksQuery, TextFilter},+ query::{FullForwardlinksQuery, OrFilter, TextFilter},schema::ToUrl,Edge, Node, Webgraph,},@@ -108,6 +108,7 @@ mod tests {vec![(Node::from("a.com"), Node::from("b.com/123"), String::new()),(Node::from("a.com"), Node::from("b.dk/123"), String::new()),+ (Node::from("a.com"), Node::from("b.se/123"), String::new()),(Node::from("a.com"), Node::from("b.com/321"), String::new()),(Node::from("a.com"), Node::from("c.com"), String::new()),]@@ -146,7 +147,26 @@ mod tests {).unwrap();- assert_eq!(res.len(), 3);+ assert_eq!(res.len(), 4);assert!(res.iter().all(|r| !r.to.as_str().contains(".dk")));}++ #[test]+ fn test_not_inside_or() {+ let (graph, _temp_dir) = test_graph();+ let node = Node::from("a.com");++ let res = graph+ .search(+ &FullForwardlinksQuery::new(node).filter(+ OrFilter::new()+ .or(NotFilter::new(TextFilter::new(".dk".to_string(), ToUrl)))+ .or(TextFilter::new(".com".to_string(), ToUrl)),+ ),+ )+ .unwrap();++ assert_eq!(res.len(), 3);+ assert!(res.iter().all(|r| r.to.as_str().contains(".com")));+ }}diff --git a/crates/core/src/webgraph/query/filter/or.rs b/crates/core/src/webgraph/query/filter/or.rsindex 6405b89e..89ce7106 100644--- a/crates/core/src/webgraph/query/filter/or.rs+++ b/crates/core/src/webgraph/query/filter/or.rs@@ -115,11 +115,21 @@ impl super::InvertedIndexFilter for OrInvertedIndexFilter {let mut queries = Vec::with_capacity(self.filters.len());for filter in self.filters.iter() {for (occur, query) in filter.query(searcher) {- queries.push((Occur::compose(Occur::Should, occur), query));+ let occur = match occur {+ Occur::Should => Occur::Should,+ Occur::Must => Occur::Should,+ Occur::MustNot => Occur::MustNot,+ };++ queries.push((occur, query));}}- vec![(Occur::Should, Box::new(BooleanQuery::new(queries)))]+ if queries.iter().all(|(occur, _)| *occur == Occur::Should) {+ vec![(Occur::Should, Box::new(BooleanQuery::new(queries)))]+ } else {+ queries+ }}}