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
//! Tests for the full BM25 text pipeline: languages, tokenizers, folding,
//! length limits, estimator, and option resolution. Behavioral expectations
//! mirror Qdrant's own `EdgeBm25` tests where applicable.

use crate::bm25_lang::Language;
use crate::bm25_text::{AvgLenEstimate, Bm25Pipeline, Bm25TextConfig, Stemmer, Tokenizer};
use crate::sparse;

fn plain(language: &str) -> Bm25Pipeline {
    Bm25TextConfig::resolve(
        None,
        None,
        None,
        Some(language),
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("valid language")
    .pipeline()
}

#[test]
fn language_parse_accepts_names_and_aliases() {
    assert_eq!(Language::parse("spanish").unwrap(), Language::Spanish);
    assert_eq!(Language::parse("es").unwrap(), Language::Spanish);
    assert_eq!(Language::parse("ES").unwrap(), Language::Spanish);
    assert_eq!(Language::parse("Chinese").unwrap(), Language::Chinese);
    assert_eq!(Language::parse("zh").unwrap(), Language::Chinese);
    assert_eq!(Language::parse("hi-en").unwrap(), Language::Hinglish);
    assert_eq!(Language::parse("english").unwrap(), Language::English);
    let err = Language::parse("klingon").expect_err("unknown language rejects");
    assert_eq!(err.code, "QQL-VALIDATION-CONFIG");
    let err = Language::parse("armenian").expect_err("no such processing language");
    assert_eq!(err.code, "QQL-VALIDATION-CONFIG");
}

#[test]
fn stemmer_coverage_matches_qdrant() {
    // Qdrant's 17 Snowball-via-language set: every other language has
    // no default stemmer (tokens pass through, stopwords still apply).
    // Armenian and Tamil round out Qdrant's 19 Snowball languages as
    // explicit-stemmer-only (no Language variant, no stopword lists —
    // reachable via Stemmer::parse, like Qdrant's explicit config).
    let stemmed = [
        "arabic",
        "danish",
        "dutch",
        "english",
        "finnish",
        "french",
        "german",
        "greek",
        "hungarian",
        "italian",
        "norwegian",
        "portuguese",
        "romanian",
        "russian",
        "spanish",
        "swedish",
        "turkish",
    ];
    for name in stemmed {
        assert!(
            Language::parse(name).unwrap().stem_algorithm().is_some(),
            "{name} must have a stemmer"
        );
    }
    let unstemmed = [
        "azerbaijani",
        "basque",
        "bengali",
        "catalan",
        "chinese",
        "hebrew",
        "hinglish",
        "indonesian",
        "japanese",
        "kazakh",
        "nepali",
        "slovene",
        "tajik",
    ];
    for name in unstemmed {
        assert!(
            Language::parse(name).unwrap().stem_algorithm().is_none(),
            "{name} must have no default stemmer"
        );
    }
    assert_eq!(
        stemmed.len() + unstemmed.len(),
        30,
        "every language must be classified"
    );

    // Explicit-only Snowball languages (Qdrant's SnowballLanguage set minus
    // the 17 language-reachable ones): parseable as stemmers, not languages.
    for (name, stemmer) in [
        ("armenian", Stemmer::Armenian),
        ("hy", Stemmer::Armenian),
        ("tamil", Stemmer::Tamil),
        ("ta", Stemmer::Tamil),
    ] {
        assert_eq!(Stemmer::parse(name).expect("explicit stemmer"), stemmer);
    }
    assert!(Language::parse("armenian").is_err());
    assert!(Language::parse("tamil").is_err());
}

#[test]
fn tokenizer_parse_round_trips() {
    for (name, tokenizer) in [
        ("word", Tokenizer::Word),
        ("whitespace", Tokenizer::Whitespace),
        ("prefix", Tokenizer::Prefix),
        ("multilingual", Tokenizer::Multilingual),
    ] {
        assert_eq!(Tokenizer::parse(name).unwrap(), tokenizer);
        assert_eq!(tokenizer.name(), name);
    }
    let err = Tokenizer::parse("ngram").expect_err("unknown tokenizer rejects");
    assert_eq!(err.code, "QQL-VALIDATION-CONFIG");
}

#[test]
fn spanish_pipeline_stems_and_filters() {
    let pipe = plain("spanish");
    // "la" is a Spanish stopword; "casa" stems to "cas".
    assert_eq!(pipe.doc_tokens("la casa").unwrap(), vec!["cas"]);
    // Same text through English keeps different content ("la" is not English).
    let english = plain("english");
    assert_ne!(
        pipe.embed_document("la casa").unwrap().indices,
        english.embed_document("la casa").unwrap().indices
    );
}

#[test]
fn disabled_stemmer_keeps_inflections_distinct() {
    // Mirrors Qdrant's own `disabled_stemmer_keeps_inflections_distinct`.
    let stemmed = plain("english");
    assert_eq!(
        stemmed.embed_document("running run").unwrap().indices.len(),
        1
    );
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some(Vec::new()),
        Some("none"),
        None,
        None,
        None,
    )
    .expect("valid");
    let unstemmed = config.pipeline();
    assert_eq!(
        unstemmed
            .embed_document("running run")
            .unwrap()
            .indices
            .len(),
        2
    );
}

