summa-core 2.0.0

Core async search engine library with WASM support
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Shared constant-score execution for expanded term filters.

use super::Scorer;
use super::docset::{BitsetDocSet, DocSet, SortedVecDocSet};
use crate::segment::reader::ExpandedPosting;
#[cfg(test)]
use crate::structures::BlockPostingList;
use crate::structures::TERMINATED;
use crate::{DocId, Score};
use std::sync::Arc;

/// Complete constant-score membership, with lazy postings for ranked callers.
pub(super) struct TermUnionScorer {
    inner: UnionDocs,
    terminated: bool,
}

enum UnionDocs {
    Materialized(SortedVecDocSet),
    Streaming(PostingUnion),
    Bitmap(BitsetDocSet),
}

struct PostingUnion {
    lists: Vec<UnionCursor>,
    heads: std::collections::BinaryHeap<std::cmp::Reverse<(DocId, usize)>>,
    upper_count: u32,
}

// Most expanded terms never open for a small top-k. Keep their metadata inline
// to avoid an allocation per pending term; only active decode scratch is boxed.
#[allow(clippy::large_enum_variant)]
enum UnionCursor {
    Pending(crate::structures::postings::DeferredPosting),
    Inline(crate::structures::DecodedInlinePostings, usize),
    Active(Box<crate::structures::BlockPostingIterator<'static>>),
    Exhausted,
}

impl UnionCursor {
    fn seek(&mut self, target: DocId) -> DocId {
        if matches!(self, Self::Pending(_)) {
            let Self::Pending(posting) = std::mem::replace(self, Self::Exhausted) else {
                unreachable!()
            };
            let iterator = posting
                .into_list()
                .into_candidate_iterator(target, &mut Default::default());
            *self = Self::Active(Box::new(iterator));
        }
        match self {
            Self::Active(iterator) => iterator.seek(target),
            Self::Inline(postings, pos) => {
                *pos += postings.docs()[*pos..].partition_point(|&doc| doc < target);
                postings.docs().get(*pos).copied().unwrap_or(TERMINATED)
            }
            Self::Exhausted => TERMINATED,
            Self::Pending(_) => unreachable!(),
        }
    }
}

impl PostingUnion {
    fn new(postings: Vec<ExpandedPosting>, num_docs: u32) -> Self {
        let upper_count = postings
            .iter()
            .fold(0u32, |n, list| n.saturating_add(list.doc_count()))
            .min(num_docs);
        let mut lists = Vec::with_capacity(postings.len());
        let mut heads = Vec::with_capacity(postings.len());
        for posting in postings {
            let index = lists.len();
            let (first, cursor) = match posting {
                ExpandedPosting::Inline(postings) => (
                    postings.docs().first().copied(),
                    UnionCursor::Inline(postings, 0),
                ),
                ExpandedPosting::External(posting) => {
                    (posting.first_doc(), UnionCursor::Pending(posting))
                }
            };
            if let Some(first) = first {
                heads.push(std::cmp::Reverse((first, index)));
            }
            lists.push(cursor);
        }
        let mut union = Self {
            lists,
            heads: heads.into(),
            upper_count,
        };
        union.seek(0);
        union
    }

    fn doc(&self) -> DocId {
        self.heads.peek().map_or(TERMINATED, |head| head.0.0)
    }

    fn seek(&mut self, target: DocId) -> DocId {
        while let Some(mut head) = self.heads.peek_mut() {
            let std::cmp::Reverse((doc, index)) = *head;
            // Metadata orders unopened lists, but an actual decoded document
            // must establish the minimum before the collector sees it.
            if doc >= target && !matches!(self.lists[index], UnionCursor::Pending(_)) {
                break;
            }
            let next = self.lists[index].seek(target);
            if next == TERMINATED {
                std::collections::binary_heap::PeekMut::pop(head);
            } else {
                *head = std::cmp::Reverse((next, index));
            }
        }
        self.doc()
    }
}

impl TermUnionScorer {
    #[cfg(test)]
    pub(super) fn new(docs: Vec<u32>) -> Self {
        Self {
            inner: UnionDocs::Materialized(SortedVecDocSet::new(Arc::new(docs))),
            terminated: false,
        }
    }

