ld-lucivy 0.26.1

BM25 search engine with cross-token fuzzy matching, substring search, regex, and highlights
Documentation
use serde::{Deserialize, Serialize};

/// `IndexRecordOption` describes an amount information associated
/// with a given indexed field.
///
/// It is both used to:
///
///  * describe in the schema the amount of information that should be retained during indexing (See
///    [`TextFieldIndexing::set_index_option()`](crate::schema::TextFieldIndexing::set_index_option))
///  * request that a given amount of information to be decoded as one goes through a posting list.
///    (See [`InvertedIndexReader::read_postings()`](crate::InvertedIndexReader::read_postings))
#[derive(
    Clone, Copy, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize, Default,
)]
pub enum IndexRecordOption {
    /// records only the `DocId`s
    #[serde(rename = "basic")]
    #[default]
    Basic,
    /// records the document ids as well as the term frequency.
    /// The term frequency can help giving better scoring of the documents.
    #[serde(rename = "freq")]
    WithFreqs,
    /// records the document id, the term frequency and the positions of
    /// the occurrences in the document.
    /// Positions are required to run a [`PhraseQuery`](crate::query::PhraseQuery).
    #[serde(rename = "position")]
    WithFreqsAndPositions,
    /// records the document id, the term frequency, the positions and the
    /// character byte offsets (offset_from, offset_to) of each occurrence.
    /// Offsets enable highlighting and separator-aware matching without
    /// re-tokenizing the stored text.
    #[serde(rename = "offsets")]
    WithFreqsAndPositionsAndOffsets,
}

impl IndexRecordOption {
    /// Returns true if this option includes encoding
    /// term frequencies.
    pub fn has_freq(self) -> bool {
        match self {
            IndexRecordOption::Basic => false,
            IndexRecordOption::WithFreqs
            | IndexRecordOption::WithFreqsAndPositions
            | IndexRecordOption::WithFreqsAndPositionsAndOffsets => true,
        }
    }

    /// Returns true if this option include encoding
    ///  term positions.
    pub fn has_positions(self) -> bool {
        match self {
            IndexRecordOption::Basic | IndexRecordOption::WithFreqs => false,
            IndexRecordOption::WithFreqsAndPositions
            | IndexRecordOption::WithFreqsAndPositionsAndOffsets => true,
        }
    }

    /// Returns true if this option includes encoding
    /// character byte offsets (offset_from, offset_to).
    pub fn has_offsets(self) -> bool {
        match self {
            IndexRecordOption::Basic
            | IndexRecordOption::WithFreqs
            | IndexRecordOption::WithFreqsAndPositions => false,
            IndexRecordOption::WithFreqsAndPositionsAndOffsets => true,
        }
    }

    /// Downgrades to the next level if provided `IndexRecordOption` is unavailable.
    ///
    /// Returns the minimum of `self` and `other` according to the ordering:
    /// Basic < WithFreqs < WithFreqsAndPositions < WithFreqsAndPositionsAndOffsets
    pub fn downgrade(&self, other: IndexRecordOption) -> IndexRecordOption {
        use IndexRecordOption::*;

        match (other, self) {
            // Both have offsets
            (WithFreqsAndPositionsAndOffsets, WithFreqsAndPositionsAndOffsets) => {
                WithFreqsAndPositionsAndOffsets
            }
            // Offsets requested but only positions available → downgrade to positions
            (WithFreqsAndPositionsAndOffsets, WithFreqsAndPositions) => WithFreqsAndPositions,
            // Positions requested and at least positions available
            (WithFreqsAndPositions, WithFreqsAndPositions)
            | (WithFreqsAndPositions, WithFreqsAndPositionsAndOffsets) => WithFreqsAndPositions,
            // Offsets/positions requested but only freqs available
            (WithFreqsAndPositionsAndOffsets, WithFreqs)
            | (WithFreqsAndPositions, WithFreqs) => WithFreqs,
            // Freqs requested and at least freqs available
            (WithFreqs, WithFreqs)
            | (WithFreqs, WithFreqsAndPositions)
            | (WithFreqs, WithFreqsAndPositionsAndOffsets) => WithFreqs,
            // Everything else falls to Basic
            _ => Basic,
        }
    }
}