Skip to content

web search done right

Fix phrase search

Mikkel Denker030a0a50cea5
diff --git a/core/benches/search-preindexed.rs b/core/benches/search-preindexed.rs
index c22b9e3a..86b66d57 100644
--- a/core/benches/search-preindexed.rs
+++ b/core/benches/search-preindexed.rs
@@ -16,24 +16,21 @@ macro_rules! bench {
desc.push('\'');
$c.bench_function(desc.as_str(), |b| {
b.iter(|| {
- dbg!(
- $searcher
- .search(&SearchQuery {
- query: $query.to_string(),
- site_rankings: Some(SiteRankings {
- liked: vec![
- "docs.rs".to_string(),
- "news.ycombinator.com".to_string(),
- "pubmed.ncbi.nlm.nih.gov".to_string(),
- ],
- disliked: vec!["www.pinterest.com".to_string()],
- blocked: vec![],
- }),
- ..Default::default()
- })
- .unwrap()
- .num_hits
- )
+ $searcher
+ .search(&SearchQuery {
+ query: $query.to_string(),
+ site_rankings: Some(SiteRankings {
+ liked: vec![
+ "docs.rs".to_string(),
+ "news.ycombinator.com".to_string(),
+ "pubmed.ncbi.nlm.nih.gov".to_string(),
+ ],
+ disliked: vec!["www.pinterest.com".to_string()],
+ blocked: vec![],
+ }),
+ ..Default::default()
+ })
+ .unwrap()
})
});
};
diff --git a/core/src/query/mod.rs b/core/src/query/mod.rs
index 1f2c5910..e4039212 100644
--- a/core/src/query/mod.rs
+++ b/core/src/query/mod.rs
@@ -56,7 +56,6 @@ pub struct Query {
impl Query {
pub fn parse(ctx: &Ctx, query: &SearchQuery, index: &InvertedIndex) -> Result<Query> {
let parsed_terms = parser::parse(&query.query);
-
let mut term_count = HashMap::new();
let mut terms = Vec::new();
diff --git a/core/src/query/parser.rs b/core/src/query/parser.rs
index e841d46d..124bcb10 100644
--- a/core/src/query/parser.rs
+++ b/core/src/query/parser.rs
@@ -15,7 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use tantivy::{
- query::{BooleanQuery, Occur, TermQuery},
+ query::{BooleanQuery, Occur, PhraseQuery, TermQuery},
tokenizer::{TextAnalyzer, TokenizerManager},
};
@@ -96,10 +96,29 @@ impl Term {
.filter(|(field, _)| ALL_FIELDS[field.field_id() as usize].is_searchable())
.filter(|(field, _)| ALL_FIELDS[field.field_id() as usize].has_pos())
{
- phrases.push((
- Occur::Should,
- Term::tantivy_text_query(field, entry, tokenizer_manager, phrase),
- ));
+ if let Some(analyzer) = Term::get_tantivy_analyzer(entry, tokenizer_manager) {
+ let mut processed_terms =
+ Term::process_tantivy_term(phrase, Some(analyzer), *field);
+
+ if processed_terms.len() == 1 {
+ let options = ALL_FIELDS[field.field_id() as usize]
+ .as_text()
+ .unwrap()
+ .index_option();
+
+ phrases.push((
+ Occur::Should,
+ Box::new(TermQuery::new(processed_terms.pop().unwrap(), options))
+ as Box<dyn tantivy::query::Query>,
+ ));
+ } else {
+ phrases.push((
+ Occur::Should,
+ Box::new(PhraseQuery::new(processed_terms))
+ as Box<dyn tantivy::query::Query>,
+ ));
+ }
+ }
}
(Occur::Must, Box::new(BooleanQuery::new(phrases)))