#[test]
fn custom_stopwords_replace_the_default() {
    // `Some(vec![])` disables filtering: "the" survives.
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some(Vec::new()),
        None,
        None,
        None,
        None,
    )
    .expect("valid");
    let tokens = config.pipeline().doc_tokens("the cat").unwrap();
    assert!(tokens.contains(&"the".to_string()), "got {tokens:?}");
    // A custom list filters exactly its words (post-normalization).
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some(vec!["cat".to_string()]),
        Some("none"),
        None,
        None,
        None,
    )
    .expect("valid");
    assert_eq!(
        config.pipeline().doc_tokens("the cat").unwrap(),
        vec!["the"]
    );
}

#[test]
fn ascii_folding_normalizes_before_lowercase() {
    let folded = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        Some(true),
        None,
        Some("none"),
        None,
        None,
        None,
    )
    .expect("valid")
    .pipeline();
    let plain_pipe = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some("none"),
        None,
        None,
        None,
    )
    .expect("valid")
    .pipeline();
    assert_eq!(folded.doc_tokens("café").unwrap(), vec!["cafe"]);
    assert_eq!(
        folded.embed_query("café").unwrap().indices,
        folded.embed_query("cafe").unwrap().indices
    );
    assert_ne!(
        plain_pipe.embed_query("café").unwrap().indices,
        plain_pipe.embed_query("cafe").unwrap().indices
    );
}

#[test]
fn lowercase_off_keeps_case_distinct() {
    let cased = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        Some(false),
        None,
        None,
        Some("none"),
        None,
        None,
        None,
    )
    .expect("valid")
    .pipeline();
    assert_ne!(
        cased.embed_query("Hello").unwrap().indices,
        cased.embed_query("hello").unwrap().indices
    );
    let lowered = plain("english");
    assert_eq!(
        lowered.embed_query("Hello").unwrap().indices,
        lowered.embed_query("hello").unwrap().indices
    );
}

#[test]
fn token_length_limits_apply_in_chars() {
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some("none"),
        Some(4),
        Some(4),
        None,
    )
    .expect("valid");
    assert_eq!(
        config.pipeline().doc_tokens("a bb ccc dddd eeeee").unwrap(),
        vec!["dddd"]
    );
}

#[test]
fn whitespace_tokenizer_keeps_hyphenated_forms() {
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        Some("whitespace"),
        None,
        None,
        None,
        Some("none"),
        None,
        None,
        None,
    )
    .expect("valid");
    assert_eq!(
        config.pipeline().doc_tokens("hello-world").unwrap(),
        vec!["hello-world"]
    );
    // Word splitting breaks it apart (modulo stopwords/stemming on pieces).
    assert!(
        plain("english")
            .doc_tokens("hello-world")
            .unwrap()
            .iter()
            .all(|t| t != "hello-world")
    );
}

