web search done right
faster optics
Mikkel Denker5dfeafcb0f5e
diff --git a/core/benches/search-preindexed-optic.rs b/core/benches/search-preindexed-optic.rsindex ea66f0d4..6f40982d 100644--- a/core/benches/search-preindexed-optic.rs+++ b/core/benches/search-preindexed-optic.rs@@ -4,7 +4,7 @@ use stract::{searcher::{LocalSearcher, SearchQuery},};-const INDEX_PATH: &str = "../data/index";+const INDEX_PATH: &str = "data/index";macro_rules! bench {($query:tt, $searcher:ident, $optic:ident, $c:ident) => {@@ -31,12 +31,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {let searcher = LocalSearcher::new(index);let optic = include_str!("../../optics/testcases/hacker_news.optic");- // for _ in 0..10 {- bench!("the", searcher, optic, c);- bench!("dtu", searcher, optic, c);- bench!("the best", searcher, optic, c);- bench!("the circle of life", searcher, optic, c);- // }+ for _ in 0..10 {+ bench!("the", searcher, optic, c);+ bench!("dtu", searcher, optic, c);+ bench!("the best", searcher, optic, c);+ bench!("the circle of life", searcher, optic, c);+ }}criterion_group!(benches, criterion_benchmark);diff --git a/core/src/fastfield_reader.rs b/core/src/fastfield_reader.rsindex 47e0457a..049f20d7 100644--- a/core/src/fastfield_reader.rs+++ b/core/src/fastfield_reader.rs@@ -24,13 +24,18 @@ use crate::{};#[derive(Default, Clone)]-pub struct FastFieldReader {+struct InnerFastFieldReader {segments: HashMap<SegmentId, Arc<SegmentReader>>,}+#[derive(Default, Clone)]+pub struct FastFieldReader {+ inner: Arc<InnerFastFieldReader>,+}+impl FastFieldReader {pub fn get_segment(&self, segment: &SegmentId) -> Arc<SegmentReader> {- Arc::clone(self.segments.get(segment).unwrap())+ Arc::clone(self.inner.segments.get(segment).unwrap())}}@@ -72,7 +77,9 @@ impl FastFieldReader {);}- Self { segments }+ Self {+ inner: Arc::new(InnerFastFieldReader { segments }),+ }}}diff --git a/core/src/query/const_query.rs b/core/src/query/const_query.rsindex eebbd663..a1346e7c 100644--- a/core/src/query/const_query.rs+++ b/core/src/query/const_query.rs@@ -45,8 +45,14 @@ impl Query for ConstQuery {&self,enable_scoring: tantivy::query::EnableScoring,) -> tantivy::Result<Box<dyn Weight>> {+ let schema = match enable_scoring {+ tantivy::query::EnableScoring::Enabled(searcher) => searcher.schema(),+ tantivy::query::EnableScoring::Disabled(s) => s,+ };Ok(Box::new(ConstWeight {- subweight: self.subquery.weight(enable_scoring)?,+ subweight: self+ .subquery+ .weight(tantivy::query::EnableScoring::Disabled(schema))?,score: self.score,}))}diff --git a/core/src/query/optic.rs b/core/src/query/optic.rsindex f937b96b..a68132ec 100644--- a/core/src/query/optic.rs+++ b/core/src/query/optic.rs@@ -105,21 +105,21 @@ impl AsSearchableRule for Rule {let subquery = if subqueries.len() == 1 {subqueries.pop().unwrap().1} else {- BooleanQuery::from(subqueries).box_clone()+ Box::new(BooleanQuery::from(subqueries))};match &self.action {Action::Boost(boost) => Some((Occur::Should,SearchableRule {- query: ConstQuery::new(subquery, 1.0).box_clone(),+ query: Box::new(ConstQuery::new(subquery, 1.0)),boost: *boost as f64,},)),Action::Downrank(boost) => Some((Occur::Should,SearchableRule {- query: ConstQuery::new(subquery, 1.0).box_clone(),+ query: Box::new(ConstQuery::new(subquery, 1.0)),boost: *boost as f64 * -1.0,},)),diff --git a/core/src/query/pattern_query.rs b/core/src/query/pattern_query.rsindex a2b9865a..982958cc 100644--- a/core/src/query/pattern_query.rs+++ b/core/src/query/pattern_query.rs@@ -143,11 +143,10 @@ impl PatternWeight {reader: &SegmentReader,boost: Score,) -> tantivy::Result<Option<PatternScorer>> {- let similarity_weight = if let Some(weight) = &self.similarity_weight {- weight.boost_by(boost)- } else {- return Ok(None);- };+ let similarity_weight = self+ .similarity_weight+ .as_ref()+ .map(|weight| weight.boost_by(boost));let fieldnorm_reader = self.fieldnorm_reader(reader)?;let mut term_postings_list = Vec::new();@@ -246,7 +245,7 @@ impl tantivy::query::Weight for PatternWeight {}struct PatternScorer {- similarity_weight: Bm25Weight,+ similarity_weight: Option<Bm25Weight>,fieldnorm_reader: FieldNormReader,intersection_docset: Intersection<SegmentPostings>,pattern: Vec<SmallPatternPart>,@@ -260,7 +259,7 @@ struct PatternScorer {impl PatternScorer {fn new(- similarity_weight: Bm25Weight,+ similarity_weight: Option<Bm25Weight>,term_postings_list: Vec<SegmentPostings>,fieldnorm_reader: FieldNormReader,pattern: Vec<SmallPatternPart>,@@ -368,10 +367,14 @@ impl PatternScorer {impl Scorer for PatternScorer {fn score(&mut self) -> Score {- let doc = self.doc();- let fieldnorm_id = self.fieldnorm_reader.fieldnorm_id(doc);self.similarity_weight- .score(fieldnorm_id, self.phrase_count())+ .as_ref()+ .map(|scorer| {+ let doc = self.doc();+ let fieldnorm_id = self.fieldnorm_reader.fieldnorm_id(doc);+ scorer.score(fieldnorm_id, self.phrase_count())+ })+ .unwrap_or_default()}}