mongodb/index/
options.rs

1use std::time::Duration;
2
3use crate::{bson::Document, collation::Collation, serde_util};
4
5use serde::{Deserialize, Deserializer, Serialize, Serializer};
6use typed_builder::TypedBuilder;
7
8/// These are the valid options for specifying an [`IndexModel`](../struct.IndexModel.html).
9/// For more information on these properties, see the [documentation](https://www.mongodb.com/docs/manual/reference/command/createIndexes/#definition).
10#[serde_with::skip_serializing_none]
11#[derive(Clone, Debug, Default, Deserialize, Serialize, TypedBuilder)]
12#[builder(field_defaults(default, setter(into)))]
13#[serde(rename_all = "camelCase")]
14#[non_exhaustive]
15pub struct IndexOptions {
16    /// Tells the server to build the index in the background and not block other tasks. Starting
17    /// in MongoDB 4.2, this option is deprecated and ignored by the server.
18    pub background: Option<bool>,
19
20    /// Specifies a TTL to control how long MongoDB retains
21    /// documents in this collection.
22    ///
23    /// See the [documentation](https://www.mongodb.com/docs/manual/core/index-ttl/)
24    /// for more information on how to use this option.
25    #[serde(
26        rename = "expireAfterSeconds",
27        default,
28        with = "serde_util::duration_option_as_int_seconds"
29    )]
30    pub expire_after: Option<Duration>,
31
32    /// Specifies a name outside the default generated name.
33    pub name: Option<String>,
34
35    /// If true, the index only references documents with the specified field. The
36    /// default value is false.
37    ///
38    /// See the [documentation](https://www.mongodb.com/docs/manual/core/index-sparse/)
39    /// for more information on how to use this option.
40    pub sparse: Option<bool>,
41
42    /// Allows users to configure the storage engine on a per-index basis when creating
43    /// an index.
44    pub storage_engine: Option<Document>,
45
46    /// Forces the index to be unique so the collection will not accept documents where the index
47    /// key value matches an existing value in the index. The default value is false.
48    pub unique: Option<bool>,
49
50    /// Specify the version number of the index.
51    /// Starting in MongoDB 3.2, Version 0 indexes are not allowed.
52    #[serde(rename = "v")]
53    pub version: Option<IndexVersion>,
54
55    /// For text indexes, the language that determines the list of stop words and the
56    /// rules for the stemmer and tokenizer.
57    #[serde(rename = "default_language")]
58    pub default_language: Option<String>,
59
60    /// For `text` indexes, the name of the field, in the collection’s documents, that
61    /// contains the override language for the document.
62    #[serde(rename = "language_override")]
63    pub language_override: Option<String>,
64
65    /// The `text` index version number. Users can use this option to override the default
66    /// version number.
67    pub text_index_version: Option<TextIndexVersion>,
68
69    /// For `text` indexes, a document that contains field and weight pairs.
70    pub weights: Option<Document>,
71
72    /// The `2dsphere` index version number.
73    /// As of MongoDB 3.2, version 3 is the default. Version 2 is the default in MongoDB 2.6 and
74    /// 3.0 series.
75    #[serde(rename = "2dsphereIndexVersion")]
76    pub sphere_2d_index_version: Option<Sphere2DIndexVersion>,
77
78    /// For `2dsphere` indexes, the number of precision of the stored geohash value of the
79    /// location data. The bits value ranges from 1 to 32 inclusive.
80    #[serde(serialize_with = "serde_util::serialize_u32_option_as_i32")]
81    pub bits: Option<u32>,
82
83    /// For `2dsphere` indexes, the upper inclusive boundary for the longitude and latitude
84    /// values.
85    pub max: Option<f64>,
86
87    /// For `2dsphere` indexes, the lower inclusive boundary for the longitude and latitude
88    /// values.
89    pub min: Option<f64>,
90
91    /// For `geoHaystack` indexes, specify the number of units within which to group the location
92    /// values.
93    #[serde(serialize_with = "serde_util::serialize_u32_option_as_i32")]
94    pub bucket_size: Option<u32>,
95
96    /// If specified, the index only references documents that match the filter
97    /// expression. See Partial Indexes for more information.
98    pub partial_filter_expression: Option<Document>,
99
100    /// Specifies the collation for the index.
101    pub collation: Option<Collation>,
102
103    /// Allows users to include or exclude specific field paths from a wildcard index.
104    pub wildcard_projection: Option<Document>,
105
106    /// A flag that determines whether the index is hidden from the query planner. A
107    /// hidden index is not evaluated as part of the query plan selection.
108    pub hidden: Option<bool>,
109
110    #[builder(default, setter(skip))]
111    clustered: Option<bool>,
112}
113
114impl IndexOptions {
115    /// Optionally specifies that this index is clustered.  This is not a valid option to provide to
116    /// 'create_indexes', but can appear in the options returned for an index via 'list_indexes'.
117    /// To create a clustered index, create a new collection using the 'clustered_index' option.
118    ///
119    /// This options is only supported by servers >= 6.0.
120    pub fn clustered(&self) -> Option<bool> {
121        self.clustered
122    }
123}
124
125/// The version of the index. Version 0 Indexes are disallowed as of MongoDB 3.2.
126///
127/// See [Version 0 Indexes](https://www.mongodb.com/docs/manual/release-notes/3.2-compatibility/#std-label-3.2-version-0-indexes) for more information.
128#[derive(Clone, Debug)]
129#[non_exhaustive]
130pub enum IndexVersion {
131    #[deprecated]
132    /// Version 0.
133    V0,
134
135    /// Version 1.
136    V1,
137
138    /// Version 2.
139    V2,
140
141    //// Specify a custom index version. This is present to provide forwards compatibility with
142    /// any future index versions which may be added to new versions of MongoDB.
143    Custom(u32),
144}
145
146impl Serialize for IndexVersion {
147    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
148    where
149        S: Serializer,
150    {
151        match self {
152            #[allow(deprecated)]
153            IndexVersion::V0 => serializer.serialize_i32(0),
154            IndexVersion::V1 => serializer.serialize_i32(1),
155            IndexVersion::V2 => serializer.serialize_i32(2),
156            IndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
157        }
158    }
159}
160
161impl<'de> Deserialize<'de> for IndexVersion {
162    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
163    where
164        D: Deserializer<'de>,
165    {
166        match i32::deserialize(deserializer)? {
167            #[allow(deprecated)]
168            0 => Ok(IndexVersion::V0),
169            1 => Ok(IndexVersion::V1),
170            2 => Ok(IndexVersion::V2),
171            i => Ok(IndexVersion::Custom(
172                i.try_into().map_err(serde::de::Error::custom)?,
173            )),
174        }
175    }
176}
177
178/// Specify the version for a `text` index. For more information, see [Versions](https://www.mongodb.com/docs/manual/core/index-text/#versions).
179#[derive(Clone, Debug)]
180pub enum TextIndexVersion {
181    /// Version 1.
182    V1,
183
184    /// Version 2.
185    V2,
186
187    /// Version 3.
188    V3,
189
190    /// Specify a custom text index version. This is present to provide forwards compatibility with
191    /// any future text index versions which may be added to new versions of MongoDB.
192    Custom(u32),
193}
194
195impl Serialize for TextIndexVersion {
196    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
197    where
198        S: Serializer,
199    {
200        match self {
201            TextIndexVersion::V1 => serializer.serialize_i32(1),
202            TextIndexVersion::V2 => serializer.serialize_i32(2),
203            TextIndexVersion::V3 => serializer.serialize_i32(3),
204            TextIndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
205        }
206    }
207}
208
209impl<'de> Deserialize<'de> for TextIndexVersion {
210    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
211    where
212        D: Deserializer<'de>,
213    {
214        match i32::deserialize(deserializer)? {
215            1 => Ok(TextIndexVersion::V1),
216            2 => Ok(TextIndexVersion::V2),
217            3 => Ok(TextIndexVersion::V3),
218            i => Ok(TextIndexVersion::Custom(
219                i.try_into().map_err(serde::de::Error::custom)?,
220            )),
221        }
222    }
223}
224
225/// Specify the version for a `2dsphere` index. For more information, see [Versions](https://www.mongodb.com/docs/manual/core/2dsphere/#versions).
226#[derive(Clone, Debug)]
227pub enum Sphere2DIndexVersion {
228    /// Version 2.
229    V2,
230
231    /// Version 3.
232    V3,
233
234    /// Specify a custom sphere 2D index version. This is present to provide forwards compatibility
235    /// with any future sphere 2D index verions which may be added to new versions of MongoDB.
236    Custom(u32),
237}
238
239impl Serialize for Sphere2DIndexVersion {
240    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
241    where
242        S: Serializer,
243    {
244        match self {
245            Sphere2DIndexVersion::V2 => serializer.serialize_i32(2),
246            Sphere2DIndexVersion::V3 => serializer.serialize_i32(3),
247            Sphere2DIndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
248        }
249    }
250}
251
252impl<'de> Deserialize<'de> for Sphere2DIndexVersion {
253    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
254    where
255        D: Deserializer<'de>,
256    {
257        match i32::deserialize(deserializer)? {
258            2 => Ok(Sphere2DIndexVersion::V2),
259            3 => Ok(Sphere2DIndexVersion::V3),
260            i => Ok(Sphere2DIndexVersion::Custom(
261                i.try_into().map_err(serde::de::Error::custom)?,
262            )),
263        }
264    }
265}