1#[cfg(feature = "sync")]
18use crate::DocId;
19use crate::dsl::Field;
20use crate::segment::SegmentReader;
21
22#[derive(Debug, Clone, Copy, PartialEq)]
24pub struct ProximityConfig {
25 pub weight: f32,
27 pub window: u32,
29}
30
31impl ProximityConfig {
32 pub const DEFAULT_WINDOW: u32 = 8;
34
35 pub fn new(weight: f32, window: u32) -> Self {
36 Self {
37 weight,
38 window: if window == 0 {
39 Self::DEFAULT_WINDOW
40 } else {
41 window
42 },
43 }
44 }
45
46 pub fn is_active(&self) -> bool {
47 self.weight > 0.0
48 }
49}
50
51pub(crate) const PROXIMITY_OVER_FETCH: usize = 4;
53
54#[cfg_attr(not(feature = "sync"), allow(dead_code))]
57pub(crate) fn count_windows(a: &[u32], b: &[u32], window: u32) -> (u32, u32) {
58 let mut ordered = 0u32;
59 let mut unordered = 0u32;
60 let (mut lo, mut hi, mut adj) = (0usize, 0usize, 0usize);
61 for &pa in a {
62 let low = pa.saturating_sub(window);
63 let high = pa.saturating_add(window);
64 while lo < b.len() && b[lo] < low {
65 lo += 1;
66 }
67 if hi < lo {
68 hi = lo;
69 }
70 while hi < b.len() && b[hi] <= high {
71 hi += 1;
72 }
73 unordered += (hi - lo) as u32;
74 while adj < b.len() && b[adj] < pa + 1 {
75 adj += 1;
76 }
77 if adj < b.len() && b[adj] == pa + 1 {
78 ordered += 1;
79 }
80 }
81 (ordered, unordered)
82}
83
84#[cfg(feature = "sync")]
87#[allow(clippy::too_many_arguments)]
88pub(crate) fn rescore_sync(
89 reader: &SegmentReader,
90 field: Field,
91 terms: &[(Vec<u8>, f32)],
92 params: super::Bm25Params,
93 lengths: Option<super::LengthSource<'_>>,
94 avg_len: f32,
95 config: ProximityConfig,
96 hits: &mut [super::ScoredDoc],
97) -> crate::Result<()> {
98 use crate::structures::TERMINATED;
99
100 if !config.is_active() || terms.len() < 2 || hits.is_empty() {
101 return Ok(());
102 }
103 let mut cursors = Vec::with_capacity(terms.len());
104 for (term, _) in terms {
105 let list = reader.get_postings_sync(field, term)?;
106 let positions = reader.get_positions_sync(field, term)?;
107 cursors.push(match (list, positions) {
108 (Some(list), Some(positions)) => Some((list.into_iterator(), positions.into_cursor())),
109 _ => None,
110 });
111 }
112 let mut order: Vec<usize> = (0..hits.len()).collect();
113 order.sort_unstable_by_key(|&i| hits[i].doc_id);
114 let mut bufs: Vec<Vec<u32>> = vec![Vec::new(); terms.len()];
115 let avg = avg_len.max(1.0);
116 for i in order {
117 let doc: DocId = hits[i].doc_id;
118 for (t, cursor) in cursors.iter_mut().enumerate() {
119 bufs[t].clear();
120 if let Some((it, positions)) = cursor
121 && it.doc() != TERMINATED
122 && it.seek(doc) == doc
123 {
124 positions.read_into(doc, it.position_cursor(), it.term_freq(), &mut bufs[t]);
125 }
126 }
127 let len = lengths
128 .map(|source| source.length(doc) as f32)
129 .filter(|len| *len > 0.0)
130 .unwrap_or(avg);
131 let mut bonus = 0.0f32;
132 for pair in 0..terms.len() - 1 {
133 let (a, b) = (&bufs[pair], &bufs[pair + 1]);
134 if a.is_empty() || b.is_empty() {
135 continue;
136 }
137 let (ordered, unordered) = count_windows(a, b, config.window);
138 let idf = (terms[pair].1 + terms[pair + 1].1) * 0.5;
139 if ordered > 0 {
140 bonus += params.score(ordered as f32, idf, len, avg);
141 }
142 if unordered > 0 {
143 bonus += 0.5 * params.score(unordered as f32, idf, len, avg);
144 }
145 }
146 hits[i].score += config.weight * bonus;
147 }
148 Ok(())
149}
150
151#[cfg(not(feature = "sync"))]
154#[allow(clippy::too_many_arguments)]
155pub(crate) fn rescore_sync(
156 _reader: &SegmentReader,
157 _field: Field,
158 _terms: &[(Vec<u8>, f32)],
159 _params: super::Bm25Params,
160 _lengths: Option<super::LengthSource<'_>>,
161 _avg_len: f32,
162 _config: ProximityConfig,
163 _hits: &mut [super::ScoredDoc],
164) -> crate::Result<()> {
165 log::debug!("proximity rescoring needs the `sync` feature; returning BM25 order");
166 Ok(())
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172
173 #[test]
174 fn windows_count_ordered_and_unordered_pairs() {
175 assert_eq!(count_windows(&[0, 10], &[1, 3, 12], 8), (1, 4));
178 assert_eq!(count_windows(&[0, 10], &[1, 3, 12], 1), (1, 1));
179 assert_eq!(count_windows(&[5], &[4], 2), (0, 1));
180 assert_eq!(count_windows(&[5], &[], 2), (0, 0));
181 assert_eq!(count_windows(&[0, 1, 2], &[1, 2, 3], 0), (3, 2));
184 }
185
186 #[test]
187 fn config_defaults_window() {
188 assert_eq!(ProximityConfig::new(1.0, 0).window, 8);
189 assert!(!ProximityConfig::new(0.0, 4).is_active());
190 }
191}