lucene-rs 0.2.0

A Rust port of Apache Lucene's core: Lucene104 postings, BM25, block-max top-k search (MaxScore, block-max conjunction), segments, merges, analyzers and a query parser
Documentation
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! `IndexSearcher`: builds index-wide weights for a query, then scores each segment with the
//! fastest applicable scorer and collects the top hits.

use crate::codec::postings_reader::NO_MORE_DOCS;
use crate::document::Document;
use crate::error::{Error, Result};
use crate::index::doc_values::SortValue;
use crate::index::{DirectoryReader, LiveDocs, SegmentReader, Term};
use crate::search::bulk::{BlockMaxConjunctionBulkScorer, MaxScoreBulkScorer, score_term};
use crate::search::collector::TopScoreDocCollector;
use crate::search::query::{Occur, Query};
use crate::search::scorer::{
    BoxScorer, ConjunctionScorer, ConstantScorer, DisjunctionScorer, MatchAllScorer, PhraseScorer,
    ReqExclScorer, ReqOptScorer,
};
use crate::search::term_scorer::TermScorer;
use crate::sim::{Bm25, avg_field_length, idf, idf_sum};
use std::ops::Bound;

/// Exact hit counting stops after this many hits (Lucene's default for `search(query, n)`).
pub const TOTAL_HITS_THRESHOLD: u64 = 1000;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TotalHitsRelation {
    EqualTo,
    /// Counting stopped early because the remaining documents could not reach the top `n`.
    GreaterThanOrEqualTo,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TotalHits {
    pub value: u64,
    pub relation: TotalHitsRelation,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScoreDoc {
    /// Global document ID (valid for the reader that produced it).
    pub doc: u32,
    pub score: f32,
}

#[derive(Clone, Debug, PartialEq)]
pub struct TopDocs {
    pub total_hits: TotalHits,
    /// Best hits first; ties broken by lower doc ID.
    pub score_docs: Vec<ScoreDoc>,
}

/// A query prepared against index-wide statistics (Lucene's `Weight`).
pub(super) enum Weight {
    Range {
        field: String,
        lower: Bound<SortValue>,
        upper: Bound<SortValue>,
        score: f32,
    },
    /// `sim` is `None` when the term occurs nowhere in the index.
    Term {
        term: Term,
        sim: Option<Bm25>,
    },
    Boolean {
        clauses: Vec<(Self, Occur)>,
        min_should_match: usize,
    },
    Phrase {
        field: String,
        terms: Vec<(Vec<u8>, u32)>,
        sim: Option<Bm25>,
    },
    MatchAll {
        score: f32,
    },
    ConstantScore {
        inner: Box<Self>,
        score: f32,
    },
    MatchNone,
}

pub struct IndexSearcher {
    reader: DirectoryReader,
}

impl IndexSearcher {
    #[must_use]
    pub const fn new(reader: DirectoryReader) -> Self {
        Self { reader }
    }

    #[must_use]
    pub const fn reader(&self) -> &DirectoryReader {
        &self.reader
    }

    /// Stored fields of a hit.
    ///
    /// # Errors
    /// As for [`DirectoryReader::document`].
    pub fn doc(&self, doc: u32) -> Result<Document> {
        self.reader.document(doc)
    }

    pub(super) fn create_weight(&self, q: &Query, boost: f32) -> Weight {
        match q {
            Query::I64Range {
                field,
                lower,
                upper,
            } => Weight::Range {
                field: field.clone(),
                lower: lower.map(SortValue::I64),
                upper: upper.map(SortValue::I64),
                score: boost,
            },
            Query::F64Range {
                field,
                lower,
                upper,
            } => Weight::Range {
                field: field.clone(),
                lower: lower.map(SortValue::F64),
                upper: upper.map(SortValue::F64),
                score: boost,
            },
            Query::Term(term) => {
                let field = term.field.as_str();
                let sim = match (
                    self.reader.collection_stats(field),
                    self.reader.term_stats(field, &term.bytes),
                ) {
                    (Some(c), Some(t)) => Some(Bm25::new(
                        boost,
                        idf(t.doc_freq, c.doc_count),
                        avg_field_length(c.sum_total_term_freq, c.doc_count),
                    )),
                    _ => None,
                };
                Weight::Term {
                    term: term.clone(),
                    sim,
                }
            }
            Query::Boolean(b) => Weight::Boolean {
                clauses: b
                    .clauses
                    .iter()
                    .map(|(q, o)| {
                        let scoring = matches!(o, Occur::Must | Occur::Should);
                        (self.create_weight(q, if scoring { boost } else { 0.0 }), *o)
                    })
                    .collect(),
                min_should_match: b.minimum_should_match,
            },
            Query::Phrase(p) => {
                let c = self.reader.collection_stats(&p.field);
                let stats: Option<Vec<_>> = p
                    .terms
                    .iter()
                    .map(|(t, _)| self.reader.term_stats(&p.field, t))
                    .collect();
                let sim = match (c, stats) {
                    (Some(c), Some(stats)) => Some(Bm25::new(
                        boost,
                        idf_sum(stats.iter().map(|t| idf(t.doc_freq, c.doc_count))),
                        avg_field_length(c.sum_total_term_freq, c.doc_count),
                    )),
                    _ => None,
                };
                Weight::Phrase {
                    field: p.field.clone(),
                    terms: p.terms.clone(),
                    sim,
                }
            }
            Query::Boost(inner, b) => self.create_weight(inner, boost * b),
            Query::ConstantScore(inner) => Weight::ConstantScore {
                inner: Box::new(self.create_weight(inner, 0.0)),
                score: boost,
            },
            Query::MatchAll => Weight::MatchAll { score: boost },
            Query::MatchNone => Weight::MatchNone,
        }
    }

    /// The top `n` hits by score, plus the hit count (exact up to 1000 hits).
    ///
    /// # Errors
    /// [`Error::IllegalArgument`] for a phrase query on a field without positions.
    pub fn search(&self, query: &Query, n: usize) -> Result<TopDocs> {
        self.validate_query(query)?;
        let n = n.max(1);
        let weight = self.create_weight(&query.rewrite(), 1.0);
        let mut c = TopScoreDocCollector::new(n, TOTAL_HITS_THRESHOLD);
        for (seg, &base) in self.reader.segments().iter().zip(self.reader.doc_bases()) {
            c.begin_leaf(base);
            score_segment(&weight, seg, &mut c)?;
        }
        Ok(TopDocs {
            total_hits: TotalHits {
                value: c.total_hits,
                relation: if c.hits_lower_bound {
                    TotalHitsRelation::GreaterThanOrEqualTo
                } else {
                    TotalHitsRelation::EqualTo
                },
            },
            score_docs: c
                .top_docs()
                .into_iter()
                .filter_map(|(doc, score)| {
                    Some(ScoreDoc {
                        doc: u32::try_from(doc).ok()?,
                        score,
                    })
                })
                .collect(),
        })
    }

    /// Exact number of live documents matching `query`.
    ///
    /// # Errors
    /// As for [`IndexSearcher::search`].
    pub fn count(&self, query: &Query) -> Result<u64> {
        self.validate_query(query)?;
        let weight = self.create_weight(&query.rewrite(), 0.0);
        let mut total = 0u64;
        for seg in self.reader.segments() {
            if let Some(mut s) = scorer(&weight, seg)? {
                let live = seg.live_docs();
                loop {
                    let d = s.next_doc();
                    if d == NO_MORE_DOCS {
                        break;
                    }
                    total = total.saturating_add(u64::from(is_live(live, d)));
                }
            }
        }
        Ok(total)
    }
}

fn term_scorer<'a>(
    seg: &'a SegmentReader,
    term: &Term,
    sim: &Bm25,
    top_level: bool,
) -> Option<TermScorer<'a>> {
    let (field, meta) = seg.term_meta(&term.field, &term.bytes)?;
    let pe = seg.postings(&field, &meta, false);
    Some(TermScorer::new(
        pe,
        sim.clone(),
        seg.norms(field.number),
        top_level,
    ))
}

