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
//! Candidate addressing shared by every point-scoring vertical.

use super::SegmentReader;
use crate::dsl::{Field, FieldType};

use crate::{DocId, Error, Result};

#[derive(Clone, Copy, Debug)]
pub(crate) struct CandidateLocation {
    pub doc: DocId,
    pub ordinal: u16,
    /// Field-local ID; chunk/BMP physical IDs and flat-vector indexes need
    /// not agree even when they refer to the same logical document ordinal.
    pub physical: u32,
}

impl SegmentReader {
    /// Resolve every stored value of the selected documents in one field.
    /// The caller controls the expansion budget; no field silently truncates
    /// a long document or invents a body ordinal for document context.
    pub(crate) async fn candidate_locations(
        &self,
        field: Field,
        documents: &[DocId],
        max_locations: usize,
        budget: &mut super::SparseProbeBudget,
    ) -> Result<Vec<CandidateLocation>> {
        if documents.iter().any(|&doc| doc >= self.num_docs())
            || !documents.windows(2).all(|pair| pair[0] < pair[1])
        {
            return Err(Error::Query(
                "candidate documents must be valid, unique and sorted".into(),
            ));
        }
        if documents.is_empty() {
            return Ok(Vec::new());
        }
        let entry = self
            .schema
            .get_field_entry(field)
            .ok_or_else(|| Error::FieldNotFound(field.0.to_string()))?;
        let mut locations = Vec::with_capacity(documents.len().min(max_locations));
        let mut push = |doc, ordinal, physical| -> Result<()> {
            if locations.len() == max_locations {
                return Err(Error::Query(format!(
                    "candidate scoring expands beyond {max_locations} field values"
                )));
            }
            locations.push(CandidateLocation {
                doc,
                ordinal,
                physical,
            });
            Ok(())
        };
        match &entry.field_type {
            FieldType::Text if entry.chunked => {
                let Some(map) = self.chunk_map(field) else {
                    return Ok(locations);
                };
                if !map.has_logical_addressing() {
                    return Err(Error::Query("legacy reordered text needs explicit Reorder to upgrade its chunk map for L1".into()));
                }
                for &doc in documents {
                    for (ordinal, physical) in map.slots_for_document(doc) {
                        push(doc, ordinal, physical)?;
                    }
                }
            }
            FieldType::SparseVector => {
                if let Some(index) = self.seismic_index(field) {
                    for &doc in documents {
                        for physical in index.rows_for_document(doc) {
                            push(doc, index.key(physical).ordinal, physical)?;
                        }
                    }
                    return Ok(locations);
                }

                let Some(bmp) = self.bmp_indexes.get(&field.0) else {
                    if self.sparse_indexes.contains_key(&field.0) {
                        return self
                            .maxscore_candidate_locations(
                                field,
                                documents,
                                None,
                                max_locations,
                                budget,
                            )
                            .await;
                    }
                    return Ok(locations);
                };
                for &doc in documents {
                    if let Some(forward) = bmp.forward() {
                        for (ordinal, physical) in forward.for_document(doc) {
                            push(doc, ordinal, physical)?;
                        }
                    } else if bmp.logically_ordered() {
                        for (ordinal, physical) in bmp.ordered_slots_for_document(doc) {
                            push(doc, ordinal, physical)?;
                        }
                    } else {
                        return Err(Error::Query("reordered BMP backfill requires forward values; enable bmp_forward_index and explicitly reorder/rebuild, or disable L1 backfill".into()));
                    }
                }
            }
            FieldType::DenseVector | FieldType::BinaryDenseVector => {
                let Some(flat) = self.flat_vectors.get(&field.0) else {
                    if self.vector_indexes.contains_key(&field.0) {
                        return Err(Error::Query(format!(
                            "candidate scoring needs stored vectors for field {}",
                            entry.name
                        )));
                    }
                    return Ok(locations);
                };
                for &doc in documents {
                    let (start, count) = flat.flat_indexes_for_doc_range(doc);
                    for physical in start..start + count {
                        let (actual_doc, ordinal) = flat.get_doc_id(physical);
                        if actual_doc != doc {
                            return Err(Error::Corruption(
                                "flat vector lookup points to another document".into(),
                            ));
                        }
                        push(
                            doc,
                            ordinal,
                            u32::try_from(physical).map_err(|_| {
                                Error::Query("candidate flat vector address exceeds u32".into())
                            })?,
                        )?;
                    }
                }
            }
            FieldType::Text => {
                if self
                    .meta
                    .field_stats
                    .get(&field.0)
                    .is_none_or(|stats| stats.total_tokens == 0)
                {
                    return Ok(locations);
                }
                let lengths = self.doc_lengths(field).ok_or_else(|| {
                    Error::Query(format!(
                        "candidate scoring requires field-length metadata for plain text field {}",
                        entry.name
                    ))
                })?;
                for &doc in documents {
                    if lengths.length(doc) == 0 {
                        continue;
                    }
                    push(doc, 0, doc)?;
                }
            }
            other => {
                return Err(Error::Query(format!(
                    "candidate scoring does not support field type {other:?}"
                )));
            }
        }
        Ok(locations)
    }
}

