Skip to content

web search done right

Make places that use rules use rule OR-ing (#133)

SekoiaTree2a62f9c28a59
diff --git a/crates/core/src/api/explore.rs b/crates/core/src/api/explore.rs
index 6621b46c..a9fe53a3 100644
--- a/crates/core/src/api/explore.rs
+++ b/crates/core/src/api/explore.rs
@@ -40,28 +40,30 @@ pub async fn explore_export_optic(
similar_hosts,
}): extract::Json<ExploreExportOpticParams>,
) -> Result<String, StatusCode> {
- let rules = similar_hosts
+ let matches = similar_hosts
.into_iter()
.chain(chosen_hosts.clone().into_iter())
- .map(|site| optics::Rule {
- matches: vec![vec![optics::Matching { // TODO: these can be compressed into a single rule.
+ .map(|site| vec![optics::Matching {
pattern: vec![
optics::PatternPart::Anchor,
optics::PatternPart::Raw(site),
optics::PatternPart::Anchor,
],
location: optics::MatchLocation::Domain,
- }]],
- action: optics::Action::Boost(0),
- })
+ }]
+ )
.collect();
+ let rule = optics::Rule {
+ matches,
+ action: optics::Action::Boost(0),
+ };
let optic = Optic {
host_rankings: HostRankings {
liked: chosen_hosts,
..Default::default()
},
- rules,
+ rules: vec![rule],
discard_non_matching: true,
..Default::default()
};
diff --git a/crates/core/src/query/optic.rs b/crates/core/src/query/optic.rs
index b7128302..c869ea61 100644
--- a/crates/core/src/query/optic.rs
+++ b/crates/core/src/query/optic.rs
@@ -14,6 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+use std::iter;
use itertools::Itertools;
use optics::{Action, MatchLocation, Matching, Optic, Rule};
use tantivy::{
@@ -53,7 +54,7 @@ impl AsMultipleTantivyQuery for Optic {
UnionQuery::from(
self.rules
.iter()
- .chain(self.host_rankings.rules().iter())
+ .chain(iter::once(&self.host_rankings.rules()))
.filter_map(|rule| rule.as_searchable_rule(schema, fastfields))
.map(|(occur, rule)| {
BooleanQuery::from(vec![(occur, rule.query)]).box_clone()
@@ -65,7 +66,7 @@ impl AsMultipleTantivyQuery for Optic {
} else {
self.rules
.iter()
- .chain(self.host_rankings.rules().iter())
+ .chain(iter::once(&self.host_rankings.rules()))
.filter_map(|rule| rule.as_searchable_rule(schema, fastfields))
.map(|(occur, rule)| (occur, rule.query))
.collect()
diff --git a/crates/optics/src/lib.rs b/crates/optics/src/lib.rs
index 7be0911f..87d71e8e 100644
--- a/crates/optics/src/lib.rs
+++ b/crates/optics/src/lib.rs
@@ -406,17 +406,27 @@ 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 matches : Vec<_> = self.blocked
+ .iter()
+ .map(|host| {
+ host.strip_prefix("www.")
+ .map(|host| host.to_string())
+ .unwrap_or(host.clone())
+ })
+ .map(|host| vec![Matching {
+ pattern: vec![
+ PatternPart::Anchor,
+ PatternPart::Raw(host.clone()),
+ PatternPart::Anchor,
+ ],
+ location: MatchLocation::Site,
+ }]
+ )
+ .collect();
+
+ if !matches.is_empty() {
let rule = Rule {
- matches: vec![vec![Matching {
- pattern: vec![
- PatternPart::Anchor,
- PatternPart::Raw(blocked.clone()),
- PatternPart::Anchor,
- ],
- location: MatchLocation::Site,
- }]],
+ matches,
action: Action::Discard,
};
@@ -428,26 +438,29 @@ impl ToString for HostRankings {
}
impl HostRankings {
- pub fn rules(&self) -> Vec<Rule> {
- self.blocked
+ pub fn rules(&self) -> Rule {
+ let matches : Vec<_> = self.blocked
.iter()
.map(|host| {
host.strip_prefix("www.")
.map(|host| host.to_string())
.unwrap_or(host.clone())
})
- .map(|host| Rule { // TODO: this can be reformatted as a single rule.
- matches: vec![vec![Matching {
+ .map(|host| vec![Matching {
pattern: vec![
PatternPart::Anchor,
PatternPart::Raw(host.clone()),
PatternPart::Anchor,
],
location: MatchLocation::Site,
- }]],
- action: Action::Discard,
- })
- .collect()
+ }]
+ )
+ .collect();
+
+ Rule {
+ matches,
+ action: Action::Discard,
+ }
}
pub fn into_optic(self) -> Optic {