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