impl SegmentReader {
    /// Capability diagnostics for legacy/reordered fields. Missing values are
    /// distinct from a field whose representation cannot support backfill.
    pub fn unprepared_candidate_fields(&self) -> Vec<String> {
        self.schema
            .fields()
            .filter_map(|(field, entry)| {
                let prepared = if let Some(map) = self.chunk_map(field) {
                    map.has_logical_addressing()
                } else if self.seismic_index(field).is_some() {
                    true
                } else if let Some(bmp) = self.bmp_indexes.get(&field.0) {
                    bmp.forward().is_some() || bmp.logically_ordered()
                } else {
                    !(self.vector_indexes.contains_key(&field.0)
                        && !self.flat_vectors.contains_key(&field.0))
                        && !(matches!(entry.field_type, FieldType::Text)
                            && !entry.chunked
                            && self
                                .meta
                                .field_stats
                                .get(&field.0)
                                .is_some_and(|stats| stats.total_tokens > 0)
                            && self.doc_lengths(field).is_none())
                };
                (!prepared).then(|| entry.name.clone())
            })
            .collect()
    }
}

impl SegmentReader {
    /// Resolve only nominated logical passages. Lookup costs depend on the
    /// candidate set, not on the number of chunks in a book.
    pub(crate) async fn candidate_passage_locations(
        &self,
        field: Field,
        targets: &[crate::segment::logical_address::LogicalUnit],
        budget: &mut super::SparseProbeBudget,
    ) -> Result<Vec<CandidateLocation>> {
        use crate::segment::logical_address::{LogicalUnit, ordered_slot_for_unit};
        if self.sparse_indexes.contains_key(&field.0) {
            let mut documents: Vec<_> = targets.iter().map(|t| t.doc).collect();
            documents.dedup();
            return self
                .maxscore_candidate_locations(
                    field,
                    &documents,
                    Some(targets),
                    targets.len(),
                    budget,
                )
                .await;
        }
        let mut locations = Vec::with_capacity(targets.len());
        for &target in targets {
            let physical = if let Some(map) = self.chunk_map(field) {
                if !map.has_logical_addressing() {
                    return Err(Error::Query("legacy reordered text needs explicit Reorder to upgrade its chunk map for L1".into()));
                }
                map.slot_for_unit(target)
            } else if let Some(index) = self.seismic_index(field) {
                index
                    .rows_for_document(target.doc)
                    .find(|&row| index.key(row).ordinal == target.ordinal)
            } else if let Some(bmp) = self.bmp_indexes.get(&field.0) {
                if let Some(forward) = bmp.forward() {
                    forward.find(target)
                } else if bmp.logically_ordered() {
                    ordered_slot_for_unit(bmp.num_virtual_docs, target, |physical| {
                        let (doc, ordinal) = bmp.virtual_to_doc(physical);
                        (doc != u32::MAX).then_some(LogicalUnit { doc, ordinal })
                    })
                } else {
                    return Err(Error::Query(
                        "reordered BMP backfill requires forward values; enable bmp_forward_index and explicitly reorder/rebuild, or disable L1 backfill"
                            .into(),
                    ));
                }
            } else if let Some(flat) = self.flat_vectors.get(&field.0) {
                let (start, count) = flat.flat_indexes_for_doc_range(target.doc);
                let mut low = start;
                let mut high = start + count;
                while low < high {
                    let mid = low + (high - low) / 2;
                    if flat.get_doc_id(mid).1 < target.ordinal {
                        low = mid + 1;
                    } else {
                        high = mid;
                    }
                }
                if low < start + count && flat.get_doc_id(low) == (target.doc, target.ordinal) {
                    Some(
                        u32::try_from(low)
                            .map_err(|_| Error::Query("flat vector address exceeds u32".into()))?,
                    )
                } else {
                    None
                }
            } else if self.vector_indexes.contains_key(&field.0) {
                return Err(Error::Query(
                    "candidate scoring needs stored flat vectors".into(),
                ));
            } else {
                None
            };
            if let Some(physical) = physical {
                let actual = if let Some(map) = self.chunk_map(field) {
                    map.resolve(physical)
                } else if let Some(index) = self.seismic_index(field) {
                    let key = index.key(physical);
                    (key.doc, key.ordinal)
                } else if let Some(bmp) = self.bmp_indexes.get(&field.0) {
                    if let Some(forward) = bmp.forward() {
                        let key = forward.key(physical);
                        (key.doc, key.ordinal)
                    } else {
                        bmp.virtual_to_doc(physical)
                    }
                } else {
                    self.flat_vectors[&field.0].get_doc_id(physical as usize)
                };
                if actual != (target.doc, target.ordinal) {
                    return Err(Error::Corruption(
                        "candidate lookup points to another passage".into(),
                    ));
                }
                locations.push(CandidateLocation {
                    doc: target.doc,
                    ordinal: target.ordinal,
                    physical,
                });
            }
        }
        Ok(locations)
    }
}

