kitedb 0.2.2

High-performance embedded graph database
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
//! Python bindings for Vector Search
//!
//! Exposes IVF and IVF-PQ indexes to Python.

use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use std::sync::RwLock;

use crate::vector::{
  DistanceMetric as RustDistanceMetric, IvfConfig as RustIvfConfig, IvfIndex as RustIvfIndex,
  IvfPqConfig as RustIvfPqConfig, IvfPqIndex as RustIvfPqIndex, MultiQueryAggregation,
  PqConfig as RustPqConfig, SearchOptions as RustSearchOptions, VectorManifest, VectorSearchResult,
};

// ============================================================================
// Distance Metric
// ============================================================================

/// Distance metric conversion
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PyDistanceMetricEnum {
  Cosine,
  Euclidean,
  DotProduct,
}

impl From<&str> for PyDistanceMetricEnum {
  fn from(s: &str) -> Self {
    match s.to_lowercase().as_str() {
      "euclidean" | "l2" => PyDistanceMetricEnum::Euclidean,
      "dot" | "dotproduct" | "dot_product" => PyDistanceMetricEnum::DotProduct,
      _ => PyDistanceMetricEnum::Cosine,
    }
  }
}

impl From<PyDistanceMetricEnum> for RustDistanceMetric {
  fn from(m: PyDistanceMetricEnum) -> Self {
    match m {
      PyDistanceMetricEnum::Cosine => RustDistanceMetric::Cosine,
      PyDistanceMetricEnum::Euclidean => RustDistanceMetric::Euclidean,
      PyDistanceMetricEnum::DotProduct => RustDistanceMetric::DotProduct,
    }
  }
}

impl From<RustDistanceMetric> for PyDistanceMetricEnum {
  fn from(m: RustDistanceMetric) -> Self {
    match m {
      RustDistanceMetric::Cosine => PyDistanceMetricEnum::Cosine,
      RustDistanceMetric::Euclidean => PyDistanceMetricEnum::Euclidean,
      RustDistanceMetric::DotProduct => PyDistanceMetricEnum::DotProduct,
    }
  }
}

// ============================================================================
// Aggregation Method
// ============================================================================

/// Aggregation method conversion
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PyAggregationEnum {
  Min,
  Max,
  Avg,
  Sum,
}

impl From<&str> for PyAggregationEnum {
  fn from(s: &str) -> Self {
    match s.to_lowercase().as_str() {
      "max" => PyAggregationEnum::Max,
      "avg" | "average" => PyAggregationEnum::Avg,
      "sum" => PyAggregationEnum::Sum,
      _ => PyAggregationEnum::Min,
    }
  }
}

impl From<PyAggregationEnum> for MultiQueryAggregation {
  fn from(a: PyAggregationEnum) -> Self {
    match a {
      PyAggregationEnum::Min => MultiQueryAggregation::Min,
      PyAggregationEnum::Max => MultiQueryAggregation::Max,
      PyAggregationEnum::Avg => MultiQueryAggregation::Avg,
      PyAggregationEnum::Sum => MultiQueryAggregation::Sum,
    }
  }
}

// ============================================================================
// IVF Configuration
// ============================================================================

/// Configuration for IVF index
#[pyclass(name = "IvfConfig")]
#[derive(Debug, Clone)]
pub struct PyIvfConfig {
  /// Number of clusters (default: 100)
  #[pyo3(get, set)]
  pub n_clusters: Option<i32>,
  /// Number of clusters to probe during search (default: 10)
  #[pyo3(get, set)]
  pub n_probe: Option<i32>,
  /// Distance metric ("cosine", "euclidean", "dot_product")
  #[pyo3(get, set)]
  pub metric: Option<String>,
}

#[pymethods]
impl PyIvfConfig {
  #[new]
  #[pyo3(signature = (n_clusters=None, n_probe=None, metric=None))]
  fn new(n_clusters: Option<i32>, n_probe: Option<i32>, metric: Option<String>) -> Self {
    Self {
      n_clusters,
      n_probe,
      metric,
    }
  }

  fn __repr__(&self) -> String {
    format!(
      "IvfConfig(n_clusters={:?}, n_probe={:?}, metric={:?})",
      self.n_clusters, self.n_probe, self.metric
    )
  }
}

