qql-embed 0.4.1

Shared dense and sparse embedding resolution for QQL runtimes
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Full BM25 text-processing pipeline, option-compatible with Qdrant.
//!
//! Qdrant's `Bm25Config` (REST) / `EdgeBm25Config` (edge) exposes, per sparse
//! vector: `k`, `b`, `avg_len`, `tokenizer`, `language`, `lowercase`,
//! `ascii_folding`, `stopwords`, `stemmer`, `min_token_len`, `max_token_len`.
//! [`Bm25TextConfig`] mirrors that surface with the same defaults (word
//! tokenizer, English, lowercase on, folding off, language stopwords/stemmer,
//! no length limits), and [`Bm25Pipeline`] executes it in Qdrant's exact
//! stage order: fold → lowercase → stopwords → stem → length check.
//!
//! Wire compatibility notes (all verified against Qdrant's implementation):
//! - Token IDs are Qdrant's `token_id` (murmur3-32 seed 0, `unsigned_abs`).
//! - The TF formula uses the same fused operation order as `lib/bm25`.
//! - `k1 = 0` is accepted (binary weighting), like Qdrant's validator.
//! - Stopword lists are ported verbatim from Qdrant's segment crate.
//! - Folding uses Qdrant's Lucene-derived mapping.
//! - `Multilingual` needs the `charabia` tokenizer and fails closed without
//!   it; Japanese falls back to generic segmentation (Qdrant uses a
//!   `vaporetto` model file we do not ship) — both are explicit errors, never
//!   silent degradation.

use std::borrow::Cow;
use std::collections::HashSet;
use std::sync::LazyLock;

use qql_core::error::QqlError;
use rust_stemmers::Algorithm;
use rust_stemmers::Stemmer as SnowballStemmer;

use super::bm25_fold::fold_to_ascii_cow;
use super::bm25_lang::Language;
use super::bm25_stopwords::stopwords_for;
use super::sparse::{Bm25Params, SparseVector, token_id};

fn config_error(message: String) -> QqlError {
    QqlError::validation("QQL-VALIDATION-CONFIG", message, None)
}

/// Tokenizer, mirroring Qdrant's `TokenizerType` names.
///
/// `Multilingual` parses but fails closed at build time: it needs the
/// `charabia`/`vaporetto` segmentation stack, which `qql-embed` deliberately
/// does not depend on (lean core for WASM/edge; no model files to ship).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Tokenizer {
    /// Split on non-alphanumeric boundaries (Qdrant default).
    #[default]
    Word,
    /// Split on Unicode whitespace only.
    Whitespace,
    /// Document side expands all n-grams; query side keeps the longest only.
    Prefix,
    /// Script-aware segmentation (requires tokenizer support not compiled in).
    Multilingual,
}

impl Tokenizer {
    /// Parse a Qdrant `tokenizer` name (`"word"`, `"whitespace"`, `"prefix"`,
    /// `"multilingual"`), ASCII-case-insensitively (superset of Qdrant's
    /// case-sensitive spelling, same accepted set). Anything else fails closed.
    pub fn parse(name: &str) -> Result<Self, QqlError> {
        match name.to_ascii_lowercase().as_str() {
            "word" => Ok(Self::Word),
            "whitespace" => Ok(Self::Whitespace),
            "prefix" => Ok(Self::Prefix),
            "multilingual" => Ok(Self::Multilingual),
            _ => Err(config_error(format!(
                "unsupported bm25 tokenizer: {name:?}"
            ))),
        }
    }

    /// Canonical Qdrant spelling.
    pub fn name(self) -> &'static str {
        match self {
            Self::Word => "word",
            Self::Whitespace => "whitespace",
            Self::Prefix => "prefix",
            Self::Multilingual => "multilingual",
        }
    }
}

/// Stemmer selection, mirroring Qdrant's `Option<StemmingAlgorithm>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stemmer {
    /// Snowball stemmer for an explicit language (Qdrant's `Snowball`).
    /// Covers Qdrant's `SnowballLanguage` set, including Armenian and Tamil
    /// (which have no `Language` variant and are only reachable this way —
    /// Qdrant has no stopword lists for them either).
    Snowball(Language),
    /// Armenian Snowball stemmer (explicit-only, like Qdrant).
    Armenian,
    /// Tamil Snowball stemmer (explicit-only, like Qdrant).
    Tamil,
    /// Explicitly no stemming (Qdrant's `{"type": "none"}`). Differs from
    /// leaving `stemmer` unset, which falls back to the language default.
    Disabled,
}