impl SegmentReader {
    async fn maxscore_candidate_locations(
        &self,
        field: Field,
        documents: &[DocId],
        targets: Option<&[crate::segment::logical_address::LogicalUnit]>,
        limit: usize,
        budget: &mut super::SparseProbeBudget,
    ) -> Result<Vec<CandidateLocation>> {
        use crate::segment::logical_address::LogicalUnit;
        let index = &self.sparse_indexes[&field.0];
        let mut units = std::collections::BTreeSet::new();
        index
            .probe_candidates(documents, None, budget, |doc, ordinal, _| {
                let key = LogicalUnit { doc, ordinal };
                if targets.is_some_and(|targets| targets.binary_search(&key).is_err()) {
                    return Ok(());
                }
                if !units.contains(&key) {
                    if units.len() == limit {
                        return Err(Error::Query(
                            "L1 sparse field-value expansion budget exceeded".into(),
                        ));
                    }
                    units.insert(key);
                }
                Ok(())
            })
            .await?;
        Ok(units
            .into_iter()
            .enumerate()
            .map(|(i, key)| CandidateLocation {
                doc: key.doc,
                ordinal: key.ordinal,
                physical: i as u32,
            })
            .collect())
    }
}

impl SegmentReader {
    /// Admit the selected BMP payload using metadata alone. Forward records
    /// are variable-sized; counting candidates does not bound their read work.
    pub(crate) fn reserve_candidate_bmp_reads(
        &self,
        field: Field,
        targets: &[u32],
        remaining: &mut u64,
    ) -> Result<()> {
        let bmp = self
            .bmp_index(field)
            .ok_or_else(|| Error::Corruption("L1 BMP locations lack a BMP index".into()))?;
        let mut previous_block = None;
        for &target in targets {
            let bytes = if let Some(forward) = bmp.forward() {
                forward.vector_byte_len(target)?
            } else {
                if target >= bmp.num_virtual_docs {
                    return Err(Error::Corruption("BMP candidate slot out of bounds".into()));
                }
                let block = target / bmp.bmp_block_size;
                if previous_block == Some(block) {
                    continue;
                }
                previous_block = Some(block);
                let (start, end) = bmp.block_data_range(block);
                end - start
            };
            *remaining = remaining.checked_sub(bytes).ok_or_else(|| {
                Error::Query("L1 text/BMP payload read budget exceeded (256 MiB)".into())
            })?;
        }
        Ok(())
    }