    #[cfg(test)]
    pub(super) fn from_postings(
        postings: Vec<BlockPostingList>,
        num_docs: u32,
        map: Option<&crate::segment::chunk_map::ChunkMap>,
        limit: usize,
    ) -> Self {
        Self::from_expanded(
            postings
                .into_iter()
                .map(|list| {
                    ExpandedPosting::External(
                        crate::structures::postings::DeferredPosting::from_list(&list),
                    )
                })
                .collect(),
            num_docs,
            map,
            limit,
        )
    }

    pub(super) fn from_expanded(
        postings: Vec<ExpandedPosting>,
        num_docs: u32,
        map: Option<&crate::segment::chunk_map::ChunkMap>,
        limit: usize,
    ) -> Self {
        if map.is_none() && limit > 0 && limit < num_docs as usize {
            Self {
                inner: UnionDocs::Streaming(PostingUnion::new(postings, num_docs)),
                terminated: false,
            }
        } else {
            Self {
                inner: materialize_expanded(postings, num_docs, map),
                terminated: false,
            }
        }
    }
}

impl DocSet for TermUnionScorer {
    fn doc(&self) -> DocId {
        if self.terminated {
            return TERMINATED;
        }
        match &self.inner {
            UnionDocs::Materialized(inner) => inner.doc(),
            UnionDocs::Bitmap(inner) => inner.doc(),
            UnionDocs::Streaming(inner) => inner.doc(),
        }
    }

    fn advance(&mut self) -> DocId {
        if self.terminated {
            return TERMINATED;
        }
        match &mut self.inner {
            UnionDocs::Materialized(inner) => inner.advance(),
            UnionDocs::Bitmap(inner) => inner.advance(),
            UnionDocs::Streaming(inner) => inner.seek(inner.doc().saturating_add(1)),
        }
    }

    fn seek(&mut self, target: DocId) -> DocId {
        if self.terminated {
            return TERMINATED;
        }
        match &mut self.inner {
            UnionDocs::Materialized(inner) => inner.seek(target),
            UnionDocs::Bitmap(inner) => inner.seek(target),
            UnionDocs::Streaming(inner) => inner.seek(target),
        }
    }

    fn size_hint(&self) -> u32 {
        match &self.inner {
            UnionDocs::Materialized(inner) => inner.size_hint(),
            UnionDocs::Bitmap(inner) => inner.size_hint(),
            UnionDocs::Streaming(inner) => inner.upper_count,
        }
    }

    fn supports_doc_windows(&self) -> bool {
        matches!(self.inner, UnionDocs::Bitmap(_))
    }

    fn fill_doc_window(&mut self, base: DocId, bits: &mut super::docset::DocWindow) {
        if self.terminated {
            bits.fill(0);
            return;
        }
        match &mut self.inner {
            UnionDocs::Bitmap(inner) => inner.fill_doc_window(base, bits),
            _ => super::docset::fill_window(self, base, bits),
        }
    }

    fn supports_doc_batches(&self) -> bool {
        matches!(self.inner, UnionDocs::Materialized(_))
    }