impl Stemmer {
    /// Parse `"none"` (disable), a language name/alias (its Snowball
    /// stemmer), or `"armenian"`/`"hy"`/`"tamil"`/`"ta"`. Anything else —
    /// including languages without any Snowball stemmer — fails closed.
    pub fn parse(name: &str) -> Result<Self, QqlError> {
        if name.eq_ignore_ascii_case("none") {
            return Ok(Self::Disabled);
        }
        let lower = name.to_ascii_lowercase();
        if lower == "armenian" || lower == "hy" {
            return Ok(Self::Armenian);
        }
        if lower == "tamil" || lower == "ta" {
            return Ok(Self::Tamil);
        }
        let language = Language::parse(name)?;
        if language.stem_algorithm().is_none() {
            return Err(config_error(format!(
                "bm25 stemmer unavailable for language {:?}: no Snowball stemmer (use \"none\" to disable)",
                language.name()
            )));
        }
        Ok(Self::Snowball(language))
    }

    /// The Snowball algorithm, if this selection stems at all.
    pub fn algorithm(self) -> Option<Algorithm> {
        match self {
            Self::Snowball(language) => language.stem_algorithm(),
            Self::Armenian => Some(Algorithm::Armenian),
            Self::Tamil => Some(Algorithm::Tamil),
            Self::Disabled => None,
        }
    }
}

/// Stopword selection, mirroring Qdrant's `StopwordsInterface`.
///
/// `None` (the `stopwords` field left unset) keeps the processing
/// language's list. `Some` **replaces** it: an empty [`Stopwords`] disables
/// filtering entirely (Qdrant's `Set` default), otherwise the listed
/// languages plus custom words are merged.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Stopwords {
    /// Additional language lists to merge.
    pub languages: Vec<Language>,
    /// Custom words to merge (compared post-normalization, like Qdrant,
    /// which lowercases entries when `lowercase` is on).
    pub custom: Vec<String>,
}

/// Full BM25 text-processing configuration — mirrors Qdrant's `Bm25Config`.
///
/// Construct via [`Bm25TextConfig::resolve`] (flat stringly options, the
/// single choke point every config surface calls) or struct-literal with
/// `..Default::default()`. Numeric parameters validate through
/// [`Bm25Params`]; names parse through [`Language`]/[`Tokenizer`]/[`Stemmer`].
#[derive(Debug, Clone, PartialEq)]
pub struct Bm25TextConfig {
    /// Validated `k1`/`b`/`avg_len`.
    pub params: Bm25Params,
    /// Tokenizer (default [`Tokenizer::Word`]).
    pub tokenizer: Tokenizer,
    /// Language for default stopwords/stemmer (default English, like Qdrant).
    pub language: Language,
    /// Lowercase before matching (default true, like Qdrant).
    pub lowercase: bool,
    /// Lucene ASCII folding before lowercasing (default false, like Qdrant).
    pub ascii_folding: bool,
    /// Stopword override (`None` = language default).
    pub stopwords: Option<Stopwords>,
    /// Stemmer override (`None` = language default).
    pub stemmer: Option<Stemmer>,
    /// Drop tokens shorter than this (chars, always enforced, like Qdrant).
    pub min_token_len: Option<usize>,
    /// Drop tokens longer than this (chars; on the document path, like
    /// Qdrant — the prefix query path truncates instead).
    pub max_token_len: Option<usize>,
}

impl Default for Bm25TextConfig {
    /// Qdrant server defaults: `k1 = 1.2`, `b = 0.75`, `avg_len = 256`,
    /// word tokenizer, English, lowercase on, folding off, language
    /// stopwords/stemmer, no length limits.
    fn default() -> Self {
        Self {
            params: Bm25Params::default(),
            tokenizer: Tokenizer::Word,
            language: Language::English,
            lowercase: true,
            ascii_folding: false,
            stopwords: None,
            stemmer: None,
            min_token_len: None,
            max_token_len: None,
        }
    }
}

