vxdb-server 0.4.0__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/Cargo.lock +3 -3
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/Cargo.toml +1 -1
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/PKG-INFO +1 -1
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/collection.rs +23 -2
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/hybrid.rs +56 -9
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/index/flat.rs +7 -1
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/index/hnsw.rs +62 -3
- vxdb_server-0.5.0/crates/vxdb-core/src/index/mod.rs +33 -0
- vxdb_server-0.4.0/crates/vxdb-core/src/index/mod.rs +0 -16
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/README.md +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/Cargo.toml +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/distance/cosine.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/distance/dot.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/distance/euclidean.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/distance/mod.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/error.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/filter.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/lib.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/storage/metadata.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/storage/mmap.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/storage/mod.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/storage/persistent.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/storage/wal.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-core/src/types.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-server/Cargo.toml +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-server/README.md +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-server/src/config.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-server/src/main.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/crates/vxdb-server/src/routes.rs +0 -0
- {vxdb_server-0.4.0 → vxdb_server-0.5.0}/pyproject.toml +0 -0
|
@@ -1837,7 +1837,7 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
|
|
1837
1837
|
|
|
1838
1838
|
[[package]]
|
|
1839
1839
|
name = "vxdb-core"
|
|
1840
|
-
version = "0.
|
|
1840
|
+
version = "0.5.0"
|
|
1841
1841
|
dependencies = [
|
|
1842
1842
|
"memmap2",
|
|
1843
1843
|
"parking_lot",
|
|
@@ -1851,7 +1851,7 @@ dependencies = [
|
|
|
1851
1851
|
|
|
1852
1852
|
[[package]]
|
|
1853
1853
|
name = "vxdb-python"
|
|
1854
|
-
version = "0.
|
|
1854
|
+
version = "0.5.0"
|
|
1855
1855
|
dependencies = [
|
|
1856
1856
|
"pyo3",
|
|
1857
1857
|
"serde_json",
|
|
@@ -1860,7 +1860,7 @@ dependencies = [
|
|
|
1860
1860
|
|
|
1861
1861
|
[[package]]
|
|
1862
1862
|
name = "vxdb-server"
|
|
1863
|
-
version = "0.
|
|
1863
|
+
version = "0.5.0"
|
|
1864
1864
|
dependencies = [
|
|
1865
1865
|
"axum",
|
|
1866
1866
|
"clap",
|
|
@@ -177,7 +177,17 @@ impl Collection {
|
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
pub fn query(&self, vector: &[f32], top_k: usize) -> VexResult<Vec<SearchResult>> {
|
|
180
|
-
|
|
180
|
+
self.query_ef(vector, top_k, None)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/// Vector search with an optional per-query `ef_search` override (HNSW only).
|
|
184
|
+
pub fn query_ef(
|
|
185
|
+
&self,
|
|
186
|
+
vector: &[f32],
|
|
187
|
+
top_k: usize,
|
|
188
|
+
ef_search: Option<usize>,
|
|
189
|
+
) -> VexResult<Vec<SearchResult>> {
|
|
190
|
+
let mut results = self.index.search_ef(vector, top_k, ef_search)?;
|
|
181
191
|
self.attach_documents(&mut results)?;
|
|
182
192
|
Ok(results)
|
|
183
193
|
}
|
|
@@ -187,9 +197,20 @@ impl Collection {
|
|
|
187
197
|
vector: &[f32],
|
|
188
198
|
top_k: usize,
|
|
189
199
|
filter: &Filter,
|
|
200
|
+
) -> VexResult<Vec<SearchResult>> {
|
|
201
|
+
self.query_with_filter_ef(vector, top_k, filter, None)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/// Filtered vector search with an optional per-query `ef_search` override.
|
|
205
|
+
pub fn query_with_filter_ef(
|
|
206
|
+
&self,
|
|
207
|
+
vector: &[f32],
|
|
208
|
+
top_k: usize,
|
|
209
|
+
filter: &Filter,
|
|
210
|
+
ef_search: Option<usize>,
|
|
190
211
|
) -> VexResult<Vec<SearchResult>> {
|
|
191
212
|
let fetch_k = (top_k * 10).max(100);
|
|
192
|
-
let candidates = self.index.
|
|
213
|
+
let candidates = self.index.search_ef(vector, fetch_k, ef_search)?;
|
|
193
214
|
let mut filtered: Vec<SearchResult> = candidates
|
|
194
215
|
.into_iter()
|
|
195
216
|
.filter(|r| filter.matches(&r.metadata))
|
|
@@ -10,8 +10,8 @@ const B: f64 = 0.75;
|
|
|
10
10
|
pub struct Bm25Index {
|
|
11
11
|
/// doc_id -> tokenized terms
|
|
12
12
|
docs: HashMap<String, Vec<String>>,
|
|
13
|
-
/// term ->
|
|
14
|
-
inverted: HashMap<String,
|
|
13
|
+
/// term -> (doc_id -> term frequency in that doc)
|
|
14
|
+
inverted: HashMap<String, HashMap<String, usize>>,
|
|
15
15
|
/// total number of documents
|
|
16
16
|
doc_count: usize,
|
|
17
17
|
/// average document length
|
|
@@ -37,10 +37,12 @@ impl Bm25Index {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
for token in &tokens {
|
|
40
|
-
self
|
|
40
|
+
*self
|
|
41
|
+
.inverted
|
|
41
42
|
.entry(token.clone())
|
|
42
43
|
.or_default()
|
|
43
|
-
.
|
|
44
|
+
.entry(id.to_string())
|
|
45
|
+
.or_insert(0) += 1;
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
self.docs.insert(id.to_string(), tokens);
|
|
@@ -52,7 +54,7 @@ impl Bm25Index {
|
|
|
52
54
|
if let Some(tokens) = self.docs.remove(id) {
|
|
53
55
|
for token in &tokens {
|
|
54
56
|
if let Some(posting) = self.inverted.get_mut(token) {
|
|
55
|
-
posting.
|
|
57
|
+
posting.remove(id);
|
|
56
58
|
if posting.is_empty() {
|
|
57
59
|
self.inverted.remove(token);
|
|
58
60
|
}
|
|
@@ -92,10 +94,9 @@ impl Bm25Index {
|
|
|
92
94
|
// IDF: log((N - df + 0.5) / (df + 0.5) + 1)
|
|
93
95
|
let idf = ((n - df + 0.5) / (df + 0.5) + 1.0).ln();
|
|
94
96
|
|
|
95
|
-
for doc_id in posting {
|
|
96
|
-
let
|
|
97
|
-
let
|
|
98
|
-
let tf = doc_tokens.iter().filter(|t| *t == token).count() as f64;
|
|
97
|
+
for (doc_id, &tf) in posting {
|
|
98
|
+
let doc_len = self.docs[doc_id.as_str()].len() as f64;
|
|
99
|
+
let tf = tf as f64;
|
|
99
100
|
|
|
100
101
|
// BM25 term score
|
|
101
102
|
let tf_norm = (tf * (K1 + 1.0)) / (tf + K1 * (1.0 - B + B * doc_len / self.avg_doc_len));
|
|
@@ -198,6 +199,52 @@ mod tests {
|
|
|
198
199
|
assert!(results.iter().all(|r| r.0 != "none"));
|
|
199
200
|
}
|
|
200
201
|
|
|
202
|
+
#[test]
|
|
203
|
+
fn test_bm25_df_counts_documents_not_occurrences() {
|
|
204
|
+
// "common" occurs 5x in 2 of 3 docs. df must be 2 (documents), not 10
|
|
205
|
+
// (occurrences) — an inflated df flips the IDF negative and penalizes
|
|
206
|
+
// exactly the docs that match the query.
|
|
207
|
+
let mut idx = Bm25Index::new();
|
|
208
|
+
idx.insert("doc1", "rare word appears here today");
|
|
209
|
+
idx.insert("doc2", "rare common common common common common filler");
|
|
210
|
+
idx.insert("doc3", "common common common common common other filler");
|
|
211
|
+
|
|
212
|
+
let results = idx.search("rare common", 10);
|
|
213
|
+
assert_eq!(
|
|
214
|
+
results[0].0, "doc2",
|
|
215
|
+
"doc matching both query terms must rank first, got {results:?}"
|
|
216
|
+
);
|
|
217
|
+
assert!(
|
|
218
|
+
results.iter().all(|(_, s)| *s >= 0.0),
|
|
219
|
+
"BM25 scores must be non-negative, got {results:?}"
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
#[test]
|
|
224
|
+
fn test_bm25_term_repetition_saturates() {
|
|
225
|
+
// A doc matching all three query terms once must beat a doc that
|
|
226
|
+
// repeats a single query term eight times (K1 saturation).
|
|
227
|
+
let mut idx = Bm25Index::new();
|
|
228
|
+
idx.insert(
|
|
229
|
+
"needle",
|
|
230
|
+
"the quick brown fox jumps over lazy dogs in autumn weather today",
|
|
231
|
+
);
|
|
232
|
+
idx.insert(
|
|
233
|
+
"spam",
|
|
234
|
+
"fox fox fox fox fox fox fox fox with some other filler words here",
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
let results = idx.search("quick brown fox", 10);
|
|
238
|
+
assert_eq!(
|
|
239
|
+
results[0].0, "needle",
|
|
240
|
+
"doc matching all query terms must outrank single-term repetition, got {results:?}"
|
|
241
|
+
);
|
|
242
|
+
assert!(
|
|
243
|
+
results.iter().all(|(_, s)| *s >= 0.0),
|
|
244
|
+
"BM25 scores must be non-negative, got {results:?}"
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
201
248
|
#[test]
|
|
202
249
|
fn test_bm25_empty_query() {
|
|
203
250
|
let mut idx = Bm25Index::new();
|
|
@@ -54,7 +54,13 @@ impl VectorIndex for FlatIndex {
|
|
|
54
54
|
Ok(())
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
// Exact brute-force search: the ef_search hint is irrelevant and ignored.
|
|
58
|
+
fn search_ef(
|
|
59
|
+
&self,
|
|
60
|
+
query: &[f32],
|
|
61
|
+
top_k: usize,
|
|
62
|
+
_ef_search: Option<usize>,
|
|
63
|
+
) -> VexResult<Vec<SearchResult>> {
|
|
58
64
|
if query.len() != self.dimension {
|
|
59
65
|
return Err(VexError::DimensionMismatch {
|
|
60
66
|
expected: self.dimension,
|
|
@@ -368,7 +368,12 @@ impl VectorIndex for HnswIndex {
|
|
|
368
368
|
Ok(())
|
|
369
369
|
}
|
|
370
370
|
|
|
371
|
-
fn
|
|
371
|
+
fn search_ef(
|
|
372
|
+
&self,
|
|
373
|
+
query: &[f32],
|
|
374
|
+
top_k: usize,
|
|
375
|
+
ef_search: Option<usize>,
|
|
376
|
+
) -> VexResult<Vec<SearchResult>> {
|
|
372
377
|
if query.len() != self.dimension {
|
|
373
378
|
return Err(VexError::DimensionMismatch {
|
|
374
379
|
expected: self.dimension,
|
|
@@ -395,8 +400,8 @@ impl VectorIndex for HnswIndex {
|
|
|
395
400
|
current_layer -= 1;
|
|
396
401
|
}
|
|
397
402
|
|
|
398
|
-
// Search layer 0 with ef_search
|
|
399
|
-
let ef = self.config.ef_search.max(top_k);
|
|
403
|
+
// Search layer 0 with the per-query ef_search override, or the default.
|
|
404
|
+
let ef = ef_search.unwrap_or(self.config.ef_search).max(top_k);
|
|
400
405
|
let candidates = self.search_layer(query, vec![ep], ef, 0, &mut visited);
|
|
401
406
|
|
|
402
407
|
let results: Vec<SearchResult> = candidates
|
|
@@ -607,6 +612,60 @@ mod tests {
|
|
|
607
612
|
);
|
|
608
613
|
}
|
|
609
614
|
|
|
615
|
+
#[test]
|
|
616
|
+
fn test_per_query_ef_search_override() {
|
|
617
|
+
// A per-query ef_search overrides the index's configured default without
|
|
618
|
+
// rebuilding the graph. Higher ef explores more candidates, so recall
|
|
619
|
+
// rises; passing None falls back to the configured ef_search.
|
|
620
|
+
use rand::rngs::StdRng;
|
|
621
|
+
use rand::SeedableRng;
|
|
622
|
+
|
|
623
|
+
let dim = 128;
|
|
624
|
+
let n = 3000;
|
|
625
|
+
let n_queries = 100;
|
|
626
|
+
let top_k = 10;
|
|
627
|
+
|
|
628
|
+
let mut rng = StdRng::seed_from_u64(0xBEEF);
|
|
629
|
+
// Configured default ef_search is deliberately low so the override is visible.
|
|
630
|
+
let mut hnsw = HnswIndex::new(dim, Metric::Euclidean, HnswConfig::new(16, 200, 16));
|
|
631
|
+
let mut flat = FlatIndex::new(dim, Metric::Euclidean);
|
|
632
|
+
for i in 0..n {
|
|
633
|
+
let v: Vec<f32> = (0..dim).map(|_| rng.gen::<f32>()).collect();
|
|
634
|
+
let id = format!("v{}", i);
|
|
635
|
+
hnsw.insert(id.clone(), v.clone(), HashMap::new()).unwrap();
|
|
636
|
+
flat.insert(id, v, HashMap::new()).unwrap();
|
|
637
|
+
}
|
|
638
|
+
let queries: Vec<Vec<f32>> = (0..n_queries)
|
|
639
|
+
.map(|_| (0..dim).map(|_| rng.gen::<f32>()).collect())
|
|
640
|
+
.collect();
|
|
641
|
+
|
|
642
|
+
let recall = |ef: Option<usize>| -> f64 {
|
|
643
|
+
let mut total = 0.0f64;
|
|
644
|
+
for q in &queries {
|
|
645
|
+
let h: HashSet<String> =
|
|
646
|
+
hnsw.search_ef(q, top_k, ef).unwrap().into_iter().map(|r| r.id).collect();
|
|
647
|
+
let f: HashSet<String> =
|
|
648
|
+
flat.search(q, top_k).unwrap().into_iter().map(|r| r.id).collect();
|
|
649
|
+
total += h.intersection(&f).count() as f64 / top_k as f64;
|
|
650
|
+
}
|
|
651
|
+
total / n_queries as f64
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
let recall_low = recall(Some(16));
|
|
655
|
+
let recall_high = recall(Some(400));
|
|
656
|
+
let recall_default = recall(None);
|
|
657
|
+
|
|
658
|
+
eprintln!("per-query ef: 16 -> {recall_low:.3}, 400 -> {recall_high:.3}");
|
|
659
|
+
assert!(
|
|
660
|
+
recall_high > recall_low + 0.05,
|
|
661
|
+
"raising per-query ef_search should lift recall: {recall_low:.3} -> {recall_high:.3}"
|
|
662
|
+
);
|
|
663
|
+
assert!(
|
|
664
|
+
(recall_default - recall_low).abs() < 1e-9,
|
|
665
|
+
"search_ef(None) must reuse the configured ef_search (=16)"
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
|
|
610
669
|
#[test]
|
|
611
670
|
fn test_single_element() {
|
|
612
671
|
let mut idx = make_hnsw(2);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
pub mod flat;
|
|
2
|
+
pub mod hnsw;
|
|
3
|
+
|
|
4
|
+
use crate::error::VexResult;
|
|
5
|
+
use crate::types::{Metadata, SearchResult, VectorData};
|
|
6
|
+
|
|
7
|
+
pub trait VectorIndex: Send + Sync {
|
|
8
|
+
fn insert(&mut self, id: String, vector: VectorData, metadata: Metadata) -> VexResult<()>;
|
|
9
|
+
|
|
10
|
+
/// Search with an optional per-query `ef_search` override.
|
|
11
|
+
///
|
|
12
|
+
/// Higher `ef_search` explores more of the graph, trading latency for recall.
|
|
13
|
+
/// `None` uses the index's configured default. Exact indexes (flat) ignore
|
|
14
|
+
/// the hint and always return exact results.
|
|
15
|
+
fn search_ef(
|
|
16
|
+
&self,
|
|
17
|
+
query: &[f32],
|
|
18
|
+
top_k: usize,
|
|
19
|
+
ef_search: Option<usize>,
|
|
20
|
+
) -> VexResult<Vec<SearchResult>>;
|
|
21
|
+
|
|
22
|
+
/// Search with the index's configured default `ef_search`.
|
|
23
|
+
fn search(&self, query: &[f32], top_k: usize) -> VexResult<Vec<SearchResult>> {
|
|
24
|
+
self.search_ef(query, top_k, None)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn delete(&mut self, id: &str) -> VexResult<bool>;
|
|
28
|
+
fn len(&self) -> usize;
|
|
29
|
+
fn is_empty(&self) -> bool {
|
|
30
|
+
self.len() == 0
|
|
31
|
+
}
|
|
32
|
+
fn contains(&self, id: &str) -> bool;
|
|
33
|
+
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
pub mod flat;
|
|
2
|
-
pub mod hnsw;
|
|
3
|
-
|
|
4
|
-
use crate::error::VexResult;
|
|
5
|
-
use crate::types::{Metadata, SearchResult, VectorData};
|
|
6
|
-
|
|
7
|
-
pub trait VectorIndex: Send + Sync {
|
|
8
|
-
fn insert(&mut self, id: String, vector: VectorData, metadata: Metadata) -> VexResult<()>;
|
|
9
|
-
fn search(&self, query: &[f32], top_k: usize) -> VexResult<Vec<SearchResult>>;
|
|
10
|
-
fn delete(&mut self, id: &str) -> VexResult<bool>;
|
|
11
|
-
fn len(&self) -> usize;
|
|
12
|
-
fn is_empty(&self) -> bool {
|
|
13
|
-
self.len() == 0
|
|
14
|
-
}
|
|
15
|
-
fn contains(&self, id: &str) -> bool;
|
|
16
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|