next-plaid 1.6.2

CPU-based PLAID implementation for multi-vector search using ndarray
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
//! Integration tests for filtering with index operations.

use ndarray::Array2;
use ndarray_rand::rand_distr::Uniform;
use ndarray_rand::RandomExt;
use next_plaid::index::MmapIndex;
use next_plaid::{filtering, IndexConfig, SearchParameters};
use serde_json::json;
use tempfile::TempDir;

fn setup_test_dir() -> TempDir {
    TempDir::new().unwrap()
}

fn random_embeddings(num_docs: usize, tokens_per_doc: usize, dim: usize) -> Vec<Array2<f32>> {
    (0..num_docs)
        .map(|_| {
            let mut emb: Array2<f32> =
                Array2::random((tokens_per_doc, dim), Uniform::new(-1.0f32, 1.0f32));
            // Normalize rows
            for mut row in emb.axis_iter_mut(ndarray::Axis(0)) {
                let norm: f32 = row.dot(&row).sqrt();
                if norm > 0.0 {
                    row.mapv_inplace(|x| x / norm);
                }
            }
            emb
        })
        .collect()
}

#[test]
fn test_create_index_with_metadata() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create embeddings
    let embeddings = random_embeddings(5, 10, 64);

    // Create metadata
    let metadata = vec![
        json!({"name": "Doc1", "category": "A", "score": 95}),
        json!({"name": "Doc2", "category": "B", "score": 87}),
        json!({"name": "Doc3", "category": "A", "score": 92}),
        json!({"name": "Doc4", "category": "B", "score": 78}),
        json!({"name": "Doc5", "category": "A", "score": 99}),
    ];

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    let index = MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();

    // Create metadata database with explicit doc_ids
    let doc_ids: Vec<i64> = (0..5).collect();
    filtering::create(path, &metadata, &doc_ids).unwrap();

    // Verify both exist
    assert_eq!(index.metadata.num_documents, 5);
    assert!(filtering::exists(path));
    assert_eq!(filtering::count(path).unwrap(), 5);
}

#[test]
fn test_search_with_subset_filter() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create embeddings
    let embeddings = random_embeddings(10, 8, 64);

    // Create metadata with categories
    let metadata: Vec<serde_json::Value> = (0..10)
        .map(|i| {
            json!({
                "doc_id": i,
                "category": if i % 2 == 0 { "even" } else { "odd" },
                "value": i * 10
            })
        })
        .collect();

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    let index = MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();
    let doc_ids: Vec<i64> = (0..10).collect();
    filtering::create(path, &metadata, &doc_ids).unwrap();

    // Get subset of "even" category documents
    let subset = filtering::where_condition(path, "category = ?", &[json!("even")]).unwrap();
    assert_eq!(subset, vec![0, 2, 4, 6, 8]);

    // Create query (use first embedding as query)
    let query = embeddings[0].clone();

    // Search with subset filter
    let params = SearchParameters {
        top_k: 3,
        n_ivf_probe: 4,
        ..Default::default()
    };
    let result = index.search(&query, &params, Some(&subset)).unwrap();

    // Verify all results are in the subset
    for pid in &result.passage_ids {
        assert!(subset.contains(pid), "Result {} not in subset", pid);
    }
}

#[test]
fn test_delete_with_metadata() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create embeddings
    let embeddings = random_embeddings(5, 8, 64);

    // Create metadata
    let metadata = vec![
        json!({"name": "Doc0"}),
        json!({"name": "Doc1"}),
        json!({"name": "Doc2"}),
        json!({"name": "Doc3"}),
        json!({"name": "Doc4"}),
    ];

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    let mut index = MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();
    let doc_ids: Vec<i64> = (0..5).collect();
    filtering::create(path, &metadata, &doc_ids).unwrap();

    // Delete documents 1 and 3
    let deleted = index.delete(&[1, 3]).unwrap();
    assert_eq!(deleted, 2);

    // Reload to get updated metadata
    index.reload().unwrap();

    // Verify index
    assert_eq!(index.metadata.num_documents, 3);

    // Verify metadata database was updated and re-indexed
    assert_eq!(filtering::count(path).unwrap(), 3);

    // Check the remaining documents have correct _subset_ IDs (0, 1, 2)
    let results = filtering::get(path, None, &[], None).unwrap();
    assert_eq!(results.len(), 3);
    assert_eq!(results[0]["name"], "Doc0");
    assert_eq!(results[0]["_subset_"], 0);
    assert_eq!(results[1]["name"], "Doc2");
    assert_eq!(results[1]["_subset_"], 1);
    assert_eq!(results[2]["name"], "Doc4");
    assert_eq!(results[2]["_subset_"], 2);
}

