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(i as u32)),
172 }
173 }
174}
175
176/// Specify the version for a `text` index. For more information, see [Versions](https://www.mongodb.com/docs/manual/core/index-text/#versions).
177#[derive(Clone, Debug)]
178pub enum TextIndexVersion {
179 /// Version 1.
180 V1,
181
182 /// Version 2.
183 V2,
184
185 /// Version 3.
186 V3,
187
188 /// Specify a custom text index version. This is present to provide forwards compatibility with
189 /// any future text index versions which may be added to new versions of MongoDB.
190 Custom(u32),
191}
192
193impl Serialize for TextIndexVersion {
194 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
195 where
196 S: Serializer,
197 {
198 match self {
199 TextIndexVersion::V1 => serializer.serialize_i32(1),
200 TextIndexVersion::V2 => serializer.serialize_i32(2),
201 TextIndexVersion::V3 => serializer.serialize_i32(3),
202 TextIndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
203 }
204 }
205}
206
207impl<'de> Deserialize<'de> for TextIndexVersion {
208 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
209 where
210 D: Deserializer<'de>,
211 {
212 match i32::deserialize(deserializer)? {
213 1 => Ok(TextIndexVersion::V1),
214 2 => Ok(TextIndexVersion::V2),
215 3 => Ok(TextIndexVersion::V3),
216 i => Ok(TextIndexVersion::Custom(i as u32)),
217 }
218 }
219}
220
221/// Specify the version for a `2dsphere` index. For more information, see [Versions](https://www.mongodb.com/docs/manual/core/2dsphere/#versions).
222#[derive(Clone, Debug)]
223pub enum Sphere2DIndexVersion {
224 /// Version 2.
225 V2,
226
227 /// Version 3.
228 V3,
229
230 /// Specify a custom sphere 2D index version. This is present to provide forwards compatibility
231 /// with any future sphere 2D index verions which may be added to new versions of MongoDB.
232 Custom(u32),
233}
234
235impl Serialize for Sphere2DIndexVersion {
236 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
237 where
238 S: Serializer,
239 {
240 match self {
241 Sphere2DIndexVersion::V2 => serializer.serialize_i32(2),
242 Sphere2DIndexVersion::V3 => serializer.serialize_i32(3),
243 Sphere2DIndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
244 }
245 }
246}
247
248impl<'de> Deserialize<'de> for Sphere2DIndexVersion {
249 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
250 where
251 D: Deserializer<'de>,
252 {
253 match i32::deserialize(deserializer)? {
254 2 => Ok(Sphere2DIndexVersion::V2),
255 3 => Ok(Sphere2DIndexVersion::V3),
256 i => Ok(Sphere2DIndexVersion::Custom(i as u32)),
257 }
258 }
259}