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