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
836
837
838
839
840
841
842
843
844
845
846
use super::*;
/// The collection HNSW index configuration.
#[cfg_attr(feature = "py", pyclass(module = "oasysdb.collection", get_all))]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
/// Nodes to consider during construction.
pub ef_construction: usize,
/// Nodes to consider during search.
pub ef_search: usize,
/// Layer multiplier. The optimal value is `1/ln(M)`.
pub ml: f32,
/// Distance calculation function.
pub distance: Distance,
}
// Any modifications to this methods should be reflected in:
// - py/tests/test_collection.py
// - py/oasysdb/collection.pyi
#[cfg(feature = "py")]
#[pymethods]
impl Config {
#[new]
fn py_new(
ef_construction: usize,
ef_search: usize,
ml: f32,
distance: &str,
) -> Result<Self, Error> {
Self::new(ef_construction, ef_search, ml, distance)
}
#[setter(ef_construction)]
fn py_set_ef_construction(&mut self, ef_construction: usize) {
self.ef_construction = ef_construction;
}
#[setter(ef_search)]
fn py_set_ef_search(&mut self, ef_search: usize) {
self.ef_search = ef_search;
}
#[setter(ml)]
fn py_set_ml(&mut self, ml: f32) {
self.ml = ml;
}
#[setter(distance)]
fn py_set_distance(&mut self, distance: &str) -> Result<(), Error> {
self.set_distance(distance)
}
#[staticmethod]
fn create_default() -> Self {
Self::default()
}
fn __repr__(&self) -> String {
format!("{:?}", self)
}
}
impl Config {
/// Creates a new collection config with the given parameters.
pub fn new(
ef_construction: usize,
ef_search: usize,
ml: f32,
distance: &str,
) -> Result<Self, Error> {
let distance = Distance::from(distance)?;
Ok(Self { ef_construction, ef_search, ml, distance })
}
/// Sets the distance calculation function.
/// * `distance`: Distance function, e.g. euclidean or dot.
pub fn set_distance(&mut self, distance: &str) -> Result<(), Error> {
self.distance = Distance::from(distance)?;
Ok(())
}
}
impl Default for Config {
/// Default configuration for the collection index.
/// * `ef_construction`: 40
/// * `ef_search`: 15
/// * `ml`: 0.3
/// * `distance`: euclidean
fn default() -> Self {
Self {
ef_construction: 40,
ef_search: 15,
ml: 0.3,
distance: Distance::Euclidean,
}
}
}
/// The collection of vector records with HNSW indexing.
#[cfg_attr(feature = "py", pyclass(module = "oasysdb.collection"))]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Collection {
/// The collection configuration object.
pub config: Config,
/// The min/max distance to consider a neighbor.
pub relevancy: f32,
// Private fields below.
data: HashMap<VectorID, Metadata>,
vectors: HashMap<VectorID, Vector>,
slots: Vec<VectorID>,
base_layer: Vec<BaseNode>,
upper_layers: Vec<Vec<UpperNode>>,
// Utility fields.
count: usize,
dimension: usize,
}
impl Index<&VectorID> for Collection {
type Output = Vector;
fn index(&self, index: &VectorID) -> &Self::Output {
&self.vectors[index]
}
}
// This exposes Collection methods to Python.
// Any modifications to these methods should be reflected in:
// - py/tests/test_collection.py
// - py/oasysdb/collection.pyi
#[cfg_attr(feature = "py", pymethods)]
impl Collection {
#[cfg(feature = "py")]
#[new]
fn py_new(config: &Config) -> Self {
Self::new(config)
}
#[cfg(feature = "py")]
#[staticmethod]
fn from_records(
config: &Config,
records: Vec<Record>,
) -> Result<Self, Error> {
Self::build(config, &records)
}
#[cfg(feature = "py")]
#[staticmethod]
#[pyo3(name = "build")]
fn py_build(
config: &Config,
records: Vec<Record>,
) -> Result<Collection, Error> {
Self::build(config, &records)
}
/// Inserts a vector record into the collection, and return `VectorID` if success.
/// * `record`: Vector record to insert.
pub fn insert(&mut self, record: &Record) -> Result<VectorID, Error> {
// Ensure the number of records is within the limit.
if self.slots.len() == u32::MAX as usize {
return Err(Error::collection_limit());
}
// Ensure the vector dimension matches the collection config.
// If it's the first record, set the dimension.
if self.vectors.is_empty() && self.dimension == 0 {
self.dimension = record.vector.len();
} else if record.vector.len() != self.dimension {
let len = record.vector.len();
let err = Error::invalid_dimension(len, self.dimension);
return Err(err);
}
// Create a new vector ID using the next available slot.
let id: VectorID = self.slots.len().into();
// Insert the new vector and data.
self.vectors.insert(id, record.vector.clone());
self.data.insert(id, record.data.clone());
// Add new vector id to the slots.
self.slots.push(id);
// Update the collection count.
self.count += 1;
// This operation is last because it depends on
// the updated vectors data.
self.insert_to_layers(&[id]);
Ok(id)
}
#[cfg(feature = "py")]
#[pyo3(name = "insert_many")]
fn py_insert_many(
&mut self,
records: Vec<Record>,
) -> Result<Vec<VectorID>, Error> {
let ids = self.insert_many(&records)?;
Ok(ids)
}
/// Deletes a vector record from the collection.
/// * `id`: Vector ID to delete.
pub fn delete(&mut self, id: &VectorID) -> Result<(), Error> {
// Ensure the vector ID exists in the collection.
if !self.contains(id) {
return Err(Error::record_not_found());
}
self.delete_from_layers(&[*id]);
// Update the collection data.
self.vectors.remove(id);
self.data.remove(id);
// Make the slot invalid so it won't be used again.
self.slots[id.0 as usize] = INVALID;
// Update the collection count.
self.count -= 1;
Ok(())
}
/// Returns vector records in the collection as a HashMap.
pub fn list(&self) -> Result<HashMap<VectorID, Record>, Error> {
// Early return if the collection is empty.
if self.vectors.is_empty() {
return Ok(HashMap::new());
}
// Map the vectors to a hashmap of records.
let mapper = |(id, vector): (&VectorID, &Vector)| {
let data = self.data[id].clone();
let record = Record::new(vector, &data);
(*id, record)
};
let records = self.vectors.par_iter().map(mapper).collect();
Ok(records)
}
/// Returns the vector record associated with the ID.
/// * `id`: Vector ID to retrieve.
pub fn get(&self, id: &VectorID) -> Result<Record, Error> {
if !self.contains(id) {
return Err(Error::record_not_found());
}
let vector = self.vectors[id].clone();
let data = self.data[id].clone();
Ok(Record::new(&vector, &data))
}
/// Updates a vector record in the collection.
/// * `id`: Vector ID to update.
/// * `record`: New vector record.
pub fn update(
&mut self,
id: &VectorID,
record: &Record,
) -> Result<(), Error> {
if !self.contains(id) {
return Err(Error::record_not_found());
}
// Validate the new vector dimension.
self.validate_dimension(&record.vector)?;
// Remove the old vector from the index layers.
self.delete_from_layers(&[*id]);
// Insert the updated vector and data.
self.vectors.insert(*id, record.vector.clone());
self.data.insert(*id, record.data.clone());
self.insert_to_layers(&[*id]);
Ok(())
}
/// Searches the collection for the nearest neighbors.
/// * `vector`: Vector to search.
/// * `n`: Number of neighbors to return.
pub fn search(
&self,
vector: &Vector,
n: usize,
) -> Result<Vec<SearchResult>, Error> {
let mut search = Search::default();
// Early return if the collection is empty.
if self.vectors.is_empty() {
return Ok(vec![]);
}
// Ensure the vector dimension matches the collection dimension.
self.validate_dimension(vector)?;
// Find the first valid vector ID from the slots.
let slots_iter = self.slots.as_slice().into_par_iter();
let vector_id = match slots_iter.find_first(|id| id.is_valid()) {
Some(id) => id,
None => return Err("Unable to initiate search.".into()),
};
search.visited.resize_capacity(self.vectors.len());
search.push(vector_id, vector, &self.vectors);
for layer in LayerID(self.upper_layers.len()).descend() {
search.ef = if layer.is_zero() { self.config.ef_search } else { 5 };
if layer.0 == 0 {
let layer = self.base_layer.as_slice();
search.search(layer, vector, &self.vectors, M * 2);
} else {
let layer = self.upper_layers[layer.0 - 1].as_slice();
search.search(layer, vector, &self.vectors, M);
}
if !layer.is_zero() {
search.cull();
}
}
let map_result = |candidate: Candidate| {
let id = candidate.vector_id.0;
let distance = candidate.distance.0;
let data = self.data[&candidate.vector_id].clone();
SearchResult { id, distance, data }
};
// Get relevant results and truncate the list.
let res = search.iter().map(map_result).collect();
let mut relevant = self.truncate_irrelevant_result(res);
relevant.truncate(n);
Ok(relevant)
}
/// Searches the collection for the true nearest neighbors.
/// * `vector`: Vector to search.
/// * `n`: Number of neighbors to return.
pub fn true_search(
&self,
vector: &Vector,
n: usize,
) -> Result<Vec<SearchResult>, Error> {
let mut nearest = Vec::with_capacity(self.vectors.len());
// Ensure the vector dimension matches the collection dimension.
self.validate_dimension(vector)?;
// Calculate the distance between the query and each record.
// Then, create a search result for each record.
for (id, vec) in self.vectors.iter() {
let distance = self.config.distance.calculate(vector, vec);
let data = self.data[id].clone();
let res = SearchResult { id: id.0, distance, data };
nearest.push(res);
}
// Sort the nearest neighbors by distance.
nearest.sort_by(|a, b| a.distance.partial_cmp(&b.distance).unwrap());
// Remove irrelevant results and truncate the list.
let mut res = self.truncate_irrelevant_result(nearest);
res.truncate(n);
Ok(res)
}
#[cfg(feature = "py")]
#[getter(config)]
fn py_config(&self) -> Config {
self.config.clone()
}
#[cfg(feature = "py")]
#[getter(dimension)]
fn py_dimension(&self) -> usize {
self.dimension
}
#[cfg(feature = "py")]
#[setter(dimension)]
fn py_set_dimension(&mut self, dimension: usize) -> Result<(), Error> {
self.set_dimension(dimension)
}
#[cfg(feature = "py")]
#[getter(relevancy)]
fn py_relevancy(&self) -> f32 {
self.relevancy
}
#[cfg(feature = "py")]
#[setter(relevancy)]
fn py_set_relevancy(&mut self, relevancy: f32) {
self.relevancy = relevancy;
}
/// Returns the number of vector records in the collection.
pub fn len(&self) -> usize {
self.count
}
/// Returns true if the collection is empty.
pub fn is_empty(&self) -> bool {
self.count == 0
}
/// Checks if the collection contains a vector ID.
/// * `id`: Vector ID to check.
pub fn contains(&self, id: &VectorID) -> bool {
self.vectors.contains_key(id)
}
fn __len__(&self) -> usize {
self.len()
}
}
impl Collection {
/// Creates an empty collection with the given configuration.
pub fn new(config: &Config) -> Self {
Self {
count: 0,
dimension: 0,
relevancy: -1.0,
config: config.clone(),
data: HashMap::new(),
vectors: HashMap::new(),
slots: vec![],
base_layer: vec![],
upper_layers: vec![],
}
}
/// Builds the collection index from vector records.
/// * `config`: Collection configuration.
/// * `records`: List of vectors to build the index from.
pub fn build(config: &Config, records: &[Record]) -> Result<Self, Error> {
if records.is_empty() {
return Ok(Self::new(config));
}
// Ensure the number of records is within the limit.
if records.len() >= u32::MAX as usize {
let message = format!(
"The collection record limit is {}. Given: {}",
u32::MAX,
records.len()
);
return Err(message.into());
}
// Ensure that the vector dimension is consistent.
let dimension = records[0].vector.len();
if records.par_iter().any(|i| i.vector.len() != dimension) {
let message = format!(
"The vector dimension is inconsistent. Expected: {}.",
dimension
);
return Err(message.into());
}
// Find the number of layers.
let mut len = records.len();
let mut layers = Vec::new();
loop {
let next = (len as f32 * config.ml) as usize;
if next < M {
break;
}
layers.push((len - next, len));
len = next;
}
layers.push((len, len));
layers.reverse();
let num_layers = layers.len();
let top_layer = LayerID(num_layers - 1);
// Give all vectors a random layer and sort the list of nodes
// by descending order for construction.
// This allows us to copy higher layers to lower layers as
// construction progresses, while preserving randomness in
// each point's layer and insertion order.
let vectors = records
.par_iter()
.enumerate()
.map(|(i, item)| (i.into(), item.vector.clone()))
.collect::<HashMap<VectorID, Vector>>();
// Figure out how many nodes will go on each layer.
// This helps us allocate memory capacity for each
// layer in advance, and also helps enable batch
// insertion of points.
let mut ranges = Vec::with_capacity(top_layer.0);
for (i, (size, cumulative)) in layers.into_iter().enumerate() {
let start = cumulative - size;
let layer_id = LayerID(num_layers - i - 1);
let value = max(start, 1)..cumulative;
ranges.push((layer_id, value));
}
// Create index constructor.
let search_pool = SearchPool::new(vectors.len());
let mut upper_layers = vec![vec![]; top_layer.0];
let base_layer = vectors
.par_iter()
.map(|_| RwLock::new(BaseNode::default()))
.collect::<Vec<_>>();
let state = IndexConstruction {
base_layer: &base_layer,
search_pool,
top_layer,
vectors: &vectors,
config,
};
// Initialize data for layers.
for (layer, range) in ranges {
let end = range.end;
range.into_par_iter().for_each(|i: usize| {
state.insert(&i.into(), &layer, &upper_layers)
});
// Copy the base layer state to the upper layer.
if !layer.is_zero() {
(&state.base_layer[..end])
.into_par_iter()
.map(|zero| UpperNode::from_zero(&zero.read()))
.collect_into_vec(&mut upper_layers[layer.0 - 1]);
}
}
let data = records
.iter()
.enumerate()
.map(|(i, item)| (i.into(), item.data.clone()))
.collect();
// Unwrap the base nodes for the base layer.
let base_iter = base_layer.into_par_iter();
let base_layer = base_iter.map(|node| node.into_inner()).collect();
// Add IDs to the slots.
let slots = (0..vectors.len()).map(|i| i.into()).collect();
Ok(Self {
data,
vectors,
base_layer,
upper_layers,
slots,
dimension,
config: config.clone(),
count: records.len(),
relevancy: -1.0,
})
}
/// Inserts multiple vector records into the collection.
/// * `records`: List of vector records to insert.
pub fn insert_many(
&mut self,
records: &[Record],
) -> Result<Vec<VectorID>, Error> {
// Make sure the collection is not full after inserting.
if self.slots.len() + records.len() >= u32::MAX as usize {
return Err(Error::collection_limit());
}
// Sets the collection dimension if it's the first record.
if self.vectors.is_empty() && self.dimension == 0 {
self.dimension = records[0].vector.len();
}
// Validate the vector dimension against the collection.
if records.par_iter().any(|i| i.vector.len() != self.dimension) {
let message = format!(
"The vector dimension is inconsistent. Expected: {}.",
self.dimension
);
return Err(message.into());
}
// Create new vector IDs for the records.
let ids: Vec<VectorID> = {
let first_id = self.slots.len();
let final_id = self.slots.len() + records.len();
(first_id..final_id).map(|i| i.into()).collect()
};
// Store the new records vector and data.
for (id, record) in ids.iter().zip(records.iter()) {
self.vectors.insert(*id, record.vector.clone());
self.data.insert(*id, record.data.clone());
}
// Add new vector IDs to the slots.
self.slots.extend(ids.clone());
// Update the collection count.
self.count += records.len();
self.insert_to_layers(&ids);
Ok(ids)
}
/// Returns the configured vector dimension of the collection.
pub fn dimension(&self) -> usize {
self.dimension
}
/// Sets the vector dimension of the collection.
/// * `dimension`: New vector dimension.
pub fn set_dimension(&mut self, dimension: usize) -> Result<(), Error> {
// This can only be set if the collection is empty.
if !self.vectors.is_empty() {
return Err("The collection must be empty.".into());
}
self.dimension = dimension;
Ok(())
}
/// Sets the min/max relevancy for the search results.
/// * `relevancy`: Relevancy score.
pub fn set_relevancy(&mut self, relevancy: f32) {
self.relevancy = relevancy;
}
/// Validates a vector dimension against the collection's.
fn validate_dimension(&self, vector: &Vector) -> Result<(), Error> {
let found = vector.len();
let expected = self.dimension;
if found != expected {
Err(Error::invalid_dimension(found, expected))
} else {
Ok(())
}
}
/// Inserts vector IDs into the index layers.
fn insert_to_layers(&mut self, ids: &[VectorID]) {
// Add new nodes to the base layer.
for _ in 0..ids.len() {
self.base_layer.push(BaseNode::default());
}
let base_layer = self
.base_layer
.par_iter()
.map(|node| RwLock::new(*node))
.collect::<Vec<_>>();
let top_layer = match self.upper_layers.is_empty() {
true => LayerID(0),
false => LayerID(self.upper_layers.len()),
};
// Create a new index construction state.
let state = IndexConstruction {
base_layer: base_layer.as_slice(),
search_pool: SearchPool::new(self.vectors.len()),
top_layer,
vectors: &self.vectors,
config: &self.config,
};
// Insert all vectors into the state.
for id in ids {
state.insert(id, &top_layer, &self.upper_layers);
}
// Update base layer using the new state.
let iter = state.base_layer.into_par_iter();
self.base_layer = iter.map(|node| *node.read()).collect();
}
/// Removes vector IDs from all index layers.
fn delete_from_layers(&mut self, ids: &[VectorID]) {
// Remove the vectors from the base layer.
for id in ids {
let base_node = &mut self.base_layer[id.0 as usize];
let index = base_node.par_iter().position_first(|x| *x == *id);
if let Some(index) = index {
base_node.set(index, &INVALID);
}
}
// Remove the vector from the upper layers.
for layer in LayerID(self.upper_layers.len()).descend() {
let upper_layer = match layer.0 > 0 {
true => &mut self.upper_layers[layer.0 - 1],
false => break,
};
for id in ids {
let node = &mut upper_layer[id.0 as usize];
let index = node.0.par_iter().position_first(|x| *x == *id);
if let Some(index) = index {
node.set(index, &INVALID);
}
}
}
}
/// Truncates the search result based on the relevancy score.
fn truncate_irrelevant_result(
&self,
result: Vec<SearchResult>,
) -> Vec<SearchResult> {
// Early return if the relevancy score is not set.
if self.relevancy == -1.0 {
return result;
}
// For Euclidean distance, relevant results are those
// smaller than the relevancy score with best distance of 0.0.
if self.config.distance == Distance::Euclidean {
return result
.into_par_iter()
.filter(|r| r.distance <= self.relevancy)
.collect();
}
// For other distance metrics, like cosine similarity
// and dot product, the relevant results are above
// the relevancy score.
result
.into_par_iter()
.filter(|r| r.distance >= self.relevancy)
.collect()
}
}
/// A record containing a vector and its associated data.
#[cfg_attr(feature = "py", pyclass(module = "oasysdb.collection", get_all))]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Record {
/// The vector embedding.
pub vector: Vector,
/// Data associated with the vector.
pub data: Metadata,
}
// Any modifications to the Python methods should be reflected in:
// - py/tests/test_collection.py
// - py/oasysdb/collection.pyi
#[cfg(feature = "py")]
#[pymethods]
impl Record {
#[new]
fn py_new(vector: Vec<f32>, data: &PyAny) -> Self {
let vector = Vector::from(vector);
let data = Metadata::from(data);
Self::new(&vector, &data)
}
#[setter(vector)]
fn py_set_vector(&mut self, vector: Vec<f32>) {
self.vector = Vector::from(vector);
}
#[setter(data)]
fn py_set_data(&mut self, data: &PyAny) -> Result<(), Error> {
self.data = Metadata::from(data);
Ok(())
}
#[staticmethod]
#[pyo3(name = "random")]
fn py_random(dimension: usize) -> Self {
Record::random(dimension)
}
#[staticmethod]
#[pyo3(name = "many_random")]
fn py_many_random(dimension: usize, len: usize) -> Vec<Self> {
Record::many_random(dimension, len)
}
fn __repr__(&self) -> String {
format!("{:?}", self)
}
}
impl Record {
/// Creates a new record with a vector and data.
pub fn new(vector: &Vector, data: &Metadata) -> Self {
Self { vector: vector.clone(), data: data.clone() }
}
/// Generates a random record for testing.
/// * `dimension`: Vector dimension.
pub fn random(dimension: usize) -> Self {
let vector = Vector::random(dimension);
let data = random::<usize>().into();
Self::new(&vector, &data)
}
/// Generates many random records for testing.
/// * `dimension`: Vector dimension.
/// * `len`: Number of records to generate.
pub fn many_random(dimension: usize, len: usize) -> Vec<Self> {
(0..len).map(|_| Self::random(dimension)).collect()
}
}
/// The collection nearest neighbor search result.
#[cfg_attr(feature = "py", pyclass(module = "oasysdb.collection", get_all))]
#[derive(Serialize, Deserialize, Debug)]
pub struct SearchResult {
/// Vector ID.
pub id: u32,
/// Distance between the query to the collection vector.
pub distance: f32,
/// Data associated with the vector.
pub data: Metadata,
}
#[cfg(feature = "py")]
impl SearchResult {
fn __repr__(&self) -> String {
format!("{:?}", self)
}
}