Skip to main content

qdrant_client/builders/
integer_index_params_builder.rs

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