From bccb87e96e44b73c6617daa8704eb0a089109b81 Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Fri, 10 Feb 2023 15:12:05 +0800 Subject: [PATCH] fix: auto downgrade index record option, instead of vint error Prev: thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: IoError(Custom { kind: InvalidData, error: "Reach end of buffer while reading VInt" })', src/main.rs:46:14 Now: Automatic downgrade to next available level --- src/core/inverted_index_reader.rs | 2 ++ src/query/term_query/term_query.rs | 5 +++-- src/schema/index_record_option.rs | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/core/inverted_index_reader.rs b/src/core/inverted_index_reader.rs index b986b74959..d9fe312666 100644 --- a/src/core/inverted_index_reader.rs +++ b/src/core/inverted_index_reader.rs @@ -135,6 +135,8 @@ impl InvertedIndexReader { term_info: &TermInfo, option: IndexRecordOption, ) -> io::Result { + let option = option.downgrade(self.record_option); + let block_postings = self.read_block_postings_from_terminfo(term_info, option)?; let position_reader = { if option.has_positions() { diff --git a/src/query/term_query/term_query.rs b/src/query/term_query/term_query.rs index 89b3a7e14e..32e0ee7cfd 100644 --- a/src/query/term_query/term_query.rs +++ b/src/query/term_query/term_query.rs @@ -104,14 +104,15 @@ impl TermQuery { } }; let scoring_enabled = enable_scoring.is_scoring_enabled(); - let index_record_option = if scoring_enabled { + let requested_index_record_option = if scoring_enabled { self.index_record_option } else { IndexRecordOption::Basic }; + Ok(TermWeight::new( self.term.clone(), - index_record_option, + requested_index_record_option, bm25_weight, scoring_enabled, )) diff --git a/src/schema/index_record_option.rs b/src/schema/index_record_option.rs index 9550549c56..e44638c320 100644 --- a/src/schema/index_record_option.rs +++ b/src/schema/index_record_option.rs @@ -1,5 +1,7 @@ use serde::{Deserialize, Serialize}; +use crate::TantivyError; + /// `IndexRecordOption` describes an amount information associated /// with a given indexed field. /// @@ -49,4 +51,17 @@ impl IndexRecordOption { IndexRecordOption::WithFreqsAndPositions => true, } } + + /// Downgrades to the next level if provided `IndexRecordOption` is unavailable. + pub fn downgrade(&self, other: IndexRecordOption) -> IndexRecordOption { + use IndexRecordOption::*; + + match (other, self) { + (WithFreqsAndPositions, WithFreqsAndPositions) => WithFreqsAndPositions, + (WithFreqs, WithFreqs) => WithFreqs, + (WithFreqsAndPositions, WithFreqs) => WithFreqs, + (WithFreqs, WithFreqsAndPositions) => WithFreqs, + _ => Basic, + } + } }