qdrant_client/builders/hnsw_config_diff_builder.rs
1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct HnswConfigDiffBuilder {
6 ///
7 /// Number of edges per node in the index graph. Larger the value - more accurate the search, more space required.
8 pub(crate) m: Option<Option<u64>>,
9 ///
10 /// Number of neighbours to consider during the index building. Larger the value - more accurate the search, more time required to build the index.
11 pub(crate) ef_construct: Option<Option<u64>>,
12 ///
13 /// Minimal size (in KiloBytes) of vectors for additional payload-based indexing.
14 /// If the payload chunk is smaller than `full_scan_threshold` additional indexing won't be used -
15 /// in this case full-scan search should be preferred by query planner and additional indexing is not required.
16 /// Note: 1 Kb = 1 vector of size 256
17 pub(crate) full_scan_threshold: Option<Option<u64>>,
18 ///
19 /// Number of parallel threads used for background index building.
20 /// If 0 - automatically select from 8 to 16.
21 /// Best to keep between 8 and 16 to prevent likelihood of building broken/inefficient HNSW graphs.
22 /// On small CPUs, less threads are used.
23 pub(crate) max_indexing_threads: Option<Option<u64>>,
24 ///
25 /// Store HNSW index on disk. If set to false, the index will be stored in RAM.
26 pub(crate) on_disk: Option<Option<bool>>,
27 ///
28 /// Number of additional payload-aware links per node in the index graph. If not set - regular M parameter will be used.
29 pub(crate) payload_m: Option<Option<u64>>,
30 ///
31 /// Store copies of original and quantized vectors within the HNSW index file. Default: false.
32 /// Enabling this option will trade the search speed for disk usage by reducing amount of
33 /// random seeks during the search.
34 /// Requires quantized vectors to be enabled. Multi-vectors are not supported.
35 pub(crate) inline_storage: Option<Option<bool>>,
36 ///
37 /// Memory placement of the HNSW graph.
38 pub(crate) memory: Option<Option<i32>>,
39}
40#[allow(clippy::all)]
41#[allow(clippy::derive_partial_eq_without_eq)]
42impl HnswConfigDiffBuilder {
43 ///
44 /// Number of edges per node in the index graph. Larger the value - more accurate the search, more space required.
45 pub fn m(self, value: u64) -> Self {
46 let mut new = self;
47 new.m = Option::Some(Option::Some(value));
48 new
49 }
50 ///
51 /// Number of neighbours to consider during the index building. Larger the value - more accurate the search, more time required to build the index.
52 pub fn ef_construct(self, value: u64) -> Self {
53 let mut new = self;
54 new.ef_construct = Option::Some(Option::Some(value));
55 new
56 }
57 ///
58 /// Minimal size (in KiloBytes) of vectors for additional payload-based indexing.
59 /// If the payload chunk is smaller than `full_scan_threshold` additional indexing won't be used -
60 /// in this case full-scan search should be preferred by query planner and additional indexing is not required.
61 /// Note: 1 Kb = 1 vector of size 256
62 pub fn full_scan_threshold(self, value: u64) -> Self {
63 let mut new = self;
64 new.full_scan_threshold = Option::Some(Option::Some(value));
65 new
66 }
67 ///
68 /// Number of parallel threads used for background index building.
69 /// If 0 - automatically select from 8 to 16.
70 /// Best to keep between 8 and 16 to prevent likelihood of building broken/inefficient HNSW graphs.
71 /// On small CPUs, less threads are used.
72 pub fn max_indexing_threads(self, value: u64) -> Self {
73 let mut new = self;
74 new.max_indexing_threads = Option::Some(Option::Some(value));
75 new
76 }
77 ///
78 /// Store HNSW index on disk. If set to false, the index will be stored in RAM.
79 ///
80 /// Deprecated since 1.19.0, use [`memory`](Self::memory) instead.
81 pub fn on_disk(self, value: bool) -> Self {
82 let mut new = self;
83 new.on_disk = Option::Some(Option::Some(value));
84 new
85 }
86 ///
87 /// Number of additional payload-aware links per node in the index graph. If not set - regular M parameter will be used.
88 pub fn payload_m(self, value: u64) -> Self {
89 let mut new = self;
90 new.payload_m = Option::Some(Option::Some(value));
91 new
92 }
93 ///
94 /// Store copies of original and quantized vectors within the HNSW index file. Default: false.
95 /// Enabling this option will trade the search speed for disk usage by reducing amount of
96 /// random seeks during the search.
97 /// Requires quantized vectors to be enabled. Multi-vectors are not supported.
98 pub fn inline_storage(self, value: bool) -> Self {
99 let mut new = self;
100 new.inline_storage = Option::Some(Option::Some(value));
101 new
102 }
103 ///
104 /// Memory placement of the HNSW graph.
105 /// Overrides the deprecated `on_disk` flag if both are set.
106 pub fn memory<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
107 let mut new = self;
108 new.memory = Option::Some(Option::Some(value.into()));
109 new
110 }
111
112 #[allow(deprecated)]
113 fn build_inner(self) -> Result<HnswConfigDiff, std::convert::Infallible> {
114 Ok(HnswConfigDiff {
115 m: match self.m {
116 Some(value) => value,
117 None => core::default::Default::default(),
118 },
119 ef_construct: match self.ef_construct {
120 Some(value) => value,
121 None => core::default::Default::default(),
122 },
123 full_scan_threshold: match self.full_scan_threshold {
124 Some(value) => value,
125 None => core::default::Default::default(),
126 },
127 max_indexing_threads: match self.max_indexing_threads {
128 Some(value) => value,
129 None => core::default::Default::default(),
130 },
131 on_disk: match self.on_disk {
132 Some(value) => value,
133 None => core::default::Default::default(),
134 },
135 payload_m: match self.payload_m {
136 Some(value) => value,
137 None => core::default::Default::default(),
138 },
139 inline_storage: match self.inline_storage {
140 Some(value) => value,
141 None => core::default::Default::default(),
142 },
143 memory: match self.memory {
144 Some(value) => value,
145 None => core::default::Default::default(),
146 },
147 })
148 }
149 /// Create an empty builder, with all fields set to `None` or `PhantomData`.
150 fn create_empty() -> Self {
151 Self {
152 m: core::default::Default::default(),
153 ef_construct: core::default::Default::default(),
154 full_scan_threshold: core::default::Default::default(),
155 max_indexing_threads: core::default::Default::default(),
156 on_disk: core::default::Default::default(),
157 payload_m: core::default::Default::default(),
158 inline_storage: core::default::Default::default(),
159 memory: core::default::Default::default(),
160 }
161 }
162}
163
164impl From<HnswConfigDiffBuilder> for HnswConfigDiff {
165 fn from(value: HnswConfigDiffBuilder) -> Self {
166 value.build_inner().unwrap_or_else(|_| {
167 panic!(
168 "Failed to convert {0} to {1}",
169 "HnswConfigDiffBuilder", "HnswConfigDiff"
170 )
171 })
172 }
173}
174
175impl HnswConfigDiffBuilder {
176 /// Builds the desired type. Can often be omitted.
177 pub fn build(self) -> HnswConfigDiff {
178 self.build_inner().unwrap_or_else(|_| {
179 panic!(
180 "Failed to build {0} into {1}",
181 "HnswConfigDiffBuilder", "HnswConfigDiff"
182 )
183 })
184 }
185}
186
187impl Default for HnswConfigDiffBuilder {
188 fn default() -> Self {
189 Self::create_empty()
190 }
191}