impl From<PyIvfConfig> for RustIvfConfig {
  fn from(c: PyIvfConfig) -> Self {
    let mut config = RustIvfConfig::default();
    if let Some(n) = c.n_clusters {
      config.n_clusters = n as usize;
    }
    if let Some(n) = c.n_probe {
      config.n_probe = n as usize;
    }
    if let Some(m) = c.metric {
      let metric: PyDistanceMetricEnum = m.as_str().into();
      config.metric = metric.into();
    }
    config
  }
}

// ============================================================================
// PQ Configuration
// ============================================================================

/// Configuration for Product Quantization
#[pyclass(name = "PqConfig")]
#[derive(Debug, Clone)]
pub struct PyPqConfig {
  /// Number of subspaces (must divide dimensions evenly)
  #[pyo3(get, set)]
  pub num_subspaces: Option<i32>,
  /// Number of centroids per subspace (default: 256)
  #[pyo3(get, set)]
  pub num_centroids: Option<i32>,
  /// Max k-means iterations for training (default: 25)
  #[pyo3(get, set)]
  pub max_iterations: Option<i32>,
}

#[pymethods]
impl PyPqConfig {
  #[new]
  #[pyo3(signature = (num_subspaces=None, num_centroids=None, max_iterations=None))]
  fn new(
    num_subspaces: Option<i32>,
    num_centroids: Option<i32>,
    max_iterations: Option<i32>,
  ) -> Self {
    Self {
      num_subspaces,
      num_centroids,
      max_iterations,
    }
  }

  fn __repr__(&self) -> String {
    format!(
      "PqConfig(num_subspaces={:?}, num_centroids={:?})",
      self.num_subspaces, self.num_centroids
    )
  }
}

impl From<PyPqConfig> for RustPqConfig {
  fn from(c: PyPqConfig) -> Self {
    let mut config = RustPqConfig::default();
    if let Some(n) = c.num_subspaces {
      config.num_subspaces = n as usize;
    }
    if let Some(n) = c.num_centroids {
      config.num_centroids = n as usize;
    }
    if let Some(n) = c.max_iterations {
      config.max_iterations = n as usize;
    }
    config
  }
}

// ============================================================================
// Search Options
// ============================================================================

/// Options for vector search
#[pyclass(name = "SearchOptions")]
#[derive(Debug, Clone)]
pub struct PySearchOptions {
  /// Number of clusters to probe (overrides index default)
  #[pyo3(get, set)]
  pub n_probe: Option<i32>,
  /// Minimum similarity threshold (0-1)
  #[pyo3(get, set)]
  pub threshold: Option<f64>,
}

#[pymethods]
impl PySearchOptions {
  #[new]
  #[pyo3(signature = (n_probe=None, threshold=None))]
  fn new(n_probe: Option<i32>, threshold: Option<f64>) -> Self {
    Self { n_probe, threshold }
  }

  fn __repr__(&self) -> String {
    format!(
      "SearchOptions(n_probe={:?}, threshold={:?})",
      self.n_probe, self.threshold
    )
  }
}

// ============================================================================
// Search Result
// ============================================================================

/// Result of a vector search
#[pyclass(name = "SearchResult")]
#[derive(Debug, Clone)]
pub struct PySearchResult {
  /// Vector ID
  #[pyo3(get)]
  pub vector_id: i64,
  /// Associated node ID
  #[pyo3(get)]
  pub node_id: i64,
  /// Distance from query
  #[pyo3(get)]
  pub distance: f64,
  /// Similarity score (0-1, higher is more similar)
  #[pyo3(get)]
  pub similarity: f64,
}

#[pymethods]
impl PySearchResult {
  fn __repr__(&self) -> String {
    format!(
      "SearchResult(node_id={}, distance={:.4}, similarity={:.4})",
      self.node_id, self.distance, self.similarity
    )
  }
}

impl From<VectorSearchResult> for PySearchResult {
  fn from(r: VectorSearchResult) -> Self {
    PySearchResult {
      vector_id: r.vector_id as i64,
      node_id: r.node_id as i64,
      distance: r.distance as f64,
      similarity: r.similarity as f64,
    }
  }
}

// ============================================================================
// IVF Index Statistics
// ============================================================================

