Skip to main content

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#[allow(clippy::all)]
38#[allow(clippy::derive_partial_eq_without_eq)]
39impl HnswConfigDiffBuilder {
40    ///
41    /// Number of edges per node in the index graph. Larger the value - more accurate the search, more space required.
42    pub fn m(self, value: u64) -> Self {
43        let mut new = self;
44        new.m = Option::Some(Option::Some(value));
45        new
46    }
47    ///
48    /// Number of neighbours to consider during the index building. Larger the value - more accurate the search, more time required to build the index.
49    pub fn ef_construct(self, value: u64) -> Self {
50        let mut new = self;
51        new.ef_construct = Option::Some(Option::Some(value));
52        new
53    }
54    ///
55    /// Minimal size (in KiloBytes) of vectors for additional payload-based indexing.
56    /// If the payload chunk is smaller than `full_scan_threshold` additional indexing won't be used -
57    /// in this case full-scan search should be preferred by query planner and additional indexing is not required.
58    /// Note: 1 Kb = 1 vector of size 256
59    pub fn full_scan_threshold(self, value: u64) -> Self {
60        let mut new = self;
61        new.full_scan_threshold = Option::Some(Option::Some(value));
62        new
63    }
64    ///
65    /// Number of parallel threads used for background index building.
66    /// If 0 - automatically select from 8 to 16.
67    /// Best to keep between 8 and 16 to prevent likelihood of building broken/inefficient HNSW graphs.
68    /// On small CPUs, less threads are used.
69    pub fn max_indexing_threads(self, value: u64) -> Self {
70        let mut new = self;
71        new.max_indexing_threads = Option::Some(Option::Some(value));
72        new
73    }
74    ///
75    /// Store HNSW index on disk. If set to false, the index will be stored in RAM.
76    pub fn on_disk(self, value: bool) -> Self {
77        let mut new = self;
78        new.on_disk = Option::Some(Option::Some(value));
79        new
80    }
81    ///
82    /// Number of additional payload-aware links per node in the index graph. If not set - regular M parameter will be used.
83    pub fn payload_m(self, value: u64) -> Self {
84        let mut new = self;
85        new.payload_m = Option::Some(Option::Some(value));
86        new
87    }
88    ///
89    /// Store copies of original and quantized vectors within the HNSW index file. Default: false.
90    /// Enabling this option will trade the search speed for disk usage by reducing amount of
91    /// random seeks during the search.
92    /// Requires quantized vectors to be enabled. Multi-vectors are not supported.
93    pub fn inline_storage(self, value: bool) -> Self {
94        let mut new = self;
95        new.inline_storage = Option::Some(Option::Some(value));
96        new
97    }
98
99    fn build_inner(self) -> Result<HnswConfigDiff, std::convert::Infallible> {
100        Ok(HnswConfigDiff {
101            m: match self.m {
102                Some(value) => value,
103                None => core::default::Default::default(),
104            },
105            ef_construct: match self.ef_construct {
106                Some(value) => value,
107                None => core::default::Default::default(),
108            },
109            full_scan_threshold: match self.full_scan_threshold {
110                Some(value) => value,
111                None => core::default::Default::default(),
112            },
113            max_indexing_threads: match self.max_indexing_threads {
114                Some(value) => value,
115                None => core::default::Default::default(),
116            },
117            on_disk: match self.on_disk {
118                Some(value) => value,
119                None => core::default::Default::default(),
120            },
121            payload_m: match self.payload_m {
122                Some(value) => value,
123                None => core::default::Default::default(),
124            },
125            inline_storage: match self.inline_storage {
126                Some(value) => value,
127                None => core::default::Default::default(),
128            },
129        })
130    }
131    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
132    fn create_empty() -> Self {
133        Self {
134            m: core::default::Default::default(),
135            ef_construct: core::default::Default::default(),
136            full_scan_threshold: core::default::Default::default(),
137            max_indexing_threads: core::default::Default::default(),
138            on_disk: core::default::Default::default(),
139            payload_m: core::default::Default::default(),
140            inline_storage: core::default::Default::default(),
141        }
142    }
143}
144
145impl From<HnswConfigDiffBuilder> for HnswConfigDiff {
146    fn from(value: HnswConfigDiffBuilder) -> Self {
147        value.build_inner().unwrap_or_else(|_| {
148            panic!(
149                "Failed to convert {0} to {1}",
150                "HnswConfigDiffBuilder", "HnswConfigDiff"
151            )
152        })
153    }
154}
155
156impl HnswConfigDiffBuilder {
157    /// Builds the desired type. Can often be omitted.
158    pub fn build(self) -> HnswConfigDiff {
159        self.build_inner().unwrap_or_else(|_| {
160            panic!(
161                "Failed to build {0} into {1}",
162                "HnswConfigDiffBuilder", "HnswConfigDiff"
163            )
164        })
165    }
166}
167
168impl Default for HnswConfigDiffBuilder {
169    fn default() -> Self {
170        Self::create_empty()
171    }
172}