    fn fill_doc_batch(&mut self, docs: &mut super::docset::DocBatch) -> usize {
        if self.terminated {
            return 0;
        }
        match &mut self.inner {
            UnionDocs::Materialized(inner) => inner.fill_doc_batch(docs),
            UnionDocs::Streaming(_) | UnionDocs::Bitmap(_) => super::docset::fill_batch(self, docs),
        }
    }
}

impl Scorer for TermUnionScorer {
    fn supports_filtered_windows(&self) -> bool {
        self.supports_doc_windows()
    }
    fn score(&self) -> Score {
        1.0
    }
    fn supports_candidate_score_bounds(&self) -> bool {
        true
    }
    fn candidate_score_upper_bound(&self) -> Score {
        1.0
    }
    fn advance_competitive_candidate(&mut self, minimum: Score, allow_equal: bool) -> DocId {
        // The ranked collector certifies that all later stable-ID ties lose.
        // Complete and nested callers use ordinary advance/seek instead.
        if minimum > 1.0 || (!allow_equal && minimum == 1.0) {
            self.terminated = true;
            TERMINATED
        } else {
            self.advance()
        }
    }
}

// ── Helpers ─────────────────────────────────────────────────────────────

/// Materialize a posting union using the smaller of two bounded scratch forms.
/// Narrow prefixes append/sort doc IDs; broad, overlapping prefixes use a
/// segment-sized bitset so duplicate postings cannot multiply memory.
#[cfg(test)]
pub(super) fn materialize_union(
    postings: &[BlockPostingList],
    num_docs: u32,
    map: Option<&crate::segment::chunk_map::ChunkMap>,
) -> Vec<u32> {
    let inner = materialize_expanded(
        postings
            .iter()
            .map(|list| {
                ExpandedPosting::External(crate::structures::postings::DeferredPosting::from_list(
                    list,
                ))
            })
            .collect::<Vec<_>>(),
        num_docs,
        map,
    );
    collect_union_for_test(inner)
}

#[cfg(test)]
fn collect_union_for_test(inner: UnionDocs) -> Vec<u32> {
    let mut scorer = TermUnionScorer {
        inner,
        terminated: false,
    };
    let mut docs = Vec::new();
    while scorer.doc() != TERMINATED {
        docs.push(scorer.doc());
        scorer.advance();
    }
    docs
}

fn materialize_expanded(
    postings: Vec<ExpandedPosting>,
    num_docs: u32,
    map: Option<&crate::segment::chunk_map::ChunkMap>,
) -> UnionDocs {
    let posting_count = postings.iter().fold(0usize, |sum, posting| {
        sum.saturating_add(posting.doc_count() as usize)
    });
    let posting_bytes = posting_count.saturating_mul(std::mem::size_of::<u32>());
    let bitset_bytes = (num_docs as usize)
        .div_ceil(64)
        .saturating_mul(std::mem::size_of::<u64>());

    if posting_bytes <= bitset_bytes {
        let mut docs = Vec::with_capacity(posting_count);
        for posting in postings {
            let mut append = |batch: &[u32]| {
                if let Some(map) = map {
                    docs.extend(batch.iter().map(|&doc| map.doc_id(doc)));
                } else {
                    docs.extend_from_slice(batch);
                }
                true
            };
            match posting {
                ExpandedPosting::Inline(postings) => {
                    append(postings.docs());
                }
                ExpandedPosting::External(postings) => postings
                    .into_list()
                    .iterator()
                    .visit_doc_ids_until(TERMINATED, append),
            }
        }
        docs.sort_unstable();
        docs.dedup();
        return UnionDocs::Materialized(SortedVecDocSet::new(Arc::new(docs)));
    }

    let mut bitset = super::DocBitset::new(num_docs);
    for posting in postings {
        let mut append = |batch: &[u32]| {
            for &doc in batch {
                bitset.set(map.map_or(doc, |map| map.doc_id(doc)));
            }
            true
        };
        match posting {
            ExpandedPosting::Inline(postings) => {
                append(postings.docs());
            }
            ExpandedPosting::External(postings) => postings
                .into_list()
                .iterator()
                .visit_doc_ids_until(TERMINATED, append),
        }
    }

    UnionDocs::Bitmap(BitsetDocSet::new(bitset))
}

/// Prefix unions materialise document-id sets; postings of a chunked field
/// are keyed by virtual chunk ids, so the union would filter the wrong
/// documents. Fail loudly instead of silently mis-matching.
pub(super) fn reject_chunked(
    reader: &crate::segment::SegmentReader,
    field: crate::Field,
    label: &str,
) -> crate::Result<()> {
    if reader.is_chunked_field(field) {
        return Err(crate::Error::Query(format!(
            "{label} is not supported on chunked text field '{}'; use a MatchQuery or PhraseQuery",
            reader.schema().get_field_name(field).unwrap_or("?")
        )));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mixed_inline_and_external_unions_preserve_membership_and_seek() {
        let mut list = crate::structures::PostingList::new();
        for doc in (0..1024).step_by(2) {
            list.push(doc, 1);
        }
        let external = BlockPostingList::from_posting_list(&list).unwrap();
        let make_inputs = || {
            vec![
                ExpandedPosting::Inline(
                    crate::structures::TermInfo::try_inline(&[1, 128, 1025], &[1, 2, 3])
                        .unwrap()
                        .decode_inline_fixed()
                        .unwrap(),
                ),
                ExpandedPosting::External(crate::structures::postings::DeferredPosting::from_list(
                    &external,
                )),
                ExpandedPosting::Inline(
                    crate::structures::TermInfo::try_inline(&[1, 3], &[2, 1])
                        .unwrap()
                        .decode_inline_fixed()
                        .unwrap(),
                ),
            ]
        };
        let mut expected: Vec<u32> = (0..1024).step_by(2).chain([1, 3, 128, 1025]).collect();
        expected.sort_unstable();
        expected.dedup();
        assert_eq!(
            collect_union_for_test(materialize_expanded(make_inputs(), 1026, None)),
            expected
        );
        let mut union = PostingUnion::new(make_inputs(), 1026);
        let mut actual = Vec::new();
        while union.doc() != TERMINATED {
            actual.push(union.doc());
            union.seek(union.doc() + 1);
        }
        assert_eq!(actual, expected);
        let mut union = PostingUnion::new(make_inputs(), 1026);
        for target in [0, 1, 3, 127, 128, 999, 1025, 1026] {
            assert_eq!(
                union.seek(target),
                expected
                    .iter()
                    .copied()
                    .find(|&doc| doc >= target)
                    .unwrap_or(TERMINATED)
            );
        }
    }

    #[test]
    fn materialized_union_batches_preserve_seeks_and_resume_after_partial_tail() {
        let expected: Vec<u32> = (0..1000).map(|doc| doc * 3).collect();
        let mut scorer = TermUnionScorer::new(expected.clone());
        assert!(scorer.supports_doc_batches());
        assert_eq!(scorer.seek(16), 18);
        let mut actual = Vec::new();
        let mut docs = [0; super::super::docset::DOC_BATCH_SIZE];
        loop {
            let count = scorer.fill_doc_batch(&mut docs);
            if count == 0 {
                break;
            }
            actual.extend_from_slice(&docs[..count]);
            assert_eq!(
                scorer.doc(),
                expected
                    .get(6 + actual.len())
                    .copied()
                    .unwrap_or(TERMINATED)
            );
        }
        assert_eq!(actual, expected[6..]);
    }

    #[test]
    fn ranked_union_opens_only_lists_that_can_supply_the_next_document() {
        let mut postings = Vec::new();
        for start in (0..64).rev().map(|i| i * 1024) {
            let mut list = crate::structures::PostingList::new();
            for doc in start..start + 512 {
                list.push(doc, 1);
            }
            postings.push(BlockPostingList::from_posting_list(&list).unwrap());
        }
        let expected = materialize_union(&postings, 65536, None);
        let mut union = PostingUnion::new(
            postings
                .into_iter()
                .map(|list| {
                    ExpandedPosting::External(
                        crate::structures::postings::DeferredPosting::from_list(&list),
                    )
                })
                .collect(),
            65536,
        );
        assert_eq!(union.doc(), 0);
        assert_eq!(
            union
                .lists
                .iter()
                .filter(|list| matches!(list, UnionCursor::Active(_)))
                .count(),
            1
        );
        for target in [1, 100, 511, 512, 2049, 65000, 65536] {
            assert_eq!(
                union.seek(target),
                expected
                    .iter()
                    .copied()
                    .find(|&doc| doc >= target)
                    .unwrap_or(TERMINATED)
            );
        }
    }

    #[test]
    fn lazy_union_keeps_complete_deduplicated_membership_across_seeks_and_block_edges() {
        let postings: Vec<_> = [2, 3, 7]
            .into_iter()
            .map(|stride| {
                let mut list = crate::structures::PostingList::new();
                for doc in (0..2049).step_by(stride) {
                    list.push(doc, 1);
                }
                BlockPostingList::from_posting_list(&list).unwrap()
            })
            .collect();
        let expected = materialize_union(&postings, 2049, None);
        let mut scorer = TermUnionScorer::from_postings(postings.clone(), 2049, None, 10);
        let mut actual = Vec::new();
        while scorer.doc() != TERMINATED {
            actual.push(scorer.doc());
            scorer.advance();
        }
        assert_eq!(actual, expected);
        let mut scorer = TermUnionScorer::from_postings(postings, 2049, None, 10);
        for target in [0, 1, 128, 511, 1200, 2001, 2048, 2050] {
            assert_eq!(
                scorer.seek(target),
                expected
                    .iter()
                    .copied()
                    .find(|&doc| doc >= target)
                    .unwrap_or(TERMINATED)
            );
        }
    }
}