impl Bm25TextConfig {
    /// Resolve flat options into a validated config. `None` keeps the
    /// corresponding default; invalid names or numbers fail closed with
    /// `QQL-VALIDATION-CONFIG`.
    ///
    /// - `stopwords`: `None` = language default; `Some(list)` **replaces**
    ///   it with exactly `list` (empty disables filtering). Use
    ///   `stopwords_languages` to merge additional language lists instead.
    /// - `stopwords_languages`: additional language lists merged with
    ///   `stopwords` (Qdrant `Set.languages`). An explicit selection
    ///   replaces the default, so include the processing language to keep
    ///   its words. Invalid names fail closed.
    /// - `stemmer`: `None` = language default; `Some("none")` disables;
    ///   `Some("<language>")` overrides.
    /// - `tokenizer`: `"multilingual"` parses here but fails at embed time
    ///   (see [`Tokenizer::Multilingual`]).
    #[allow(clippy::too_many_arguments)]
    pub fn resolve(
        k1: Option<f64>,
        b: Option<f64>,
        avg_len: Option<f64>,
        language: Option<&str>,
        tokenizer: Option<&str>,
        lowercase: Option<bool>,
        ascii_folding: Option<bool>,
        stopwords: Option<Vec<String>>,
        stemmer: Option<&str>,
        min_token_len: Option<usize>,
        max_token_len: Option<usize>,
        stopwords_languages: Option<Vec<String>>,
    ) -> Result<Self, QqlError> {
        let mut languages = Vec::new();
        if let Some(names) = stopwords_languages {
            for name in &names {
                languages.push(Language::parse(name)?);
            }
        }
        Ok(Self {
            params: Bm25Params::resolve(k1, b, avg_len)?,
            tokenizer: match tokenizer {
                None => Tokenizer::Word,
                Some(name) => Tokenizer::parse(name)?,
            },
            language: match language {
                None => Language::English,
                Some(name) => Language::parse(name)?,
            },
            lowercase: lowercase.unwrap_or(true),
            ascii_folding: ascii_folding.unwrap_or(false),
            stopwords: match (stopwords, languages.is_empty()) {
                // No override at all: the processing language's list.
                (None, true) => None,
                // Otherwise exactly the merged selection (Qdrant `Set`
                // semantics: an explicit list replaces the default, so
                // include the processing language to keep its words).
                (custom, _) => Some(Stopwords {
                    languages,
                    custom: custom.unwrap_or_default(),
                }),
            },
            stemmer: match stemmer {
                None => None,
                Some(name) => Some(Stemmer::parse(name)?),
            },
            min_token_len,
            max_token_len,
        })
    }

    /// Update text knobs over the current config: `None` (or empty names)
    /// keeps `self`'s value, explicit values replace it. Unlike
    /// [`Bm25TextConfig::resolve`], where `None` means "Qdrant default",
    /// this is the incremental-update path (WASM `setBm25Text`, REPL-style
    /// tuning): previously configured values survive untouched knobs.
    /// Names validate exactly like [`Bm25TextConfig::resolve`]. There is no
    /// reset-to-default signal: to restore defaults, resolve a fresh
    /// [`Bm25TextConfig::default`] instead of updating.
    #[allow(clippy::too_many_arguments)]
    pub fn with_text_options(
        &self,
        language: Option<&str>,
        tokenizer: Option<&str>,
        lowercase: Option<bool>,
        ascii_folding: Option<bool>,
        stopwords: Option<Vec<String>>,
        stemmer: Option<&str>,
        min_token_len: Option<usize>,
        max_token_len: Option<usize>,
        stopwords_languages: Option<Vec<String>>,
    ) -> Result<Self, QqlError> {
        let mut next = self.clone();
        if let Some(name) = language.filter(|s| !s.is_empty()) {
            next.language = Language::parse(name)?;
        }
        if let Some(name) = tokenizer.filter(|s| !s.is_empty()) {
            next.tokenizer = Tokenizer::parse(name)?;
        }
        if let Some(lowercase) = lowercase {
            next.lowercase = lowercase;
        }
        if let Some(ascii_folding) = ascii_folding {
            next.ascii_folding = ascii_folding;
        }
        if stopwords.is_some() || stopwords_languages.is_some() {
            let mut languages = Vec::new();
            if let Some(names) = stopwords_languages {
                for name in &names {
                    languages.push(Language::parse(name)?);
                }
            }
            next.stopwords = Some(Stopwords {
                languages,
                custom: stopwords.unwrap_or_default(),
            });
        }
        if let Some(name) = stemmer.filter(|s| !s.is_empty()) {
            next.stemmer = Some(Stemmer::parse(name)?);
        }
        if min_token_len.is_some() {
            next.min_token_len = min_token_len;
        }
        if max_token_len.is_some() {
            next.max_token_len = max_token_len;
        }
        Ok(next)
    }