/// Statistics for IVF index
#[pyclass(name = "IvfStats")]
#[derive(Debug, Clone)]
pub struct PyIvfStats {
  /// Whether the index is trained
  #[pyo3(get)]
  pub trained: bool,
  /// Number of clusters
  #[pyo3(get)]
  pub n_clusters: i32,
  /// Total vectors in the index
  #[pyo3(get)]
  pub total_vectors: i64,
  /// Average vectors per cluster
  #[pyo3(get)]
  pub avg_vectors_per_cluster: f64,
  /// Number of empty clusters
  #[pyo3(get)]
  pub empty_cluster_count: i32,
  /// Minimum cluster size
  #[pyo3(get)]
  pub min_cluster_size: i32,
  /// Maximum cluster size
  #[pyo3(get)]
  pub max_cluster_size: i32,
}

#[pymethods]
impl PyIvfStats {
  fn __repr__(&self) -> String {
    format!(
      "IvfStats(trained={}, n_clusters={}, total_vectors={})",
      self.trained, self.n_clusters, self.total_vectors
    )
  }
}

// ============================================================================
// IVF Index Python Wrapper
// ============================================================================

/// IVF (Inverted File) index for approximate nearest neighbor search
#[pyclass(name = "IvfIndex")]
pub struct PyIvfIndex {
  inner: RwLock<RustIvfIndex>,
}

#[pymethods]
impl PyIvfIndex {
  /// Create a new IVF index
  #[new]
  #[pyo3(signature = (dimensions, config=None))]
  fn new(dimensions: i32, config: Option<PyIvfConfig>) -> PyResult<Self> {
    let rust_config = config.map(Into::into).unwrap_or_default();
    Ok(PyIvfIndex {
      inner: RwLock::new(RustIvfIndex::new(dimensions as usize, rust_config)),
    })
  }

  /// Get the number of dimensions
  #[getter]
  fn dimensions(&self) -> PyResult<i32> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    Ok(index.dimensions as i32)
  }

  /// Check if the index is trained
  #[getter]
  fn trained(&self) -> PyResult<bool> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    Ok(index.trained)
  }

  /// Add training vectors
  fn add_training_vectors(&self, vectors: Vec<f64>, num_vectors: i32) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let vectors_f32: Vec<f32> = vectors.iter().map(|&v| v as f32).collect();
    index
      .add_training_vectors(&vectors_f32, num_vectors as usize)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to add training vectors: {e}")))
  }

  /// Train the index on added training vectors
  fn train(&self) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    index
      .train()
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to train index: {e}")))
  }

  /// Insert a vector into the index
  fn insert(&self, vector_id: i64, vector: Vec<f64>) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    index
      .insert(vector_id as u64, &vector_f32)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to insert vector: {e}")))
  }

  /// Delete a vector from the index
  fn delete(&self, vector_id: i64, vector: Vec<f64>) -> PyResult<bool> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    Ok(index.delete(vector_id as u64, &vector_f32))
  }

  /// Clear all data from the index
  fn clear(&self) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    index.clear();
    Ok(())
  }

  /// Search for k nearest neighbors
  #[pyo3(signature = (manifest_json, query, k, options=None))]
  fn search(
    &self,
    manifest_json: String,
    query: Vec<f64>,
    k: i32,
    options: Option<PySearchOptions>,
  ) -> PyResult<Vec<PySearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;

    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to parse manifest: {e}")))?;

    let query_f32: Vec<f32> = query.iter().map(|&v| v as f32).collect();

    let rust_options = options.map(|o| RustSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search(&manifest, &query_f32, k as usize, rust_options);
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Search with multiple query vectors
  #[pyo3(signature = (manifest_json, queries, k, aggregation, options=None))]
  fn search_multi(
    &self,
    manifest_json: String,
    queries: Vec<Vec<f64>>,
    k: i32,
    aggregation: String,
    options: Option<PySearchOptions>,
  ) -> PyResult<Vec<PySearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;

    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to parse manifest: {e}")))?;

    let queries_f32: Vec<Vec<f32>> = queries
      .iter()
      .map(|q| q.iter().map(|&v| v as f32).collect())
      .collect();

    let query_refs: Vec<&[f32]> = queries_f32.iter().map(|q| q.as_slice()).collect();

    let agg: PyAggregationEnum = aggregation.as_str().into();

    let rust_options = options.map(|o| RustSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search_multi(&manifest, &query_refs, k as usize, agg.into(), rust_options);
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Get index statistics
  fn stats(&self) -> PyResult<PyIvfStats> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let s = index.stats();
    Ok(PyIvfStats {
      trained: s.trained,
      n_clusters: s.n_clusters as i32,
      total_vectors: s.total_vectors as i64,
      avg_vectors_per_cluster: s.avg_vectors_per_cluster as f64,
      empty_cluster_count: s.empty_cluster_count as i32,
      min_cluster_size: s.min_cluster_size as i32,
      max_cluster_size: s.max_cluster_size as i32,
    })
  }

  /// Serialize the index to bytes
  fn serialize<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let bytes = crate::vector::ivf::serialize::serialize_ivf(&index);
    Ok(PyBytes::new_bound(py, &bytes))
  }

  /// Deserialize an index from bytes
  #[staticmethod]
  fn deserialize(data: &[u8]) -> PyResult<PyIvfIndex> {
    let index = crate::vector::ivf::serialize::deserialize_ivf(data)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to deserialize: {e}")))?;
    Ok(PyIvfIndex {
      inner: RwLock::new(index),
    })
  }

  fn __repr__(&self) -> PyResult<String> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    Ok(format!(
      "IvfIndex(dimensions={}, trained={})",
      index.dimensions, index.trained
    ))
  }
}