#[test]
fn test_complex_filter_query() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create embeddings
    let embeddings = random_embeddings(10, 8, 64);

    // Create metadata with various types
    let categories = ["A", "B", "C"];
    let metadata: Vec<serde_json::Value> = (0..10)
        .map(|i| {
            json!({
                "doc_id": i,
                "category": categories[i % 3],
                "score": 50 + i * 5,
                "active": i % 2 == 0,
                "name": format!("Document {}", i)
            })
        })
        .collect();

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();
    let doc_ids: Vec<i64> = (0..10).collect();
    filtering::create(path, &metadata, &doc_ids).unwrap();

    // Complex query: category A and score >= 70
    let subset = filtering::where_condition(
        path,
        "category = ? AND score >= ?",
        &[json!("A"), json!(70)],
    )
    .unwrap();

    // Docs with category A are 0, 3, 6, 9
    // Scores are 50, 65, 80, 95
    // So 6 (score 80) and 9 (score 95) match
    assert_eq!(subset, vec![6, 9]);

    // Query with LIKE
    let subset = filtering::where_condition(path, "name LIKE ?", &[json!("Document 1%")]).unwrap();
    assert_eq!(subset, vec![1]); // Only "Document 1"

    // Query with OR
    let subset =
        filtering::where_condition(path, "category = ? OR score > ?", &[json!("C"), json!(85)])
            .unwrap();
    // Category C: 2, 5, 8
    // Score > 85: 8, 9
    // Union: 2, 5, 8, 9
    assert_eq!(subset, vec![2, 5, 8, 9]);
}

#[test]
fn test_get_metadata_by_subset() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create embeddings
    let embeddings = random_embeddings(5, 8, 64);

    // Create metadata
    let metadata = vec![
        json!({"name": "Alice", "age": 30}),
        json!({"name": "Bob", "age": 25}),
        json!({"name": "Charlie", "age": 35}),
        json!({"name": "Diana", "age": 28}),
        json!({"name": "Eve", "age": 32}),
    ];

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();
    let doc_ids: Vec<i64> = (0..5).collect();
    filtering::create(path, &metadata, &doc_ids).unwrap();

    // Get metadata in specific order
    let results = filtering::get(path, None, &[], Some(&[4, 1, 3])).unwrap();
    assert_eq!(results.len(), 3);
    assert_eq!(results[0]["name"], "Eve");
    assert_eq!(results[1]["name"], "Bob");
    assert_eq!(results[2]["name"], "Diana");
}

#[test]
fn test_update_with_new_metadata() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create initial embeddings and metadata
    let embeddings = random_embeddings(3, 8, 64);
    let metadata = vec![
        json!({"name": "Doc0", "category": "A"}),
        json!({"name": "Doc1", "category": "B"}),
        json!({"name": "Doc2", "category": "A"}),
    ];

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    let mut index = MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();
    let doc_ids: Vec<i64> = (0..3).collect();
    filtering::create(path, &metadata, &doc_ids).unwrap();

    // Add new documents with metadata
    let new_embeddings = random_embeddings(2, 8, 64);
    let new_metadata = vec![
        json!({"name": "Doc3", "category": "C", "new_field": "value"}),
        json!({"name": "Doc4", "category": "A"}),
    ];

    // Update index and metadata
    let update_config = next_plaid::UpdateConfig::default();
    let new_doc_ids = index.update(&new_embeddings, &update_config).unwrap();
    filtering::update(path, &new_metadata, &new_doc_ids).unwrap();

    // Verify counts
    assert_eq!(index.metadata.num_documents, 5);
    assert_eq!(filtering::count(path).unwrap(), 5);

    // Query by category A should now return 0, 2, 4
    let subset = filtering::where_condition(path, "category = ?", &[json!("A")]).unwrap();
    assert_eq!(subset, vec![0, 2, 4]);

    // Verify new column was added
    let results = filtering::get(path, None, &[], Some(&[3])).unwrap();
    assert_eq!(results[0]["new_field"], "value");
    // Old rows should have null for new column
    let results = filtering::get(path, None, &[], Some(&[0])).unwrap();
    assert!(results[0].get("new_field").is_none_or(|v| v.is_null()));
}

#[test]
fn test_search_empty_subset() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create embeddings
    let embeddings = random_embeddings(5, 8, 64);

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    let index = MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();

    // Create query
    let query = embeddings[0].clone();

    // Search with empty subset
    let params = SearchParameters {
        top_k: 3,
        n_ivf_probe: 4,
        ..Default::default()
    };
    let result = index.search(&query, &params, Some(&[])).unwrap();

    // Should return empty results
    assert!(result.passage_ids.is_empty());
}