    /// Compile into an executable pipeline. Infallible: names already
    /// validated at parse/resolve time; remaining choices are total.
    pub fn pipeline(&self) -> Bm25Pipeline {
        let stemmer = match self.stemmer {
            Some(Stemmer::Disabled) => None,
            Some(stemmer) => stemmer.algorithm().map(SnowballStemmer::create),
            None => self.language.stem_algorithm().map(SnowballStemmer::create),
        };
        // Mirror Qdrant's `StopwordsFilter`: entries are lowercased at build
        // time when `lowercase` is on, and matching runs post-normalization.
        let mut stopwords = HashSet::new();
        let mut insert = |word: &str| {
            if self.lowercase {
                stopwords.insert(word.to_lowercase());
            } else {
                stopwords.insert(word.to_string());
            }
        };
        match &self.stopwords {
            None => {
                for word in stopwords_for(self.language) {
                    insert(word);
                }
            }
            Some(selection) => {
                for language in &selection.languages {
                    for word in stopwords_for(*language) {
                        insert(word);
                    }
                }
                for word in &selection.custom {
                    insert(word.as_str());
                }
            }
        }
        Bm25Pipeline {
            params: self.params,
            tokenizer: self.tokenizer,
            lowercase: self.lowercase,
            ascii_folding: self.ascii_folding,
            stopwords,
            stemmer,
            min_token_len: self.min_token_len,
            max_token_len: self.max_token_len,
        }
    }
}

/// Compiled BM25 pipeline: build once per config, embed many texts.
///
/// Construct via [`Bm25TextConfig::pipeline`]. The default English pipeline
/// backing the [`crate::sparse`] free functions is shared process-wide.
pub struct Bm25Pipeline {
    params: Bm25Params,
    tokenizer: Tokenizer,
    lowercase: bool,
    ascii_folding: bool,
    stopwords: HashSet<String>,
    stemmer: Option<SnowballStemmer>,
    min_token_len: Option<usize>,
    max_token_len: Option<usize>,
}

impl Bm25Pipeline {
    /// Default text knobs with explicit numeric parameters.
    pub fn with_params(params: &Bm25Params) -> Self {
        Bm25TextConfig {
            params: *params,
            ..Bm25TextConfig::default()
        }
        .pipeline()
    }