// ============================================================================
// IVF-PQ Index Python Wrapper
// ============================================================================

/// IVF-PQ combined index for memory-efficient approximate nearest neighbor search
#[pyclass(name = "IvfPqIndex")]
pub struct PyIvfPqIndex {
  inner: RwLock<RustIvfPqIndex>,
}

#[pymethods]
impl PyIvfPqIndex {
  /// Create a new IVF-PQ index
  #[new]
  #[pyo3(signature = (dimensions, ivf_config=None, pq_config=None, use_residuals=None))]
  fn new(
    dimensions: i32,
    ivf_config: Option<PyIvfConfig>,
    pq_config: Option<PyPqConfig>,
    use_residuals: Option<bool>,
  ) -> PyResult<Self> {
    let config = RustIvfPqConfig {
      ivf: ivf_config.map(Into::into).unwrap_or_default(),
      pq: pq_config.map(Into::into).unwrap_or_default(),
      use_residuals: use_residuals.unwrap_or(true),
    };

    let index = RustIvfPqIndex::new(dimensions as usize, config)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to create index: {e}")))?;

    Ok(PyIvfPqIndex {
      inner: RwLock::new(index),
    })
  }

  /// Get the number of dimensions
  #[getter]
  fn dimensions(&self) -> PyResult<i32> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    Ok(index.dimensions as i32)
  }

  /// Check if the index is trained
  #[getter]
  fn trained(&self) -> PyResult<bool> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    Ok(index.trained)
  }

  /// Add training vectors
  fn add_training_vectors(&self, vectors: Vec<f64>, num_vectors: i32) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let vectors_f32: Vec<f32> = vectors.iter().map(|&v| v as f32).collect();
    index
      .add_training_vectors(&vectors_f32, num_vectors as usize)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to add training vectors: {e}")))
  }

  /// Train the index
  fn train(&self) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    index
      .train()
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to train index: {e}")))
  }

  /// Insert a vector
  fn insert(&self, vector_id: i64, vector: Vec<f64>) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    index
      .insert(vector_id as u64, &vector_f32)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to insert vector: {e}")))
  }

  /// Delete a vector
  fn delete(&self, vector_id: i64, vector: Vec<f64>) -> PyResult<bool> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    Ok(index.delete(vector_id as u64, &vector_f32))
  }

  /// Clear the index
  fn clear(&self) -> PyResult<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    index.clear();
    Ok(())
  }

  /// Search for k nearest neighbors using PQ distance approximation
  #[pyo3(signature = (manifest_json, query, k, options=None))]
  fn search(
    &self,
    manifest_json: String,
    query: Vec<f64>,
    k: i32,
    options: Option<PySearchOptions>,
  ) -> PyResult<Vec<PySearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;

    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to parse manifest: {e}")))?;

    let query_f32: Vec<f32> = query.iter().map(|&v| v as f32).collect();

    let rust_options = options.map(|o| crate::vector::ivf_pq::IvfPqSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search(&manifest, &query_f32, k as usize, rust_options);
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Search with multiple query vectors
  #[pyo3(signature = (manifest_json, queries, k, aggregation, options=None))]
  fn search_multi(
    &self,
    manifest_json: String,
    queries: Vec<Vec<f64>>,
    k: i32,
    aggregation: String,
    options: Option<PySearchOptions>,
  ) -> PyResult<Vec<PySearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;

    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to parse manifest: {e}")))?;

    let queries_f32: Vec<Vec<f32>> = queries
      .iter()
      .map(|q| q.iter().map(|&v| v as f32).collect())
      .collect();

    let query_refs: Vec<&[f32]> = queries_f32.iter().map(|q| q.as_slice()).collect();

    let agg: PyAggregationEnum = aggregation.as_str().into();

    let rust_options = options.map(|o| crate::vector::ivf_pq::IvfPqSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search_multi(&manifest, &query_refs, k as usize, agg.into(), rust_options);
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Get index statistics
  fn stats(&self) -> PyResult<PyIvfStats> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let s = index.stats();
    Ok(PyIvfStats {
      trained: s.trained,
      n_clusters: s.n_clusters as i32,
      total_vectors: s.total_vectors as i64,
      avg_vectors_per_cluster: s.avg_vectors_per_cluster as f64,
      empty_cluster_count: s.empty_cluster_count as i32,
      min_cluster_size: s.min_cluster_size as i32,
      max_cluster_size: s.max_cluster_size as i32,
    })
  }

  /// Serialize the index to bytes
  fn serialize<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    let bytes = crate::vector::ivf_pq::serialize_ivf_pq(&index);
    Ok(PyBytes::new_bound(py, &bytes))
  }

  /// Deserialize an index from bytes
  #[staticmethod]
  fn deserialize(data: &[u8]) -> PyResult<PyIvfPqIndex> {
    let index = crate::vector::ivf_pq::deserialize_ivf_pq(data)
      .map_err(|e| PyRuntimeError::new_err(format!("Failed to deserialize: {e}")))?;
    Ok(PyIvfPqIndex {
      inner: RwLock::new(index),
    })
  }

  fn __repr__(&self) -> PyResult<String> {
    let index = self
      .inner
      .read()
      .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
    Ok(format!(
      "IvfPqIndex(dimensions={}, trained={})",
      index.dimensions, index.trained
    ))
  }
}

