Skip to content

web search done right

make sure site query terms are treated as phrase for correct matching

Mikkel Denker905fec80ae1e
diff --git a/crates/core/src/query/mod.rs b/crates/core/src/query/mod.rs
index ce2c8abb..4450b2f9 100644
--- a/crates/core/src/query/mod.rs
+++ b/crates/core/src/query/mod.rs
@@ -431,6 +431,24 @@ mod tests {
.unwrap(),
)
.expect("failed to insert webpage");
+ index
+ .insert(
+ &Webpage::test_parse(
+ r#"
+ <html>
+ <head>
+ <title>Test test</title>
+ </head>
+ <body>
+ This test page does not contain the forbidden word
+ </body>
+ </html>
+ "#,
+ "https://www.second.com/first",
+ )
+ .unwrap(),
+ )
+ .expect("failed to insert webpage");
index.commit().expect("failed to commit index");
let searcher = LocalSearcher::from(index);
@@ -455,8 +473,12 @@ mod tests {
..Default::default()
};
let result = searcher.search(&query).expect("Search failed");
- assert_eq!(result.webpages.len(), 1);
- assert_eq!(result.webpages[0].url, "https://www.second.com/");
+ assert_eq!(result.webpages.len(), 2);
+
+ assert!(result
+ .webpages
+ .iter()
+ .all(|w| w.url != "https://www.first.com/"));
}
#[test]
diff --git a/crates/core/src/query/plan/mod.rs b/crates/core/src/query/plan/mod.rs
index cf578f63..72a31576 100644
--- a/crates/core/src/query/plan/mod.rs
+++ b/crates/core/src/query/plan/mod.rs
@@ -148,18 +148,7 @@ impl Query {
let term = terms.remove(0);
Some(Box::new(tantivy::query::TermQuery::new(term, option)))
} else {
- Some(Box::new(tantivy::query::BooleanQuery::new(
- terms
- .into_iter()
- .map(|term| {
- (
- tantivy::query::Occur::Must,
- Box::new(tantivy::query::TermQuery::new(term, option))
- as Box<dyn tantivy::query::Query + 'static>,
- )
- })
- .collect(),
- )))
+ Some(Box::new(tantivy::query::PhraseQuery::new(terms)))
}
}
SimpleOrPhrase::Phrase(p) => {