/// A clause that is a (possibly boosted, already folded into `sim`) term.
const fn as_term(w: &Weight) -> Option<(&Term, Option<&Bm25>)> {
    match w {
        Weight::Term { term, sim } => Some((term, sim.as_ref())),
        _ => None,
    }
}

/// Scores one segment, using Lucene's specialized bulk scorers where they apply.
fn score_segment(w: &Weight, seg: &SegmentReader, c: &mut TopScoreDocCollector) -> Result<()> {
    let live = seg.live_docs();
    match w {
        Weight::Term {
            term,
            sim: Some(sim),
        } => {
            if let Some(mut s) = term_scorer(seg, term, sim, true) {
                score_term(&mut s, c, live);
            }
            return Ok(());
        }
        Weight::Term { sim: None, .. } | Weight::MatchNone => return Ok(()),
        Weight::Phrase { field, terms, sim } => {
            if let Some(mut s) = phrase_scorer(seg, field, terms, sim.as_ref())? {
                s.score_top_k(c, live);
            }
            return Ok(());
        }
        Weight::Boolean {
            clauses,
            min_should_match,
        } if clauses.iter().all(|(w, _)| as_term(w).is_some()) => {
            let only = |o: &[Occur]| clauses.iter().all(|(_, x)| o.contains(x));
            let musts = clauses.iter().filter(|(_, o)| *o == Occur::Must).count();
            if only(&[Occur::Should]) && *min_should_match <= 1 {
                // MaxScoreBulkScorer over the terms present in this segment
                let scorers: Vec<TermScorer> = clauses
                    .iter()
                    .filter_map(|(w, _)| {
                        as_term(w).and_then(|(t, sim)| term_scorer(seg, t, sim?, false))
                    })
                    .collect();
                let mut scorers = scorers;
                if scorers.len() > 1 {
                    MaxScoreBulkScorer::new(scorers, live).score(c, 0, NO_MORE_DOCS);
                } else if let Some(mut s) = scorers.pop() {
                    // a single clause left in this segment is scored as a top-level term
                    s.set_top_level();
                    score_term(&mut s, c, live);
                }
                return Ok(());
            }
            if only(&[Occur::Must, Occur::Filter]) && musts >= 2 {
                let mut scorers = Vec::with_capacity(clauses.len());
                for (w, _) in clauses {
                    let Some((t, sim)) = as_term(w) else {
                        return Ok(());
                    };
                    match sim.and_then(|sim| term_scorer(seg, t, sim, false)) {
                        Some(s) => scorers.push(s),
                        None => return Ok(()), // a required term is missing: no matches here
                    }
                }
                BlockMaxConjunctionBulkScorer::new(scorers, live).score(c, 0, NO_MORE_DOCS);
                return Ok(());
            }
        }
        _ => {}
    }
    if let Some(mut s) = scorer(w, seg)? {
        loop {
            let d = s.next_doc();
            if d == NO_MORE_DOCS {
                break;
            }
            if is_live(live, d) {
                c.collect(d, s.score());
            }
        }
    }
    Ok(())
}