// ============================================================================
// Brute Force Search
// ============================================================================

/// Brute force search result
#[pyclass(name = "BruteForceResult")]
#[derive(Debug, Clone)]
pub struct PyBruteForceResult {
  #[pyo3(get)]
  pub node_id: i64,
  #[pyo3(get)]
  pub distance: f64,
  #[pyo3(get)]
  pub similarity: f64,
}

#[pymethods]
impl PyBruteForceResult {
  fn __repr__(&self) -> String {
    format!(
      "BruteForceResult(node_id={}, distance={:.4}, similarity={:.4})",
      self.node_id, self.distance, self.similarity
    )
  }
}

/// Perform brute-force search over all vectors
#[pyfunction]
#[pyo3(signature = (vectors, node_ids, query, k, metric=None))]
pub fn brute_force_search(
  vectors: Vec<Vec<f64>>,
  node_ids: Vec<i64>,
  query: Vec<f64>,
  k: i32,
  metric: Option<String>,
) -> PyResult<Vec<PyBruteForceResult>> {
  if vectors.len() != node_ids.len() {
    return Err(PyRuntimeError::new_err(
      "vectors and node_ids must have same length",
    ));
  }

  let metric_enum: PyDistanceMetricEnum = metric.as_deref().unwrap_or("cosine").into();
  let rust_metric: RustDistanceMetric = metric_enum.into();
  let distance_fn = rust_metric.distance_fn();

  let query_f32: Vec<f32> = query.iter().map(|&v| v as f32).collect();

  let mut results: Vec<(i64, f32)> = vectors
    .iter()
    .zip(node_ids.iter())
    .map(|(v, &node_id)| {
      let v_f32: Vec<f32> = v.iter().map(|&x| x as f32).collect();
      let dist = distance_fn(&query_f32, &v_f32);
      (node_id, dist)
    })
    .collect();

  // Sort by distance
  results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
  results.truncate(k as usize);

  Ok(
    results
      .into_iter()
      .map(|(node_id, distance)| PyBruteForceResult {
        node_id,
        distance: distance as f64,
        similarity: rust_metric.distance_to_similarity(distance) as f64,
      })
      .collect(),
  )
}