mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2025-12-23 02:29:57 +00:00
Fix explanation of boost queries seeking beyond query result. (#2142)
* Make current nightly Clippy happy. * Fix explanation of boost queries seeking beyond query result.
This commit is contained in:
@@ -64,10 +64,8 @@ fn mem_usage<T>(items: &Vec<T>) -> usize {
|
||||
|
||||
impl BlockedBitpacker {
|
||||
pub fn new() -> Self {
|
||||
let mut compressed_blocks = vec![];
|
||||
compressed_blocks.resize(8, 0);
|
||||
Self {
|
||||
compressed_blocks,
|
||||
compressed_blocks: vec![0; 8],
|
||||
buffer: vec![],
|
||||
offset_and_bits: vec![],
|
||||
}
|
||||
|
||||
@@ -221,5 +221,19 @@ fn main() -> tantivy::Result<()> {
|
||||
println!("{}", schema.to_json(&retrieved_doc));
|
||||
}
|
||||
|
||||
// We can also get an explanation to understand
|
||||
// how a found document got its score.
|
||||
let query = query_parser.parse_query("title:sea^20 body:whale^70")?;
|
||||
|
||||
let (_score, doc_address) = searcher
|
||||
.search(&query, &TopDocs::with_limit(1))?
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap();
|
||||
|
||||
let explanation = query.explain(&searcher, doc_address)?;
|
||||
|
||||
println!("{}", explanation.to_pretty_json());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::iter::once;
|
||||
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::character::complete::{
|
||||
@@ -35,11 +37,7 @@ fn field_name(i: &str) -> IResult<&str, String> {
|
||||
)),
|
||||
char(':'),
|
||||
),
|
||||
|(first_char, next)| {
|
||||
std::iter::once(first_char)
|
||||
.chain(next.into_iter())
|
||||
.collect()
|
||||
},
|
||||
|(first_char, next)| once(first_char).chain(next).collect(),
|
||||
)(i)
|
||||
}
|
||||
|
||||
@@ -822,8 +820,7 @@ fn aggregate_infallible_expressions(
|
||||
if let Some(last) = clauses.last_mut() {
|
||||
last.push((occur.or(Some(Occur::Must)), ast.clone()));
|
||||
} else {
|
||||
let mut last = Vec::new();
|
||||
last.push((occur.or(Some(Occur::Must)), ast.clone()));
|
||||
let last = vec![(occur.or(Some(Occur::Must)), ast.clone())];
|
||||
clauses.push(last);
|
||||
}
|
||||
}
|
||||
@@ -852,8 +849,7 @@ fn aggregate_infallible_expressions(
|
||||
if let Some(last) = clauses.last_mut() {
|
||||
last.push((last_occur.or(Some(Occur::Must)), last_ast));
|
||||
} else {
|
||||
let mut last = Vec::new();
|
||||
last.push((last_occur.or(Some(Occur::Must)), last_ast));
|
||||
let last = vec![(last_occur.or(Some(Occur::Must)), last_ast)];
|
||||
clauses.push(last);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ impl IndexingPositionsPerPath {
|
||||
fn get_position(&mut self, term: &Term) -> &mut IndexingPosition {
|
||||
self.positions_per_path
|
||||
.entry(murmurhash2(term.serialized_term()))
|
||||
.or_insert_with(Default::default)
|
||||
.or_default()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
|
||||
let sub_scorer: Box<dyn Scorer> = subweight.scorer(reader, boost)?;
|
||||
per_occur_scorers
|
||||
.entry(*occur)
|
||||
.or_insert_with(Vec::new)
|
||||
.or_default()
|
||||
.push(sub_scorer);
|
||||
}
|
||||
Ok(per_occur_scorers)
|
||||
@@ -193,7 +193,7 @@ impl<TScoreCombiner: ScoreCombiner + Sync> Weight for BooleanWeight<TScoreCombin
|
||||
return Ok(Explanation::new("BooleanQuery with no scoring", 1.0));
|
||||
}
|
||||
|
||||
let mut explanation = Explanation::new("BooleanClause. Sum of ...", scorer.score());
|
||||
let mut explanation = Explanation::new("BooleanClause. sum of ...", scorer.score());
|
||||
for (occur, subweight) in &self.weights {
|
||||
if is_positive_occur(*occur) {
|
||||
if let Ok(child_explanation) = subweight.explain(reader, doc) {
|
||||
|
||||
@@ -73,13 +73,9 @@ impl Weight for BoostWeight {
|
||||
}
|
||||
|
||||
fn explain(&self, reader: &SegmentReader, doc: u32) -> crate::Result<Explanation> {
|
||||
let mut scorer = self.scorer(reader, 1.0)?;
|
||||
if scorer.seek(doc) != doc {
|
||||
return Err(does_not_match(doc));
|
||||
}
|
||||
let mut explanation =
|
||||
Explanation::new(format!("Boost x{} of ...", self.boost), scorer.score());
|
||||
let underlying_explanation = self.weight.explain(reader, doc)?;
|
||||
let score = underlying_explanation.value() * self.boost;
|
||||
let mut explanation = Explanation::new(format!("Boost x{} of ...", self.boost), score);
|
||||
explanation.add_detail(underlying_explanation);
|
||||
Ok(explanation)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user