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
//! HNSW index parameters and search quality profiles.
//!
//! This module contains configuration types for tuning HNSW index
//! performance and search quality.
use crate::quantization::StorageMode;
use serde::{Deserialize, Serialize};
/// HNSW index parameters for tuning performance and recall.
///
/// Use [`HnswParams::auto`] for automatic tuning based on vector dimension,
/// or create custom parameters for specific workloads.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct HnswParams {
/// Number of bi-directional links per node (M parameter).
/// Higher = better recall, more memory, slower insert.
pub max_connections: usize,
/// Size of dynamic candidate list during construction.
/// Higher = better recall, slower indexing.
pub ef_construction: usize,
/// Initial capacity (grows automatically if exceeded).
pub max_elements: usize,
/// Vector storage mode (Full, SQ8, or Binary).
/// SQ8 provides 4x memory reduction with ~1% recall loss.
#[serde(default)]
pub storage_mode: StorageMode,
}
impl Default for HnswParams {
fn default() -> Self {
Self::auto(768)
}
}
impl HnswParams {
/// Creates optimized parameters based on vector dimension.
///
/// These defaults are tuned for datasets up to 100K vectors with high recall targets.
/// For larger datasets, use [`HnswParams::for_dataset_size`].
#[must_use]
pub fn auto(dimension: usize) -> Self {
match dimension {
0..=256 => Self {
max_connections: 24,
ef_construction: 300,
max_elements: 100_000,
storage_mode: StorageMode::Full,
},
// 257+ dimensions: aggressive params targeting high recall
_ => Self {
max_connections: 32,
ef_construction: 400,
max_elements: 100_000,
storage_mode: StorageMode::Full,
},
}
}
/// Creates parameters optimized for a specific dataset size.
///
/// Targets high recall up to 1M vectors under benchmark-calibrated settings.
///
/// # Parameters by Scale
///
/// | Dataset Size | M | `ef_construction` | Target Recall |
/// |--------------|---|-------------------|---------------|
/// | ≤10K | 32 | 200 | ≥98% |
/// | ≤100K | 64 | 800 | ≥95% |
/// | ≤500K | 96 | 1200 | ≥95% |
/// | ≤1M | 128 | 1600 | ≥95% |
#[must_use]
pub fn for_dataset_size(dimension: usize, expected_vectors: usize) -> Self {
let (m_low, ef_low, m_high, ef_high, max_elems) = match expected_vectors {
0..=10_000 => (24, 200, 32, 400, 20_000),
10_001..=100_000 => (64, 800, 128, 1600, 150_000),
100_001..=500_000 => (96, 1200, 128, 2000, 750_000),
_ => (64, 800, 128, 1600, 1_500_000),
};
let (m, ef) = if dimension <= 256 {
(m_low, ef_low)
} else {
(m_high, ef_high)
};
Self {
max_connections: m,
ef_construction: ef,
max_elements: max_elems,
storage_mode: StorageMode::Full,
}
}
/// Creates parameters optimized for large datasets (100K+ vectors).
///
/// Higher M and `ef_construction` ensure good recall at scale.
/// For 1M+ vectors, use [`HnswParams::for_dataset_size`] instead.
#[must_use]
pub fn large_dataset(dimension: usize) -> Self {
Self::for_dataset_size(dimension, 500_000)
}
/// Creates parameters for 1 million vectors with high-recall target settings.
///
/// Based on `OpenSearch` 2025 research: M=128, `ef_construction`=1600.
#[must_use]
pub fn million_scale(dimension: usize) -> Self {
Self::for_dataset_size(dimension, 1_000_000)
}
/// Creates fast parameters optimized for insertion speed.
/// Lower recall but faster indexing. Best for small datasets (<10K).
#[must_use]
pub fn fast() -> Self {
Self {
max_connections: 16,
ef_construction: 150,
max_elements: 100_000,
storage_mode: StorageMode::Full,
}
}
/// Creates turbo parameters for maximum insert throughput.
///
/// **Target**: 5k+ vec/s (vs ~2k/s with `auto` params)
///
/// # Trade-offs
///
/// - **Recall**: ~85% (vs ≥95% with standard params)
/// - **Best for**: Bulk loading, development, benchmarking
/// - **Not recommended for**: Production search workloads
///
/// # Parameters
///
/// - `M = 12`: Minimal connections for fast graph construction
/// - `ef_construction = 100`: Low expansion factor
///
/// After bulk loading, consider rebuilding with higher params for production.
#[must_use]
pub fn turbo() -> Self {
Self {
max_connections: 12,
ef_construction: 100,
max_elements: 100_000,
storage_mode: StorageMode::Full,
}
}
/// Creates parameters optimized for high recall.
#[must_use]
pub fn high_recall(dimension: usize) -> Self {
let base = Self::auto(dimension);
Self {
max_connections: base.max_connections + 8,
ef_construction: base.ef_construction + 200,
..base
}
}
/// Creates parameters optimized for maximum recall.
#[must_use]
pub fn max_recall(dimension: usize) -> Self {
match dimension {
0..=256 => Self {
max_connections: 32,
ef_construction: 500,
max_elements: 100_000,
storage_mode: StorageMode::Full,
},
257..=768 => Self {
max_connections: 48,
ef_construction: 800,
max_elements: 100_000,
storage_mode: StorageMode::Full,
},
_ => Self {
max_connections: 64,
ef_construction: 1000,
max_elements: 100_000,
storage_mode: StorageMode::Full,
},
}
}
/// Creates parameters optimized for fast indexing.
#[must_use]
pub fn fast_indexing(dimension: usize) -> Self {
let base = Self::auto(dimension);
Self {
max_connections: (base.max_connections / 2).max(8),
ef_construction: base.ef_construction / 2,
..base
}
}
/// Creates custom parameters.
#[must_use]
pub const fn custom(
max_connections: usize,
ef_construction: usize,
max_elements: usize,
) -> Self {
Self {
max_connections,
ef_construction,
max_elements,
storage_mode: StorageMode::Full,
}
}
/// Creates parameters with SQ8 quantization for 4x memory reduction.
///
/// # Memory Savings
///
/// | Dimension | Full (f32) | SQ8 (u8) | Reduction |
/// |-----------|------------|----------|----------|
/// | 768 | 3 KB | 776 B | 4x |
/// | 1536 | 6 KB | 1.5 KB | 4x |
#[must_use]
pub fn with_sq8(dimension: usize) -> Self {
let mut params = Self::auto(dimension);
params.storage_mode = StorageMode::SQ8;
params
}
/// Creates parameters with binary quantization for 32x memory reduction.
/// Best for edge/IoT devices with limited RAM.
#[must_use]
pub fn with_binary(dimension: usize) -> Self {
let mut params = Self::auto(dimension);
params.storage_mode = StorageMode::Binary;
params
}
}
/// Search quality profile controlling the recall/latency tradeoff.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum SearchQuality {
/// Fast search with `ef_search=64`. ~92% recall, lowest latency.
Fast,
/// Balanced search with `ef_search=128`. ~99% recall, production default.
#[default]
Balanced,
/// Accurate search with `ef_search=512`. ~100% recall.
Accurate,
/// Perfect recall mode with `ef_search=4096` for guaranteed 100% recall.
/// Uses large candidate pool with exact SIMD re-ranking.
Perfect,
/// Custom `ef_search` value.
Custom(usize),
/// Adaptive `ef_search` that starts low and doubles if the query is "hard".
///
/// Uses a two-phase approach:
/// 1. Search with `min_ef`
/// 2. If result spread (`max_dist / min_dist`) exceeds a threshold, re-search
/// with doubled ef (up to `max_ef`)
///
/// For easy queries (dense cluster hits), this is 2-4x faster than fixed ef.
/// For hard queries, it gracefully degrades to `max_ef` with no recall loss.
Adaptive {
/// Minimum `ef_search` (starting point). Default: 32.
min_ef: usize,
/// Maximum `ef_search` (cap). Default: 512.
max_ef: usize,
},
/// Auto-tuned adaptive search based on collection statistics.
///
/// Computes optimal `min_ef` / `max_ef` from the collection's current size
/// and vector dimension, then delegates to the same two-phase adaptive
/// algorithm used by [`SearchQuality::Adaptive`].
///
/// This is the recommended quality setting for applications that want
/// good recall without manual ef tuning:
///
/// - Small collections (≤1K): conservative ef (fast)
/// - Medium collections (1K–100K): moderate ef (balanced)
/// - Large collections (100K+): aggressive ef (high recall)
/// - High dimensions (>512): additional exploration factor
///
/// # Example
///
/// ```rust,ignore
/// use velesdb_core::SearchQuality;
/// let results = index.search_with_quality(&query, 10, SearchQuality::AutoTune);
/// ```
AutoTune,
}
impl SearchQuality {
/// Returns the `ef_search` value for this quality profile.
///
/// # Large-scale optimization (v0.9+)
///
/// - **Accurate**: 512 base (was 256), scales with k×16 for ≥95% recall at 100K+
/// - **Perfect**: 4096 base (was 2048), scales with k×100 for guaranteed 100% at 100K+
/// - **Adaptive**: returns `min_ef` (first phase); caller handles second phase
#[must_use]
pub fn ef_search(&self, k: usize) -> usize {
match self {
Self::Fast => 64.max(k * 2),
// AutoTune: resolved at search time via auto_ef_range(); fallback
// to Balanced for contexts that call ef_search() without collection info.
Self::Balanced | Self::AutoTune => 128.max(k * 4),
// Increased from 256 to 512 for better recall at 100K+ scale
Self::Accurate => 512.max(k * 16),
// Increased from 2048 to 4096 for guaranteed 100% recall at 100K+
Self::Perfect => 4096.max(k * 100),
Self::Custom(ef) => (*ef).max(k),
// Adaptive: start with min_ef (first phase)
Self::Adaptive { min_ef, .. } => (*min_ef).max(k),
}
}
/// Returns `true` if this quality profile uses two-phase adaptive search.
#[must_use]
pub const fn is_adaptive(&self) -> bool {
matches!(self, Self::Adaptive { .. } | Self::AutoTune)
}
/// Returns the maximum ef for adaptive search, or `None` for fixed profiles.
#[must_use]
pub const fn adaptive_max_ef(&self) -> Option<usize> {
match self {
Self::Adaptive { max_ef, .. } => Some(*max_ef),
_ => None,
}
}
}