    /// Existing text readers return zero-copy views on mmap/RAM but materialize
    /// ranges on lazy backends. Admit those ranges before invoking the reader.
    pub(crate) async fn reserve_candidate_text_reads(
        &self,
        field: Field,
        term: &[u8],
        positions: bool,
        remaining: &mut u64,
    ) -> Result<()> {
        let lazy_postings = !self.postings.file().is_sync();
        let lazy_positions =
            positions && self.postings.positions_file().is_some_and(|h| !h.is_sync());
        if !lazy_postings && !lazy_positions {
            return Ok(());
        }
        let mut key = Vec::with_capacity(4 + term.len());
        key.extend_from_slice(&field.0.to_le_bytes());
        key.extend_from_slice(term);
        let Some(info) = self.term_dict.get(&key).await? else {
            return Ok(());
        };
        let posting_bytes = if lazy_postings {
            info.external_info().map_or(0, |(_, bytes)| bytes)
        } else {
            0
        };
        let position_bytes = if lazy_positions {
            info.position_info().map_or(0, |(_, bytes)| bytes)
        } else {
            0
        };
        *remaining = posting_bytes
            .checked_add(position_bytes)
            .and_then(|bytes| remaining.checked_sub(bytes))
            .ok_or_else(|| Error::Query("L1 lazy text read budget exceeded (256 MiB)".into()))?;
        Ok(())
    }
}

#[cfg(all(test, feature = "native"))]
mod tests {
    use super::*;
    #[tokio::test]
    async fn lazy_text_metadata_counts_and_backfill_admission_avoid_payload_reads() {
        use crate::directories::{FileHandle, RamDirectory};
        use crate::{Document, Index, IndexConfig, IndexWriter, Schema};
        let mut schema = Schema::builder();
        let field = schema.add_text_field_with_tokenizer("body", true, false, "simple");
        let schema = std::sync::Arc::new(schema.build());
        let dir = RamDirectory::new();
        let config = IndexConfig::default();
        let mut writer = IndexWriter::create(dir.clone(), (*schema).clone(), config.clone())
            .await
            .unwrap();
        for _ in 0..256 {
            let mut doc = Document::new();
            doc.add_text(field, "common term");
            writer.add_document(doc).unwrap();
        }
        writer.commit().await.unwrap();
        let index = Index::open(dir.clone(), config).await.unwrap();
        let searcher = index.reader().await.unwrap().searcher().await.unwrap();
        let id = crate::segment::SegmentId(searcher.segment_readers()[0].meta().id);
        let mut reader = SegmentReader::open(&dir, id, schema, 4).await.unwrap();
        reader.postings = crate::structures::postings::PostingListReader::new(
            FileHandle::lazy(
                reader.postings.file().len(),
                std::sync::Arc::new(|_| Box::pin(async { panic!("payload I/O before admission") })),
            ),
            reader.postings.positions_file().cloned(),
        );
        let error = reader
            .reserve_candidate_text_reads(field, b"common", false, &mut 0)
            .await
            .unwrap_err();
        assert!(error.to_string().contains("text read budget"));
        reader
            .reserve_candidate_text_reads(field, b"absent", false, &mut 0)
            .await
            .unwrap();
        // Statistics precede scoring admission, so they must read only the
        // dictionary even when a common term has an external posting list.
        assert_eq!(reader.text_doc_freq(field, b"common").await.unwrap(), 256);
        assert_eq!(reader.text_doc_freq(field, b"absent").await.unwrap(), 0);
        use crate::query::{CountCollector, Query, TermQuery, collect_segment};
        let required = crate::query::BooleanQuery::new()
            .must(TermQuery::text(field, "common"))
            .should(TermQuery::text(field, "term"))
            .should(TermQuery::text(field, "absent"));
        let mut count = CountCollector::new();
        collect_segment(&reader, &required, &mut count)
            .await
            .unwrap();
        assert_eq!(count.count(), 256);
        for (term, expected) in [("common", 256), ("absent", 0)] {
            let query = TermQuery::text(field, term);
            let mut count = CountCollector::new();
            collect_segment(&reader, &query, &mut count).await.unwrap();
            assert_eq!(count.count(), expected);
            assert_eq!(
                u64::from(query.count_estimate(&reader).await.unwrap()),
                expected
            );
        }
    }
}