From 0346942174367ee43cf88b4745ab74bac6271669 Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Tue, 20 Jan 2026 14:21:10 +0100 Subject: [PATCH] blop --- src/postings/loaded_postings.rs | 4 +-- src/query/all_query.rs | 6 ++-- src/query/const_score_query.rs | 7 +++-- src/query/empty_query.rs | 4 +-- src/query/exist_query.rs | 12 +++---- src/query/intersection.rs | 10 +++--- src/query/mod.rs | 2 +- .../phrase_prefix_weight.rs | 6 ++-- src/query/phrase_query/phrase_weight.rs | 6 ++-- src/query/phrase_query/regex_phrase_weight.rs | 8 +++-- src/query/range_query/range_query.rs | 6 ++-- .../range_query/range_query_fastfield.rs | 31 ++++++++++--------- src/query/scorer.rs | 2 +- src/query/term_query/term_weight.rs | 6 ++-- 14 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/postings/loaded_postings.rs b/src/postings/loaded_postings.rs index 2b0ce8cd0..937558bb1 100644 --- a/src/postings/loaded_postings.rs +++ b/src/postings/loaded_postings.rs @@ -2,7 +2,7 @@ use crate::docset::{DocSet, TERMINATED}; use crate::fieldnorm::FieldNormReader; use crate::postings::Postings; use crate::query::term_query::TermScorer; -use crate::query::{Bm25Weight, Scorer}; +use crate::query::{box_scorer, Bm25Weight, Scorer}; use crate::DocId; /// `LoadedPostings` is a `DocSet` and `Postings` implementation. @@ -30,7 +30,7 @@ impl LoadedPostings { fieldnorm_reader: FieldNormReader, similarity_weight: Bm25Weight, ) -> Box { - Box::new(TermScorer::new(*self, fieldnorm_reader, similarity_weight)) + box_scorer(TermScorer::new(*self, fieldnorm_reader, similarity_weight)) } /// Creates a new `LoadedPostings` from a `SegmentPostings`. diff --git a/src/query/all_query.rs b/src/query/all_query.rs index 5431a3a1b..ff7351ec3 100644 --- a/src/query/all_query.rs +++ b/src/query/all_query.rs @@ -2,7 +2,7 @@ use crate::docset::{DocSet, COLLECT_BLOCK_BUFFER_LEN, TERMINATED}; use crate::index::SegmentReader; use crate::query::boost_query::BoostScorer; use crate::query::explanation::does_not_match; -use crate::query::{EnableScoring, Explanation, Query, Scorer, Weight}; +use crate::query::{box_scorer, EnableScoring, Explanation, Query, Scorer, Weight}; use crate::{DocId, Score}; /// Query that matches all of the documents. @@ -24,9 +24,9 @@ impl Weight for AllWeight { fn scorer(&self, reader: &SegmentReader, boost: Score) -> crate::Result> { let all_scorer = AllScorer::new(reader.max_doc()); if boost != 1.0 { - Ok(Box::new(BoostScorer::new(all_scorer, boost))) + Ok(box_scorer(BoostScorer::new(all_scorer, boost))) } else { - Ok(Box::new(all_scorer)) + Ok(box_scorer(all_scorer)) } } diff --git a/src/query/const_score_query.rs b/src/query/const_score_query.rs index d07e6a96f..49c9cf583 100644 --- a/src/query/const_score_query.rs +++ b/src/query/const_score_query.rs @@ -1,7 +1,7 @@ use std::fmt; use crate::docset::COLLECT_BLOCK_BUFFER_LEN; -use crate::query::{EnableScoring, Explanation, Query, Scorer, Weight}; +use crate::query::{box_scorer, EnableScoring, Explanation, Query, Scorer, Weight}; use crate::{DocId, DocSet, Score, SegmentReader, TantivyError, Term}; /// `ConstScoreQuery` is a wrapper over a query to provide a constant score. @@ -65,7 +65,10 @@ impl ConstWeight { impl Weight for ConstWeight { fn scorer(&self, reader: &SegmentReader, boost: Score) -> crate::Result> { let inner_scorer = self.weight.scorer(reader, boost)?; - Ok(Box::new(ConstScorer::new(inner_scorer, boost * self.score))) + Ok(box_scorer(ConstScorer::new( + inner_scorer, + boost * self.score, + ))) } fn explain(&self, reader: &SegmentReader, doc: u32) -> crate::Result { diff --git a/src/query/empty_query.rs b/src/query/empty_query.rs index 2fa1772bd..75467e524 100644 --- a/src/query/empty_query.rs +++ b/src/query/empty_query.rs @@ -2,7 +2,7 @@ use super::Scorer; use crate::docset::TERMINATED; use crate::index::SegmentReader; use crate::query::explanation::does_not_match; -use crate::query::{EnableScoring, Explanation, Query, Weight}; +use crate::query::{box_scorer, EnableScoring, Explanation, Query, Weight}; use crate::{DocId, DocSet, Score, Searcher}; /// `EmptyQuery` is a dummy `Query` in which no document matches. @@ -27,7 +27,7 @@ impl Query for EmptyQuery { pub struct EmptyWeight; impl Weight for EmptyWeight { fn scorer(&self, _reader: &SegmentReader, _boost: Score) -> crate::Result> { - Ok(Box::new(EmptyScorer)) + Ok(box_scorer(EmptyScorer)) } fn explain(&self, _reader: &SegmentReader, doc: DocId) -> crate::Result { diff --git a/src/query/exist_query.rs b/src/query/exist_query.rs index 7eb09722c..572ce928d 100644 --- a/src/query/exist_query.rs +++ b/src/query/exist_query.rs @@ -3,7 +3,7 @@ use core::fmt::Debug; use columnar::{ColumnIndex, DynamicColumn}; use common::BitSet; -use super::{ConstScorer, EmptyScorer}; +use super::{box_scorer, ConstScorer, EmptyScorer}; use crate::docset::{DocSet, TERMINATED}; use crate::index::SegmentReader; use crate::query::all_query::AllScorer; @@ -117,7 +117,7 @@ impl Weight for ExistsWeight { } } if non_empty_columns.is_empty() { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); } // If any column is full, all docs match. @@ -128,9 +128,9 @@ impl Weight for ExistsWeight { { let all_scorer = AllScorer::new(max_doc); if boost != 1.0f32 { - return Ok(Box::new(BoostScorer::new(all_scorer, boost))); + return Ok(box_scorer(BoostScorer::new(all_scorer, boost))); } else { - return Ok(Box::new(all_scorer)); + return Ok(box_scorer(all_scorer)); } } @@ -138,7 +138,7 @@ impl Weight for ExistsWeight { // NOTE: A lower number may be better for very sparse columns if non_empty_columns.len() < 4 { let docset = ExistsDocSet::new(non_empty_columns, reader.max_doc()); - return Ok(Box::new(ConstScorer::new(docset, boost))); + return Ok(box_scorer(ConstScorer::new(docset, boost))); } // If we have many dynamic columns, precompute a bitset of matching docs @@ -162,7 +162,7 @@ impl Weight for ExistsWeight { } } let docset = BitSetDocSet::from(doc_bitset); - Ok(Box::new(ConstScorer::new(docset, boost))) + Ok(box_scorer(ConstScorer::new(docset, boost))) } fn explain(&self, reader: &SegmentReader, doc: DocId) -> crate::Result { diff --git a/src/query/intersection.rs b/src/query/intersection.rs index d536dcf05..723dad85d 100644 --- a/src/query/intersection.rs +++ b/src/query/intersection.rs @@ -1,7 +1,7 @@ use super::size_hint::estimate_intersection; use crate::docset::{DocSet, TERMINATED}; use crate::query::term_query::TermScorer; -use crate::query::{EmptyScorer, Scorer}; +use crate::query::{box_scorer, EmptyScorer, Scorer}; use crate::{DocId, Score}; /// Returns the intersection scorer. @@ -20,7 +20,7 @@ pub fn intersect_scorers( num_docs_segment: u32, ) -> Box { if scorers.is_empty() { - return Box::new(EmptyScorer); + return box_scorer(EmptyScorer); } if scorers.len() == 1 { return scorers.pop().unwrap(); @@ -29,7 +29,7 @@ pub fn intersect_scorers( scorers.sort_by_key(|scorer| scorer.cost()); let doc = go_to_first_doc(&mut scorers[..]); if doc == TERMINATED { - return Box::new(EmptyScorer); + return box_scorer(EmptyScorer); } // We know that we have at least 2 elements. let left = scorers.remove(0); @@ -38,14 +38,14 @@ pub fn intersect_scorers( .iter() .all(|&scorer| scorer.is::()); if all_term_scorers { - return Box::new(Intersection { + return box_scorer(Intersection { left: *(left.downcast::().map_err(|_| ()).unwrap()), right: *(right.downcast::().map_err(|_| ()).unwrap()), others: scorers, num_docs: num_docs_segment, }); } - Box::new(Intersection { + box_scorer(Intersection { left, right, others: scorers, diff --git a/src/query/mod.rs b/src/query/mod.rs index 314fad5bd..5b5556a2b 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -60,7 +60,7 @@ pub use self::range_query::*; pub use self::regex_query::RegexQuery; pub use self::reqopt_scorer::RequiredOptionalScorer; pub use self::score_combiner::{DisjunctionMaxCombiner, ScoreCombiner, SumCombiner}; -pub use self::scorer::Scorer; +pub use self::scorer::{box_scorer, Scorer}; pub use self::set_query::TermSetQuery; pub use self::term_query::TermQuery; pub use self::union::BufferedUnionScorer; diff --git a/src/query/phrase_prefix_query/phrase_prefix_weight.rs b/src/query/phrase_prefix_query/phrase_prefix_weight.rs index a11fcf64d..3a05c137b 100644 --- a/src/query/phrase_prefix_query/phrase_prefix_weight.rs +++ b/src/query/phrase_prefix_query/phrase_prefix_weight.rs @@ -3,7 +3,7 @@ use crate::fieldnorm::FieldNormReader; use crate::index::SegmentReader; use crate::postings::Postings; use crate::query::bm25::Bm25Weight; -use crate::query::{EmptyScorer, Explanation, Scorer, Weight}; +use crate::query::{box_scorer, EmptyScorer, Explanation, Scorer, Weight}; use crate::schema::{IndexRecordOption, Term}; use crate::{DocId, Score}; @@ -103,7 +103,7 @@ impl PhrasePrefixWeight { } // TODO make this specialized. - Ok(Some(Box::new(PhrasePrefixScorer::new( + Ok(Some(box_scorer(PhrasePrefixScorer::new( term_postings_list, similarity_weight_opt, fieldnorm_reader, @@ -118,7 +118,7 @@ impl Weight for PhrasePrefixWeight { if let Some(scorer) = self.phrase_scorer(reader, boost)? { Ok(scorer) } else { - Ok(Box::new(EmptyScorer)) + Ok(box_scorer(EmptyScorer)) } } diff --git a/src/query/phrase_query/phrase_weight.rs b/src/query/phrase_query/phrase_weight.rs index 0ae96aa46..243e95cd1 100644 --- a/src/query/phrase_query/phrase_weight.rs +++ b/src/query/phrase_query/phrase_weight.rs @@ -3,7 +3,7 @@ use crate::fieldnorm::FieldNormReader; use crate::index::SegmentReader; use crate::query::bm25::Bm25Weight; use crate::query::explanation::does_not_match; -use crate::query::{EmptyScorer, Explanation, Scorer, Weight}; +use crate::query::{box_scorer, EmptyScorer, Explanation, Scorer, Weight}; use crate::schema::{IndexRecordOption, Term}; use crate::{DocId, DocSet, Score}; @@ -59,7 +59,7 @@ impl PhraseWeight { return Ok(None); } } - Ok(Some(Box::new(PhraseScorer::new( + Ok(Some(box_scorer(PhraseScorer::new( term_postings_list, similarity_weight_opt, fieldnorm_reader, @@ -77,7 +77,7 @@ impl Weight for PhraseWeight { if let Some(scorer) = self.phrase_scorer(reader, boost)? { Ok(scorer) } else { - Ok(Box::new(EmptyScorer)) + Ok(box_scorer(EmptyScorer)) } } diff --git a/src/query/phrase_query/regex_phrase_weight.rs b/src/query/phrase_query/regex_phrase_weight.rs index 10abfbc98..04bb57bab 100644 --- a/src/query/phrase_query/regex_phrase_weight.rs +++ b/src/query/phrase_query/regex_phrase_weight.rs @@ -10,7 +10,9 @@ use crate::postings::{LoadedPostings, Postings, TermInfo}; use crate::query::bm25::Bm25Weight; use crate::query::explanation::does_not_match; use crate::query::union::{BitSetPostingUnion, SimpleUnion}; -use crate::query::{AutomatonWeight, BitSetDocSet, EmptyScorer, Explanation, Scorer, Weight}; +use crate::query::{ + box_scorer, AutomatonWeight, BitSetDocSet, EmptyScorer, Explanation, Scorer, Weight, +}; use crate::schema::{Field, IndexRecordOption}; use crate::{DocId, DocSet, InvertedIndexReader, Score}; @@ -262,9 +264,9 @@ impl RegexPhraseWeight { impl Weight for RegexPhraseWeight { fn scorer(&self, reader: &SegmentReader, boost: Score) -> crate::Result> { if let Some(scorer) = self.phrase_scorer(reader, boost)? { - Ok(Box::new(scorer)) + Ok(box_scorer(scorer)) } else { - Ok(Box::new(EmptyScorer)) + Ok(box_scorer(EmptyScorer)) } } diff --git a/src/query/range_query/range_query.rs b/src/query/range_query/range_query.rs index fcb066fe4..df0f5fa6f 100644 --- a/src/query/range_query/range_query.rs +++ b/src/query/range_query/range_query.rs @@ -8,7 +8,9 @@ use super::range_query_fastfield::FastFieldRangeWeight; use crate::index::SegmentReader; use crate::query::explanation::does_not_match; use crate::query::range_query::is_type_valid_for_fastfield_range_query; -use crate::query::{BitSetDocSet, ConstScorer, EnableScoring, Explanation, Query, Scorer, Weight}; +use crate::query::{ + box_scorer, BitSetDocSet, ConstScorer, EnableScoring, Explanation, Query, Scorer, Weight, +}; use crate::schema::{Field, IndexRecordOption, Term, Type}; use crate::termdict::{TermDictionary, TermStreamer}; use crate::{DocId, DocSet, Score}; @@ -233,7 +235,7 @@ impl Weight for InvertedIndexRangeWeight { postings.fill_bitset(&mut doc_bitset); } let doc_bitset = BitSetDocSet::from(doc_bitset); - Ok(Box::new(ConstScorer::new(doc_bitset, boost))) + Ok(box_scorer(ConstScorer::new(doc_bitset, boost))) } fn explain(&self, reader: &SegmentReader, doc: DocId) -> crate::Result { diff --git a/src/query/range_query/range_query_fastfield.rs b/src/query/range_query/range_query_fastfield.rs index e379e108e..f048afa55 100644 --- a/src/query/range_query/range_query_fastfield.rs +++ b/src/query/range_query/range_query_fastfield.rs @@ -13,7 +13,8 @@ use common::bounds::{BoundsRange, TransformBound}; use super::fast_field_range_doc_set::RangeDocSet; use crate::query::{ - AllScorer, ConstScorer, EmptyScorer, EnableScoring, Explanation, Query, Scorer, Weight, + box_scorer, AllScorer, ConstScorer, EmptyScorer, EnableScoring, Explanation, Query, Scorer, + Weight, }; use crate::schema::{Type, ValueBytes}; use crate::{DocId, DocSet, Score, SegmentReader, TantivyError, Term}; @@ -55,7 +56,7 @@ impl Weight for FastFieldRangeWeight { fn scorer(&self, reader: &SegmentReader, boost: Score) -> crate::Result> { // Check if both bounds are Bound::Unbounded if self.bounds.is_unbounded() { - return Ok(Box::new(AllScorer::new(reader.max_doc()))); + return Ok(box_scorer(AllScorer::new(reader.max_doc()))); } let term = self @@ -95,7 +96,7 @@ impl Weight for FastFieldRangeWeight { let Some(str_dict_column): Option = reader.fast_fields().str(&field_name)? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; let dict = str_dict_column.dictionary(); @@ -107,7 +108,7 @@ impl Weight for FastFieldRangeWeight { let Some((column, _col_type)) = fast_field_reader .u64_lenient_for_type(Some(&[ColumnType::Str]), &field_name)? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; search_on_u64_ff(column, boost, BoundsRange::new(lower_bound, upper_bound)) } @@ -119,7 +120,7 @@ impl Weight for FastFieldRangeWeight { let Some((column, _col_type)) = fast_field_reader .u64_lenient_for_type(Some(&[ColumnType::DateTime]), &field_name)? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; let bounds = bounds.map_bound(|term| term.as_date().unwrap().to_u64()); search_on_u64_ff( @@ -146,7 +147,7 @@ impl Weight for FastFieldRangeWeight { let Some(ip_addr_column): Option> = reader.fast_fields().column_opt(&field_name)? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; let value_range = bound_range_inclusive_ip( &bounds.lower_bound, @@ -155,11 +156,11 @@ impl Weight for FastFieldRangeWeight { ip_addr_column.max_value(), ); let docset = RangeDocSet::new(value_range, ip_addr_column); - Ok(Box::new(ConstScorer::new(docset, boost))) + Ok(box_scorer(ConstScorer::new(docset, boost))) } else if field_type.is_str() { let Some(str_dict_column): Option = reader.fast_fields().str(&field_name)? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; let dict = str_dict_column.dictionary(); @@ -171,7 +172,7 @@ impl Weight for FastFieldRangeWeight { let Some((column, _col_type)) = fast_field_reader.u64_lenient_for_type(None, &field_name)? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; search_on_u64_ff(column, boost, BoundsRange::new(lower_bound, upper_bound)) } else { @@ -209,7 +210,7 @@ impl Weight for FastFieldRangeWeight { &field_name, )? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; search_on_u64_ff( column, @@ -250,7 +251,7 @@ fn search_on_json_numerical_field( let Some((column, col_type)) = fast_field_reader.u64_lenient_for_type(allowed_column_types, field_name)? else { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); }; let actual_column_type: NumericalType = col_type .numerical_type() @@ -408,18 +409,18 @@ fn search_on_u64_ff( ) .unwrap_or(1..=0); // empty range if value_range.is_empty() { - return Ok(Box::new(EmptyScorer)); + return Ok(box_scorer(EmptyScorer)); } if col_min_value >= *value_range.start() && col_max_value <= *value_range.end() { // all values in the column are within the range. if column.index.get_cardinality() == Cardinality::Full { if boost != 1.0f32 { - return Ok(Box::new(ConstScorer::new( + return Ok(box_scorer(ConstScorer::new( AllScorer::new(column.num_docs()), boost, ))); } else { - return Ok(Box::new(AllScorer::new(column.num_docs()))); + return Ok(box_scorer(AllScorer::new(column.num_docs()))); } } else { // TODO Make it a field presence request for that specific column @@ -427,7 +428,7 @@ fn search_on_u64_ff( } let docset = RangeDocSet::new(value_range, column); - Ok(Box::new(ConstScorer::new(docset, boost))) + Ok(box_scorer(ConstScorer::new(docset, boost))) } /// Returns true if the type maps to a u64 fast field diff --git a/src/query/scorer.rs b/src/query/scorer.rs index b468f82de..c643c931e 100644 --- a/src/query/scorer.rs +++ b/src/query/scorer.rs @@ -1,4 +1,4 @@ -use std::mem::{ManuallyDrop, transmute_copy}; +use std::mem::{transmute_copy, ManuallyDrop}; use std::ops::DerefMut; use downcast_rs::impl_downcast; diff --git a/src/query/term_query/term_weight.rs b/src/query/term_query/term_weight.rs index a924e17af..78fc81f91 100644 --- a/src/query/term_query/term_weight.rs +++ b/src/query/term_query/term_weight.rs @@ -5,7 +5,7 @@ use crate::query::bm25::Bm25Weight; use crate::query::explanation::does_not_match; use crate::query::term_query::TermScorer; use crate::query::weight::{for_each_docset_buffered, for_each_scorer}; -use crate::query::{AllScorer, AllWeight, EmptyScorer, Explanation, Scorer, Weight}; +use crate::query::{box_scorer, AllScorer, AllWeight, EmptyScorer, Explanation, Scorer, Weight}; use crate::schema::IndexRecordOption; use crate::{DocId, Score, TantivyError, Term}; @@ -26,8 +26,8 @@ impl TermOrEmptyOrAllScorer { pub fn into_boxed_scorer(self) -> Box { match self { TermOrEmptyOrAllScorer::TermScorer(scorer) => scorer, - TermOrEmptyOrAllScorer::Empty => Box::new(EmptyScorer), - TermOrEmptyOrAllScorer::AllMatch(scorer) => Box::new(scorer), + TermOrEmptyOrAllScorer::Empty => box_scorer(EmptyScorer), + TermOrEmptyOrAllScorer::AllMatch(scorer) => box_scorer(scorer), } } }