Skip to main content

qdrant_client/builders/
integer_index_params_builder.rs

1use crate::qdrant::*;
2
3#[derive(Clone)]
4pub struct IntegerIndexParamsBuilder {
5    /// If true - support direct lookups.
6    pub(crate) lookup: Option<Option<bool>>,
7    /// If true - support ranges filters.
8    pub(crate) range: Option<Option<bool>>,
9    /// If true - use this key to organize storage of the collection data. This option assumes that this key will be used in majority of filtered requests.
10    pub(crate) is_principal: Option<Option<bool>>,
11    /// If true - store index on disk.
12    pub(crate) on_disk: Option<Option<bool>>,
13    /// If true - enable HNSW index for this field.
14    pub(crate) enable_hnsw: Option<Option<bool>>,
15}
16
17impl IntegerIndexParamsBuilder {
18    pub fn new(lookup: bool, range: bool) -> Self {
19        Self::create_empty().lookup(lookup).range(range)
20    }
21
22    /// If true - support direct lookups.
23    pub fn lookup<VALUE: core::convert::Into<bool>>(self, value: VALUE) -> Self {
24        let mut new = self;
25        new.lookup = Option::Some(Option::Some(value.into()));
26        new
27    }
28    /// If true - support ranges filters.
29    pub fn range<VALUE: core::convert::Into<bool>>(self, value: VALUE) -> Self {
30        let mut new = self;
31        new.range = Option::Some(Option::Some(value.into()));
32        new
33    }
34    /// If true - use this key to organize storage of the collection data. This option assumes that this key will be used in majority of filtered requests.
35    pub fn is_principal(self, value: bool) -> Self {
36        let mut new = self;
37        new.is_principal = Option::Some(Option::Some(value));
38        new
39    }
40    /// If true - store index on disk.
41    pub fn on_disk(self, value: bool) -> Self {
42        let mut new = self;
43        new.on_disk = Option::Some(Option::Some(value));
44        new
45    }
46    /// If true - enable HNSW index for this field.
47    pub fn enable_hnsw(self, value: bool) -> Self {
48        let mut new = self;
49        new.enable_hnsw = Option::Some(Option::Some(value));
50        new
51    }
52
53    fn build_inner(self) -> Result<IntegerIndexParams, IntegerIndexParamsBuilderError> {
54        Ok(IntegerIndexParams {
55            lookup: self.lookup.unwrap_or_default(),
56            range: self.range.unwrap_or_default(),
57            is_principal: self.is_principal.unwrap_or_default(),
58            on_disk: self.on_disk.unwrap_or_default(),
59            enable_hnsw: self.enable_hnsw.unwrap_or_default(),
60        })
61    }
62    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
63    fn create_empty() -> Self {
64        Self {
65            lookup: core::default::Default::default(),
66            range: core::default::Default::default(),
67            is_principal: core::default::Default::default(),
68            on_disk: core::default::Default::default(),
69            enable_hnsw: core::default::Default::default(),
70        }
71    }
72}
73
74impl From<IntegerIndexParamsBuilder> for IntegerIndexParams {
75    fn from(value: IntegerIndexParamsBuilder) -> Self {
76        value.build_inner().unwrap_or_else(|_| {
77            panic!(
78                "Failed to convert {0} to {1}",
79                "IntegerIndexParamsBuilder", "IntegerIndexParams"
80            )
81        })
82    }
83}
84
85impl IntegerIndexParamsBuilder {
86    /// Builds the desired type. Can often be omitted.
87    pub fn build(self) -> IntegerIndexParams {
88        self.build_inner().unwrap_or_else(|_| {
89            panic!(
90                "Failed to build {0} into {1}",
91                "IntegerIndexParamsBuilder", "IntegerIndexParams"
92            )
93        })
94    }
95}
96
97impl Default for IntegerIndexParamsBuilder {
98    fn default() -> Self {
99        Self::create_empty()
100    }
101}
102
103/// Error type for IntegerIndexParamsBuilder
104#[non_exhaustive]
105#[derive(Debug)]
106pub enum IntegerIndexParamsBuilderError {
107    /// Uninitialized field
108    UninitializedField(&'static str),
109    /// Custom validation error
110    ValidationError(String),
111}
112
113// Implementing the Display trait for better error messages
114impl std::fmt::Display for IntegerIndexParamsBuilderError {
115    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
116        match self {
117            Self::UninitializedField(field) => {
118                write!(f, "`{field}` must be initialized")
119            }
120            Self::ValidationError(error) => write!(f, "{error}"),
121        }
122    }
123}
124
125// Implementing the Error trait
126impl std::error::Error for IntegerIndexParamsBuilderError {}
127
128// Implementing From trait for conversion from UninitializedFieldError
129impl From<derive_builder::UninitializedFieldError> for IntegerIndexParamsBuilderError {
130    fn from(error: derive_builder::UninitializedFieldError) -> Self {
131        Self::UninitializedField(error.field_name())
132    }
133}
134
135// Implementing From trait for conversion from String
136impl From<String> for IntegerIndexParamsBuilderError {
137    fn from(error: String) -> Self {
138        Self::ValidationError(error)
139    }
140}