#[test]
fn prefix_tokenizer_expands_docs_and_truncates_queries() {
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        Some("prefix"),
        None,
        None,
        None,
        Some("none"),
        Some(2),
        None,
        None,
    )
    .expect("valid");
    let pipe = config.pipeline();
    assert_eq!(
        pipe.doc_tokens("hello").unwrap(),
        vec!["he", "hel", "hell", "hello"]
    );
    // Short words still emit once (full word + break).
    assert_eq!(pipe.doc_tokens("hi").unwrap(), vec!["hi"]);
    // Query keeps the longest n-gram only.
    let mut query_tokens = Vec::new();
    pipe.for_each_query("hello", |t: &str| query_tokens.push(t.to_string()))
        .expect("query tokens");
    assert_eq!(query_tokens, vec!["hello"]);
    // With a max cap the query truncates.
    let capped = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        Some("prefix"),
        None,
        None,
        None,
        Some("none"),
        Some(2),
        Some(3),
        None,
    )
    .expect("valid")
    .pipeline();
    assert_eq!(capped.doc_tokens("hello").unwrap(), vec!["he", "hel"]);
    let mut capped_query = Vec::new();
    capped
        .for_each_query("hello", |t: &str| capped_query.push(t.to_string()))
        .expect("query tokens");
    assert_eq!(capped_query, vec!["hel"]);
}

#[test]
fn multilingual_fails_closed() {
    let pipe = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        Some("multilingual"),
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("multilingual parses")
    .pipeline();
    for result in [
        pipe.embed_query("hello"),
        pipe.embed_document("hello"),
        pipe.token_count("hello")
            .map(|_| crate::sparse::SparseVector::default()),
    ] {
        let err = result.expect_err("multilingual must fail closed");
        assert_eq!(err.code, "QQL-VALIDATION-CONFIG");
    }
}

#[test]
fn k1_zero_gives_binary_weighting() {
    let params = sparse::Bm25Params::new(0.0, 0.75, 256.0).expect("k1 = 0 valid");
    let vector = sparse::embed_document_with_params("cat sat mat cat", &params);
    assert!(!vector.indices.is_empty());
    assert!(
        vector.values.iter().all(|&v| v == 1.0),
        "k1 = 0 must saturate every term to 1.0, got {:?}",
        vector.values
    );
}

#[test]
fn tf_formula_matches_qdrant_reference() {
    // Qdrant `lib/bm25` reference: 5 tokens, "the" 2x; k1=1.2, b=0.75,
    // avg_len=5 → tf = 2*2.2 / (1.2*(0.25+0.75*1) + 2) = 1.375. Whitespace
    // tokens with processing disabled reproduce their exact input.
    let config = Bm25TextConfig {
        params: sparse::Bm25Params::new(1.2, 0.75, 5.0).expect("valid"),
        tokenizer: Tokenizer::Whitespace,
        stopwords: Some(crate::bm25_text::Stopwords::default()),
        stemmer: Some(Stemmer::Disabled),
        ..Bm25TextConfig::default()
    };
    let vector = config
        .pipeline()
        .embed_document("the cat sat on the")
        .expect("embed");
    let id = sparse::token_id("the");
    let value = vector
        .indices
        .iter()
        .zip(&vector.values)
        .find(|(i, _)| **i == id)
        .map(|(_, v)| *v)
        .expect("token 'the' should appear");
    assert!((value - 1.375).abs() < 1e-6, "got {value}, want 1.375");
}

#[test]
fn estimator_measures_post_pipeline_lengths() {
    let pipe = plain("english");
    // "the" is filtered: (2 + 1) / 2 = 1.5.
    let estimate =
        crate::bm25_text::estimate_avg_len(["the cat sat", "dogs"], &pipe).expect("estimate");
    assert_eq!(estimate, Some(AvgLenEstimate { mean: 1.5, docs: 2 }));
    assert_eq!(
        crate::bm25_text::estimate_avg_len(Vec::<&str>::new(), &pipe).expect("estimate"),
        None
    );
    assert_eq!(
        crate::bm25_text::estimate_avg_len(["the a"], &pipe).expect("estimate"),
        None,
        "all-filtered sample has no meaningful average"
    );
}

#[test]
fn resolve_rejects_bad_names() {
    for (language, tokenizer, stemmer) in [
        (Some("klingon"), None, None),
        (None, Some("ngram"), None),
        (None, None, Some("yoda")),
    ] {
        let err = Bm25TextConfig::resolve(
            None, None, None, language, tokenizer, None, None, None, stemmer, None, None, None,
        )
        .expect_err("bad option must fail closed");
        assert_eq!(err.code, "QQL-VALIDATION-CONFIG");
    }
    // Explicit stemmer override wins over the language default.
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        Some("spanish"),
        None,
        None,
        None,
        None,
        Some("none"),
        None,
        None,
        None,
    )
    .expect("valid");
    assert_eq!(config.stemmer, Some(Stemmer::Disabled));
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        Some("spanish"),
        None,
        None,
        None,
        None,
        Some("french"),
        None,
        None,
        None,
    )
    .expect("valid");
    assert_eq!(config.stemmer, Some(Stemmer::Snowball(Language::French)));
}

