1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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,
}
}
}