    /// Qdrant's stage order: fold → lowercase → stopwords → stem → length.
    /// `check_max_len` is Qdrant's per-call flag: word/whitespace pass true
    /// on both paths; the prefix document path passes false (the n-gram loop
    /// bounds length instead); the prefix query path truncates afterwards.
    fn process_token<'a>(
        &self,
        raw: &'a str,
        is_query: bool,
        check_max_len: bool,
    ) -> Option<Cow<'a, str>> {
        if raw.is_empty() {
            return None;
        }
        let mut token: Cow<'a, str> = Cow::Borrowed(raw);
        if self.ascii_folding {
            token = fold_to_ascii_cow(token);
        }
        if self.lowercase {
            token = Cow::Owned(token.to_lowercase());
        }
        let prefix_query = is_query && self.tokenizer == Tokenizer::Prefix;
        if !prefix_query && self.stopwords.contains(token.as_ref()) {
            return None;
        }
        if let Some(stemmer) = self.stemmer.as_ref() {
            token = Cow::Owned(stemmer.stem(token.as_ref()).into_owned());
        }
        if self
            .min_token_len
            .is_some_and(|min| token.chars().count() < min)
        {
            return None;
        }
        if check_max_len
            && self
                .max_token_len
                .is_some_and(|max| token.chars().count() > max)
        {
            return None;
        }
        Some(token)
    }

    /// Iterate processed tokens (`is_query` selects the query path).
    fn for_each<F>(&self, text: &str, is_query: bool, mut f: F) -> Result<(), QqlError>
    where
        F: FnMut(&str),
    {
        match self.tokenizer {
            Tokenizer::Word => {
                for raw in text.split(|c: char| !c.is_alphanumeric()) {
                    if let Some(token) = self.process_token(raw, is_query, true) {
                        f(token.as_ref());
                    }
                }
            }
            Tokenizer::Whitespace => {
                for raw in text.split_whitespace() {
                    if let Some(token) = self.process_token(raw, is_query, true) {
                        f(token.as_ref());
                    }
                }
            }
            Tokenizer::Prefix => {
                if is_query {
                    self.for_each_prefix_query(text, &mut f);
                } else {
                    self.for_each_prefix_doc(text, &mut f);
                }
            }
            Tokenizer::Multilingual => {
                return Err(config_error(
                    "bm25 tokenizer \"multilingual\" needs script-aware segmentation (charabia/vaporetto), which is not compiled in; use \"word\" or \"whitespace\"".to_string(),
                ));
            }
        }
        Ok(())
    }

    /// Document path: expand every n-gram in `min..=max` (Qdrant's
    /// `PrefixTokenizer::tokenize`; `max` unbounded emits up to the full word
    /// and always emits the full word last). Note: `min_token_len = 0` emits
    /// a phantom empty token (`nth(0)`), exactly like Qdrant's own
    /// implementation — shared quirk kept for parity, not fixed.
    fn for_each_prefix_doc<F>(&self, text: &str, mut f: F)
    where
        F: FnMut(&str),
    {
        let min_ngram = self.min_token_len.unwrap_or(1);
        let max_ngram = self.max_token_len.unwrap_or(usize::MAX);
        for raw in text.split(|c: char| !c.is_alphanumeric()) {
            let Some(word) = self.process_token(raw, false, false) else {
                continue;
            };
            for n in min_ngram..=max_ngram {
                match word.char_indices().map(|(i, _)| i).nth(n) {
                    Some(end) => f(&word[..end]),
                    None => {
                        f(word.as_ref());
                        break;
                    }
                }
            }
        }
    }

    /// Query path: longest n-gram only, no stopwords (Qdrant's
    /// `PrefixTokenizer::tokenize_query`).
    fn for_each_prefix_query<F>(&self, text: &str, mut f: F)
    where
        F: FnMut(&str),
    {
        let max_ngram = self.max_token_len.unwrap_or(usize::MAX);
        for raw in text.split(|c: char| !c.is_alphanumeric()) {
            if raw.is_empty() {
                continue;
            }
            // No stopwords and no max-as-filter here: over-long words
            // truncate to `max_ngram` below instead of dropping.
            let Some(word) = self.process_token(raw, true, false) else {
                continue;
            };
            match word.char_indices().map(|(i, _)| i).nth(max_ngram) {
                Some(end) => f(&word[..end]),
                None => f(word.as_ref()),
            }
        }
    }

    /// Processed document tokens (post-pipeline, in order, duplicates kept).
    /// Used by the `avg_len` estimator so the estimate measures exactly the
    /// `doc_len` the TF formula consumes.
    pub fn doc_tokens(&self, text: &str) -> Result<Vec<String>, QqlError> {
        let mut tokens = Vec::new();
        self.for_each(text, false, |token| {
            tokens.push(token.to_string());
        })?;
        Ok(tokens)
    }

    /// Processed query tokens (query path: prefix keeps the longest n-gram
    /// only and skips stopwords; other tokenizers match the document path).
    pub(crate) fn for_each_query<F>(&self, text: &str, f: F) -> Result<(), QqlError>
    where
        F: FnMut(&str),
    {
        self.for_each(text, true, f)
    }

    /// Post-pipeline token count of one document (the formula's `doc_len`).
    pub fn token_count(&self, text: &str) -> Result<usize, QqlError> {
        let mut count = 0;
        self.for_each(text, false, |_| {
            count += 1;
        })?;
        Ok(count)
    }

    /// Embed query text: unique token IDs (sorted) with unit weights —
    /// identical to Qdrant's `qdrant/bm25` query embedding.
    pub fn embed_query(&self, text: &str) -> Result<SparseVector, QqlError> {
        let mut indices = Vec::with_capacity(text.len() / 6 + 1);
        self.for_each_query(text, |token| {
            indices.push(token_id(token));
        })?;
        if indices.is_empty() {
            return Ok(SparseVector::default());
        }
        indices.sort_unstable();
        indices.dedup();
        let values = vec![1.0; indices.len()];
        Ok(SparseVector { indices, values })
    }

    /// Embed document text with this pipeline's validated [`Bm25Params`].
    ///
    /// Frequencies count per token ID: on the rare murmur3 collision two
    /// terms merge into one dimension with summed counts, keeping output
    /// deterministic (the server counts per string, so collided IDs carry no
    /// cross-implementation contract — same caveat as before).
    pub fn embed_document(&self, text: &str) -> Result<SparseVector, QqlError> {
        self.embed_document_with(
            text,
            self.params.k1(),
            self.params.b(),
            self.params.avg_len(),
        )
    }

    /// Embed with explicit parameters, used as given (no validation — the
    /// caller sanitizes, mirroring [`crate::sparse::embed_document_with`]).
    /// The formula is the same fused op order as [`Bm25Pipeline::embed_document`].
    pub(crate) fn embed_document_with(
        &self,
        text: &str,
        k1: f64,
        b: f64,
        avgdl: f64,
    ) -> Result<SparseVector, QqlError> {
        let mut token_ids: Vec<u32> = Vec::with_capacity(text.len() / 6 + 1);
        self.for_each(text, false, |token| {
            token_ids.push(token_id(token));
        })?;
        if token_ids.is_empty() {
            return Ok(SparseVector::default());
        }
        let doc_len = token_ids.len() as f64;
        // Same fused operation order as Qdrant `lib/bm25`, so weights agree
        // bit-for-bit absent murmur3 collisions — not just algebraically:
        // `n * (k1 + 1)` over `k1.mul_add(1 - b + b * doc_len / avgdl, n)`.
        // (On a collision Qdrant counts per string and overwrites while we
        // count per ID and sum, so collided IDs carry no cross-impl contract
        // either way — same caveat as the server documents.)
        let k1p1 = k1 + 1.0;
        let norm = 1.0 - b + b * doc_len / avgdl;
        token_ids.sort_unstable();
        let mut indices = Vec::with_capacity(token_ids.len());
        let mut values = Vec::with_capacity(token_ids.len());
        let mut i = 0;
        while i < token_ids.len() {
            let id = token_ids[i];
            let mut count = 1u32;
            while i + 1 < token_ids.len() && token_ids[i + 1] == id {
                count += 1;
                i += 1;
            }
            indices.push(id);
            let n = count as f64;
            values.push((n * k1p1 / k1.mul_add(norm, n)) as f32);
            i += 1;
        }
        Ok(SparseVector { indices, values })
    }
}