pub(super) fn is_live(live: Option<&LiveDocs>, doc: i32) -> bool {
    u32::try_from(doc).is_ok_and(|d| live.is_none_or(|l| l.get(d)))
}

/// At least `min_match` (and at least one) of `subs`; `None` if that cannot happen.
fn disjunction(mut subs: Vec<BoxScorer<'_>>, min_match: usize) -> Option<BoxScorer<'_>> {
    if subs.len() < min_match.max(1) {
        return None;
    }
    if subs.len() == 1 {
        return subs.pop();
    }
    Some(Box::new(DisjunctionScorer::new(subs, min_match)))
}

/// All of `subs` (flag: whether the clause scores); a lone scoring clause is used directly.
fn conjunction(mut subs: Vec<(BoxScorer<'_>, bool)>) -> Option<BoxScorer<'_>> {
    match subs.as_slice() {
        [] => None,
        [(_, true)] => subs.pop().map(|(s, _)| s),
        _ => Some(Box::new(ConjunctionScorer::new(subs))),
    }
}

/// Exact-phrase scorer for one segment, or `None` if some term is absent there.
fn phrase_scorer<'a>(
    seg: &'a SegmentReader,
    field: &str,
    terms: &[(Vec<u8>, u32)],
    sim: Option<&Bm25>,
) -> Result<Option<PhraseScorer<'a>>> {
    let Some(info) = seg.field_infos().get(field) else {
        return Ok(None);
    };
    if info.index_options.is_indexed_without_positions() {
        return Err(Error::IllegalArgument(format!(
            "field \"{field}\" was indexed without position data; cannot run a phrase query"
        )));
    }
    let Some(sim) = sim else { return Ok(None) };
    let mut postings = Vec::with_capacity(terms.len());
    for (t, pos) in terms {
        let Some((fi, meta)) = seg.term_meta(field, t) else {
            return Ok(None);
        };
        let pos = i32::try_from(*pos)
            .map_err(|_| Error::IllegalArgument("phrase position too large".into()))?;
        postings.push((seg.postings(&fi, &meta, true), pos));
    }
    Ok(Some(PhraseScorer::new(
        postings,
        sim.clone(),
        seg.norms(info.number),
    )))
}

/// A doc-at-a-time scorer for any weight, or `None` if nothing in the segment can match.
pub(super) fn scorer<'a>(w: &Weight, seg: &'a SegmentReader) -> Result<Option<BoxScorer<'a>>> {
    Ok(match w {
        Weight::Range {
            field,
            lower,
            upper,
            score,
        } => Some(Box::new(crate::search::scorer::RangeScorer::new(
            seg.range_docs(field, lower, upper),
            *score,
        ))),
        Weight::MatchNone => None,
        Weight::Term { term, sim } => sim
            .as_ref()
            .and_then(|sim| term_scorer(seg, term, sim, false))
            .map(|s| -> BoxScorer { Box::new(s) }),
        Weight::MatchAll { score } => Some(Box::new(MatchAllScorer::new(seg.max_doc(), *score))),
        Weight::ConstantScore { inner, score } => {
            scorer(inner, seg)?.map(|s| -> BoxScorer { Box::new(ConstantScorer::new(s, *score)) })
        }
        Weight::Phrase { field, terms, sim } => {
            phrase_scorer(seg, field, terms, sim.as_ref())?.map(|s| -> BoxScorer { Box::new(s) })
        }
        Weight::Boolean {
            clauses,
            min_should_match,
        } => {
            let msm = *min_should_match;
            let mut required: Vec<(BoxScorer, bool)> = Vec::new();
            let mut optional: Vec<BoxScorer> = Vec::new();
            let mut prohibited: Vec<BoxScorer> = Vec::new();
            for (cw, occur) in clauses {
                let s = scorer(cw, seg)?;
                match (occur, s) {
                    (Occur::Must | Occur::Filter, None) => return Ok(None),
                    (Occur::Must, Some(s)) => required.push((s, true)),
                    (Occur::Filter, Some(s)) => required.push((s, false)),
                    (Occur::Should, Some(s)) => optional.push(s),
                    (Occur::MustNot, Some(s)) => prohibited.push(s),
                    (_, None) => {}
                }
            }
            let main = if required.is_empty() {
                let Some(any) = disjunction(optional, msm) else {
                    return Ok(None);
                };
                any
            } else {
                if msm > 0 {
                    let Some(any) = disjunction(std::mem::take(&mut optional), msm) else {
                        return Ok(None);
                    };
                    required.push((any, true));
                }
                let Some(req) = conjunction(required) else {
                    return Ok(None);
                };
                match disjunction(optional, 1) {
                    Some(opt) => Box::new(ReqOptScorer::new(req, opt)),
                    None => req,
                }
            };
            Some(match disjunction(prohibited, 1) {
                Some(excl) => Box::new(ReqExclScorer::new(main, excl)),
                None => main,
            })
        }
    })
}