/// Vendored stopword lists keep Qdrant's exact entries, including the
/// easily "normalized away" ones: hinglish carries both apostrophe forms
/// (`aint` and `ain't`); tajik keeps trailing-space and hyphenated forms
/// verbatim (dead or live exactly as upstream); arabic keeps its diacritics.
/// Guards the `scripts/gen_bm25_stopwords.py` port against well-meaning
/// cleanup. See `bm25_stopwords.rs` header for re-sync.
#[test]
fn vendored_stopwords_keep_upstream_entries_verbatim() {
    use crate::bm25_stopwords::stopwords_for;

    let hinglish = stopwords_for(Language::Hinglish);
    for word in ["aint", "ain't", "well", "we'll", "were", "we're"] {
        assert!(hinglish.contains(word), "hinglish must keep {word:?}");
    }
    let tajik = stopwords_for(Language::Tajik);
    for word in ["агар ", "аз-баски ", "чун-ки", "то даме ки", "то даме ки "]
    {
        assert!(tajik.contains(word), "tajik must keep {word:?} verbatim");
    }
    let arabic = stopwords_for(Language::Arabic);
    for word in ["ّأيّان", "لا سيما"] {
        assert!(arabic.contains(word), "arabic must keep {word:?} verbatim");
    }
    let kazakh = stopwords_for(Language::Kazakh);
    assert!(kazakh.contains("әттеген-ай"));
    let hungarian = stopwords_for(Language::Hungarian);
    assert!(hungarian.contains("ill"));
}

/// Incremental updates keep untouched knobs (the WASM `setBm25Text`
/// contract): setting the tokenizer must not reset the language.
#[test]
fn with_text_options_keeps_untouched_knobs() {
    let spanish = Bm25TextConfig::resolve(
        None,
        None,
        None,
        Some("spanish"),
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("valid");
    let updated = spanish
        .with_text_options(
            None,
            Some("whitespace"),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        )
        .expect("valid update");
    assert_eq!(updated.language, Language::Spanish);
    assert_eq!(updated.tokenizer, Tokenizer::Whitespace);
    assert_eq!(updated.params, spanish.params);
    // Empty names behave like None (JS empty-string convention).
    let same = spanish
        .with_text_options(Some(""), Some(""), None, None, None, None, None, None, None)
        .expect("valid update");
    assert_eq!(same, spanish);
    // Invalid names still fail closed on the update path.
    let err = spanish
        .with_text_options(
            Some("klingon"),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        )
        .expect_err("bad language rejects");
    assert_eq!(err.code, "QQL-VALIDATION-CONFIG");
}

/// Additional language lists merge with custom words (Qdrant `Set`
/// semantics): an explicit selection replaces the default.
#[test]
fn stopwords_languages_merge() {
    // French + custom: "le" (french) and "zzz" (custom) filter; english
    // "the" survives because the default list is replaced, not merged.
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some(vec!["french".to_string()]),
    )
    .expect("valid");
    let tokens = config
        .pipeline()
        .doc_tokens("le the zzz abc")
        .expect("tokens");
    assert_eq!(tokens, vec!["the", "zzz", "abc"], "got {tokens:?}");

    // Custom words ride alongside merged languages.
    let config = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some(vec!["abc".to_string()]),
        None,
        None,
        None,
        Some(vec!["french".to_string()]),
    )
    .expect("valid");
    let tokens = config.pipeline().doc_tokens("le abc").expect("tokens");
    assert!(tokens.is_empty(), "got {tokens:?}");

    // Unknown language names fail closed.
    let err = Bm25TextConfig::resolve(
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some(vec!["klingon".to_string()]),
    )
    .expect_err("bad stopwords language rejects");
    assert_eq!(err.code, "QQL-VALIDATION-CONFIG");
}