static DEFAULT_PIPELINE: LazyLock<Bm25Pipeline> =
    LazyLock::new(|| Bm25TextConfig::default().pipeline());

/// Default English pipeline backing the [`crate::sparse`] free functions.
pub fn default_pipeline() -> &'static Bm25Pipeline {
    &DEFAULT_PIPELINE
}

/// Mean post-pipeline token count over sampled document texts — the
/// estimator for a corpus-true `avg_len`.
///
/// "Real data" in one function: pass the actual field texts (e.g. from a
/// `SCROLL` sample) and get the average `doc_len` the TF formula consumes,
/// measured with the same pipeline that will embed the writes. Returns
/// `None` when the sample holds no documents or no tokens at all (an empty
/// corpus has no meaningful average — keep the default instead of
/// dividing by zero or storing `avg_len = 0`, which validation rejects).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AvgLenEstimate {
    /// Mean post-pipeline tokens per document (`> 0` when returned).
    pub mean: f64,
    /// Documents measured.
    pub docs: usize,
}

/// Estimate a corpus-true `avg_len` from sampled document texts.
///
/// Measures each text with `pipeline.token_count` (the same `doc_len` the
/// TF formula consumes) and returns the mean. Returns `None` when the
/// sample holds no documents or no tokens at all — an empty corpus has no
/// meaningful average, so callers should keep the default instead of
/// storing `avg_len = 0` (which [`Bm25Params`] validation rejects).
pub fn estimate_avg_len<'a, I>(
    texts: I,
    pipeline: &Bm25Pipeline,
) -> Result<Option<AvgLenEstimate>, QqlError>
where
    I: IntoIterator<Item = &'a str>,
{
    let mut docs = 0usize;
    let mut total = 0usize;
    for text in texts {
        docs += 1;
        total += pipeline.token_count(text)?;
    }
    if docs == 0 || total == 0 {
        return Ok(None);
    }
    Ok(Some(AvgLenEstimate {
        mean: total as f64 / docs as f64,
        docs,
    }))
}