Skip to main content

qdrant_client/builders/
dense_vector_creation_config_builder.rs

1use crate::qdrant::*;
2
3/// Dense vector creation parameters.
4/// Only includes immutable properties that define the vector space.
5/// Storage type, index, and quantization are configured separately.
6#[must_use]
7#[derive(Clone)]
8pub struct DenseVectorCreationConfigBuilder {
9    /// Size/dimensionality of the vectors
10    pub(crate) size: Option<u64>,
11    /// Distance function used for comparing vectors
12    pub(crate) distance: Option<i32>,
13    /// Configuration for multi-vector search (e.g., ColBERT)
14    pub(crate) multivector_config: Option<Option<MultiVectorConfig>>,
15    /// Data type of the vectors (Float32, Float16, Uint8)
16    pub(crate) datatype: Option<Option<i32>>,
17}
18
19impl DenseVectorCreationConfigBuilder {
20    /// Size/dimensionality of the vectors
21    pub fn size(self, value: u64) -> Self {
22        let mut new = self;
23        new.size = Option::Some(value);
24        new
25    }
26
27    /// Distance function used for comparing vectors
28    pub fn distance<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
29        let mut new = self;
30        new.distance = Option::Some(value.into());
31        new
32    }
33
34    /// Configuration for multi-vector search (e.g., ColBERT)
35    pub fn multivector_config<VALUE: core::convert::Into<MultiVectorConfig>>(
36        self,
37        value: VALUE,
38    ) -> Self {
39        let mut new = self;
40        new.multivector_config = Option::Some(Option::Some(value.into()));
41        new
42    }
43
44    /// Data type of the vectors (Float32, Float16, Uint8)
45    pub fn datatype<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
46        let mut new = self;
47        new.datatype = Option::Some(Option::Some(value.into()));
48        new
49    }
50
51    fn build_inner(
52        self,
53    ) -> Result<DenseVectorCreationConfig, DenseVectorCreationConfigBuilderError> {
54        Ok(DenseVectorCreationConfig {
55            size: match self.size {
56                Some(value) => value,
57                None => {
58                    return Result::Err(core::convert::Into::into(
59                        ::derive_builder::UninitializedFieldError::from("size"),
60                    ));
61                }
62            },
63            distance: match self.distance {
64                Some(value) => value,
65                None => {
66                    return Result::Err(core::convert::Into::into(
67                        ::derive_builder::UninitializedFieldError::from("distance"),
68                    ));
69                }
70            },
71            multivector_config: self.multivector_config.unwrap_or_default(),
72            datatype: self.datatype.unwrap_or_default(),
73        })
74    }
75    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
76    fn create_empty() -> Self {
77        Self {
78            size: core::default::Default::default(),
79            distance: core::default::Default::default(),
80            multivector_config: core::default::Default::default(),
81            datatype: core::default::Default::default(),
82        }
83    }
84}
85
86impl From<DenseVectorCreationConfigBuilder> for DenseVectorCreationConfig {
87    fn from(value: DenseVectorCreationConfigBuilder) -> Self {
88        value.build_inner().unwrap_or_else(|_| {
89            panic!(
90                "Failed to convert {0} to {1}",
91                "DenseVectorCreationConfigBuilder", "DenseVectorCreationConfig"
92            )
93        })
94    }
95}
96
97impl DenseVectorCreationConfigBuilder {
98    /// Builds the desired type. Can often be omitted.
99    pub fn build(self) -> DenseVectorCreationConfig {
100        self.build_inner().unwrap_or_else(|_| {
101            panic!(
102                "Failed to build {0} into {1}",
103                "DenseVectorCreationConfigBuilder", "DenseVectorCreationConfig"
104            )
105        })
106    }
107}
108
109impl DenseVectorCreationConfigBuilder {
110    pub(crate) fn empty() -> Self {
111        Self::create_empty()
112    }
113}
114
115/// Error type for DenseVectorCreationConfigBuilder
116#[non_exhaustive]
117#[derive(Debug)]
118pub enum DenseVectorCreationConfigBuilderError {
119    /// Uninitialized field
120    UninitializedField(&'static str),
121    /// Custom validation error
122    ValidationError(String),
123}
124
125// Implementing the Display trait for better error messages
126impl std::fmt::Display for DenseVectorCreationConfigBuilderError {
127    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
128        match self {
129            Self::UninitializedField(field) => {
130                write!(f, "`{field}` must be initialized")
131            }
132            Self::ValidationError(error) => write!(f, "{error}"),
133        }
134    }
135}
136
137// Implementing the Error trait
138impl std::error::Error for DenseVectorCreationConfigBuilderError {}
139
140// Implementing From trait for conversion from UninitializedFieldError
141impl From<derive_builder::UninitializedFieldError> for DenseVectorCreationConfigBuilderError {
142    fn from(error: derive_builder::UninitializedFieldError) -> Self {
143        Self::UninitializedField(error.field_name())
144    }
145}
146
147// Implementing From trait for conversion from String
148impl From<String> for DenseVectorCreationConfigBuilderError {
149    fn from(error: String) -> Self {
150        Self::ValidationError(error)
151    }
152}