#[test]
fn test_create_update_delete_update_workflow() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();
    let embedding_dim = 128;
    let tokens_per_doc = 10;

    // 1. Create initial documents with metadata
    let initial_embeddings = random_embeddings(3, tokens_per_doc, embedding_dim);
    let initial_metadata = vec![
        json!({"name": "Alice", "category": "A", "join_date": "2023-05-17"}),
        json!({"name": "Bob", "category": "B", "join_date": "2021-06-21"}),
        json!({"name": "Alex", "category": "A", "join_date": "2023-08-01"}),
    ];

    let config = IndexConfig {
        nbits: 4,
        batch_size: 100,
        seed: Some(42),
        ..Default::default()
    };
    let mut index = MmapIndex::create_with_kmeans(&initial_embeddings, path, &config).unwrap();
    let doc_ids: Vec<i64> = (0..3).collect();
    filtering::create(path, &initial_metadata, &doc_ids).unwrap();

    // Verify 3 documents after initial creation
    assert_eq!(
        filtering::count(path).unwrap(),
        3,
        "Expected 3 documents after initial creation"
    );
    assert_eq!(
        index.metadata.num_documents, 3,
        "Expected 3 documents in index after initial creation"
    );

    // Search should return 3 results
    let random_query = random_embeddings(1, tokens_per_doc, embedding_dim)
        .pop()
        .unwrap();
    let params = SearchParameters {
        top_k: 10,
        n_ivf_probe: 4,
        // Disable threshold for random embeddings test (random vectors have low similarity)
        centroid_score_threshold: None,
        ..Default::default()
    };
    let result = index.search(&random_query, &params, None).unwrap();
    assert_eq!(
        result.passage_ids.len(),
        3,
        "Expected 3 search results after initial creation"
    );

    // 2. Update with 1 new document
    let new_embeddings = random_embeddings(1, tokens_per_doc, embedding_dim);
    let new_metadata = vec![json!({"name": "Charlie", "category": "B", "join_date": "2020-03-15"})];

    let update_config = next_plaid::UpdateConfig::default();
    let new_doc_ids = index.update(&new_embeddings, &update_config).unwrap();
    filtering::update(path, &new_metadata, &new_doc_ids).unwrap();

    // Verify 4 documents after update
    assert_eq!(
        filtering::count(path).unwrap(),
        4,
        "Expected 4 documents after update"
    );
    assert_eq!(
        index.metadata.num_documents, 4,
        "Expected 4 documents in index after update"
    );

    // Search should return 4 results
    let result = index.search(&random_query, &params, None).unwrap();
    assert_eq!(
        result.passage_ids.len(),
        4,
        "Expected 4 search results after update"
    );

    // 3. Delete document with ID 3
    let deleted = index.delete(&[3]).unwrap();
    assert_eq!(deleted, 1, "Expected 1 document to be deleted");

    // Reload to get updated metadata
    index.reload().unwrap();

    // Verify 3 documents after deletion
    assert_eq!(
        filtering::count(path).unwrap(),
        3,
        "Expected 3 documents after deletion"
    );
    assert_eq!(
        index.metadata.num_documents, 3,
        "Expected 3 documents in index after deletion"
    );

    // Search should return 3 results
    let result = index.search(&random_query, &params, None).unwrap();
    assert_eq!(
        result.passage_ids.len(),
        3,
        "Expected 3 search results after deletion"
    );

    // 4. Update again with same new document
    let new_embeddings = random_embeddings(1, tokens_per_doc, embedding_dim);
    let new_metadata = vec![json!({"name": "Charlie", "category": "B", "join_date": "2020-03-15"})];

    let new_doc_ids = index.update(&new_embeddings, &update_config).unwrap();
    filtering::update(path, &new_metadata, &new_doc_ids).unwrap();

    // Verify 4 documents after second update
    assert_eq!(
        filtering::count(path).unwrap(),
        4,
        "Expected 4 documents after second update"
    );
    assert_eq!(
        index.metadata.num_documents, 4,
        "Expected 4 documents in index after second update"
    );

    // Search should return 4 results
    let result = index.search(&random_query, &params, None).unwrap();
    assert_eq!(
        result.passage_ids.len(),
        4,
        "Expected 4 search results after second update"
    );
}

#[test]
fn test_numeric_range_queries() {
    let dir = setup_test_dir();
    let path = dir.path().to_str().unwrap();

    // Create embeddings
    let embeddings = random_embeddings(10, 8, 64);

    // Create metadata with numeric fields
    let metadata: Vec<serde_json::Value> = (0..10)
        .map(|i| {
            json!({
                "id": i,
                "price": 10.0 + i as f64 * 5.5,
                "quantity": i * 10
            })
        })
        .collect();

    // Create index
    let config = IndexConfig {
        nbits: 4,
        batch_size: 10,
        seed: Some(42),
        ..Default::default()
    };
    MmapIndex::create_with_kmeans(&embeddings, path, &config).unwrap();
    let doc_ids: Vec<i64> = (0..10).collect();
    filtering::create(path, &metadata, &doc_ids).unwrap();

    // Range query on price
    let subset = filtering::where_condition(
        path,
        "price >= ? AND price < ?",
        &[json!(20.0), json!(40.0)],
    )
    .unwrap();
    // price values: 10, 15.5, 21, 26.5, 32, 37.5, 43, 48.5, 54, 59.5
    // In range [20, 40): 21, 26.5, 32, 37.5 (indices 2, 3, 4, 5)
    assert_eq!(subset, vec![2, 3, 4, 5]);

    // Range query on integer field
    let subset =
        filtering::where_condition(path, "quantity BETWEEN ? AND ?", &[json!(30), json!(60)])
            .unwrap();
    // quantity values: 0, 10, 20, 30, 40, 50, 60, 70, 80, 90
    // In range [30, 60]: 30, 40, 50, 60 (indices 3, 4, 5, 6)
    assert_eq!(subset, vec![3, 4, 5, 6]);
}