web search done right
Initial rule or-ing (#127)
SekoiaTree359117bfbe3f
diff --git a/crates/core/src/api/explore.rs b/crates/core/src/api/explore.rsindex c4e4c7fb..6621b46c 100644--- a/crates/core/src/api/explore.rs+++ b/crates/core/src/api/explore.rs@@ -44,14 +44,14 @@ pub async fn explore_export_optic(.into_iter().chain(chosen_hosts.clone().into_iter()).map(|site| optics::Rule {- matches: vec![optics::Matching {+ matches: vec![vec![optics::Matching { // TODO: these can be compressed into a single rule.pattern: vec![optics::PatternPart::Anchor,optics::PatternPart::Raw(site),optics::PatternPart::Anchor,],location: optics::MatchLocation::Domain,- }],+ }]],action: optics::Action::Boost(0),}).collect();diff --git a/crates/core/src/query/optic.rs b/crates/core/src/query/optic.rsindex ca6bff6f..b7128302 100644--- a/crates/core/src/query/optic.rs+++ b/crates/core/src/query/optic.rs@@ -95,7 +95,26 @@ impl AsSearchableRule for Rule {let mut subqueries: Vec<_> = self.matches.iter()- .map(|matching| (Occur::Must, matching.as_tantivy(schema, fastfield_reader)))+ .filter_map(|and_rule| {+ let mut and_queries: Vec<_> = and_rule+ .iter()+ .map(|matching| (Occur::Must, matching.as_tantivy(schema, fastfield_reader)))+ .collect();++ // Empty queries never match anything. A priori these shouldn't exist, but it doesn't+ // really cost us anything to check.+ // (though, technically it's an extra check or two for every rule? But rules aren't parsed very often)+ if and_queries.is_empty() {+ None+ } else {+ let query = if and_queries.len() == 1 {+ and_queries.pop().unwrap().1+ } else {+ Box::new(BooleanQuery::from(and_queries))+ };+ Some((Occur::Should, query))+ }+ }).collect();if subqueries.is_empty() {@@ -946,9 +965,6 @@ mod tests {Matches {Site("stackoverflow.blog")},- Action(Boost(1))- };- Rule {Matches {Site("chat.b.eu")},diff --git a/crates/optics/src/ast.rs b/crates/optics/src/ast.rsindex bc17ffff..5988480c 100644--- a/crates/optics/src/ast.rs+++ b/crates/optics/src/ast.rs@@ -101,7 +101,7 @@ pub enum RawOpticBlock {#[derive(Debug, PartialEq)]pub struct RawRule {- pub matches: RawMatchBlock,+ pub matches: Vec<RawMatchBlock>,pub action: Option<RawAction>,}@@ -196,16 +196,16 @@ mod tests {RawOptic {rules: vec![RawRule {- matches: RawMatchBlock(vec![RawMatchPart::Url(+ matches: vec![RawMatchBlock(vec![RawMatchPart::Url("/this/is/a/*/pattern".to_string()- )]),+ )])],action: None,},RawRule {- matches: RawMatchBlock(vec![+ matches: vec![RawMatchBlock(vec![RawMatchPart::Url("/this/is/a/pattern".to_string()),RawMatchPart::Site("example.com".to_string()),- ],),+ ])],action: None,},],@@ -250,13 +250,13 @@ mod tests {RawOptic {rules: vec![RawRule {- matches: RawMatchBlock(vec![RawMatchPart::Url(+ matches: vec![RawMatchBlock(vec![RawMatchPart::Url("/this/is/a/*/pattern".to_string()- )]),+ )])],action: Some(RawAction::Boost(2)),},RawRule {- matches: RawMatchBlock(vec![RawMatchPart::Site("example.com".to_string())],),+ matches: vec![RawMatchBlock(vec![RawMatchPart::Site("example.com".to_string())])],action: Some(RawAction::Downrank(4)),},],@@ -293,13 +293,13 @@ mod tests {RawOptic {rules: vec![RawRule {- matches: RawMatchBlock(vec![RawMatchPart::Url(+ matches: vec![RawMatchBlock(vec![RawMatchPart::Url("/this/is/a/*/pattern".to_string()- )]),+ )])],action: Some(RawAction::Boost(2)),},RawRule {- matches: RawMatchBlock(vec![RawMatchPart::Site("example.com".to_string())],),+ matches: vec![RawMatchBlock(vec![RawMatchPart::Site("example.com".to_string())])],action: Some(RawAction::Downrank(4)),},],diff --git a/crates/optics/src/lib.rs b/crates/optics/src/lib.rsindex 0c4f1d34..7be0911f 100644--- a/crates/optics/src/lib.rs+++ b/crates/optics/src/lib.rs@@ -107,15 +107,24 @@ impl TryFrom<RawRule> for Rule {type Error = Error;fn try_from(raw: RawRule) -> Result<Self> {- let mut matches = Vec::new();-- for matching in raw.matches.0 {- matches.push(matching.try_into()?);- }+ let RawRule {+ matches,+ action,+ } = raw;++ let matches = matches+ .into_iter()+ .map(|m|+ m.0+ .into_iter()+ .map(|m| <RawMatchPart as TryInto<Matching>>::try_into(m))+ .collect::<Result<Vec<Matching>>>()+ )+ .collect::<Result<Vec<Vec<Matching>>>>()?;Ok(Rule {matches,- action: raw.action.map(Action::from).unwrap_or(Action::Boost(0)),+ action: action.map(Action::from).unwrap_or(Action::Boost(0)),})}}@@ -349,7 +358,9 @@ impl ToString for Optic {#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]pub struct Rule {- pub matches: Vec<Matching>,+ /// A list of matchings, structured as an OR of ANDs (i.e. the rule matches if all of the matchings inside one list match).+ pub matches: Vec<Vec<Matching>>,+ /// What action to take if the rule matches.pub action: Action,}@@ -359,11 +370,13 @@ impl ToString for Rule {res.push_str("Rule {\n");if !self.matches.is_empty() {- res.push_str("\tMatches {\n");- for m in &self.matches {- res.push_str(&format!("\t\t{},\n", m.to_string()));+ for matcher in &self.matches {+ res.push_str("\tMatches {\n");+ for m in matcher {+ res.push_str(&format!("\t\t{},\n", m.to_string()));+ }+ res.push_str("\t}")}- res.push_str("\t}")}res.push_str(&format!("\t{}\n", self.action.to_string()));@@ -393,16 +406,17 @@ impl ToString for HostRankings {res.push_str(&format!("Dislike(Site(\"{}\"));\n", disliked));}+ // TODO: this can be reformatted as a single rule.for blocked in &self.blocked {let rule = Rule {- matches: vec![Matching {+ matches: vec![vec![Matching {pattern: vec![PatternPart::Anchor,PatternPart::Raw(blocked.clone()),PatternPart::Anchor,],location: MatchLocation::Site,- }],+ }]],action: Action::Discard,};@@ -422,15 +436,15 @@ impl HostRankings {.map(|host| host.to_string()).unwrap_or(host.clone())})- .map(|host| Rule {- matches: vec![Matching {+ .map(|host| Rule { // TODO: this can be reformatted as a single rule.+ matches: vec![vec![Matching {pattern: vec![PatternPart::Anchor,PatternPart::Raw(host.clone()),PatternPart::Anchor,],location: MatchLocation::Site,- }],+ }]],action: Action::Discard,}).collect()@@ -492,14 +506,14 @@ mod tests {blocked: vec![],},rules: vec![Rule {- matches: vec![Matching {+ matches: vec![vec![Matching {pattern: vec![PatternPart::Anchor,PatternPart::Raw("test".to_string()),PatternPart::Anchor,],location: MatchLocation::Site,- }],+ }]],action: Action::Boost(0),}],discard_non_matching: true,diff --git a/crates/optics/src/parser.lalrpop b/crates/optics/src/parser.lalrpopindex 500ef16d..7ebca451 100644--- a/crates/optics/src/parser.lalrpop+++ b/crates/optics/src/parser.lalrpop@@ -24,14 +24,14 @@ Block: RawOpticBlock = {}Rule: RawRule = {- "Rule" "{" <matches:RawMatchBlock> <action:RawAction?> "}" => RawRule {+ "Rule" "{" <matches:Sep<",", RawMatchBlock>> <action:RawAction?> "}" => RawRule {matches,action}}RawMatchBlock: RawMatchBlock = {- "Matches" "{" <Sep<",", RawMatchPart>> "}" ","? => RawMatchBlock(<>)+ "Matches" "{" <Sep<",", RawMatchPart>> "}" => RawMatchBlock(<>)}RawMatchPart: RawMatchPart = {