azure_data_cosmos 0.32.0

Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use azure_core::fmt::SafeDebug;
use serde::{Deserialize, Serialize};

/// Represents the indexing policy for a container.
///
/// For more information see <https://learn.microsoft.com/azure/cosmos-db/index-policy>
#[derive(Clone, Default, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct IndexingPolicy {
    /// Indicates that the indexing policy is automatic.
    #[serde(default)]
    pub automatic: bool,

    /// The indexing mode in use.
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub indexing_mode: Option<IndexingMode>,

    /// The paths to be indexed.
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub included_paths: Vec<PropertyPath>,

    /// The paths to be excluded.
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub excluded_paths: Vec<PropertyPath>,

    /// A list of spatial indexes in the container.
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub spatial_indexes: Vec<SpatialIndex>,

    /// A list of composite indexes in the container
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub composite_indexes: Vec<CompositeIndex>,

    /// A list of vector indexes in the container
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub vector_indexes: Vec<VectorIndex>,
}

impl IndexingPolicy {
    pub fn with_indexing_mode(mut self, indexing_mode: IndexingMode) -> Self {
        self.indexing_mode = Some(indexing_mode);
        self
    }

    pub fn with_included_path(mut self, included_path: impl Into<PropertyPath>) -> Self {
        self.included_paths.push(included_path.into());
        self
    }

    pub fn with_excluded_path(mut self, excluded_path: impl Into<PropertyPath>) -> Self {
        self.excluded_paths.push(excluded_path.into());
        self
    }

    pub fn with_spatial_index(mut self, spatial_index: SpatialIndex) -> Self {
        self.spatial_indexes.push(spatial_index);
        self
    }

    pub fn with_composite_index(mut self, composite_index: CompositeIndex) -> Self {
        self.composite_indexes.push(composite_index);
        self
    }

    pub fn with_vector_index(mut self, vector_index: VectorIndex) -> Self {
        self.vector_indexes.push(vector_index);
        self
    }
}

/// Defines the indexing modes supported by Azure Cosmos DB.
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
pub enum IndexingMode {
    Consistent,
    None,
}

/// Represents a JSON path.
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PropertyPath {
    // The path to the property referenced in this index.
    pub path: String,
}

impl<T: Into<String>> From<T> for PropertyPath {
    fn from(value: T) -> Self {
        PropertyPath { path: value.into() }
    }
}

/// Represents a spatial index
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SpatialIndex {
    /// The path to the property referenced in this index.
    pub path: String,

    /// The spatial types used in this index
    pub types: Vec<SpatialType>,
}

/// Defines the types of spatial data that can be indexed.
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "PascalCase")]
pub enum SpatialType {
    Point,
    Polygon,
    LineString,
    MultiPolygon,
}

/// Represents a composite index
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(transparent)]
#[non_exhaustive]
pub struct CompositeIndex {
    /// The properties in this composite index
    pub properties: Vec<CompositeIndexProperty>,
}

/// Describes a single property in a composite index.
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CompositeIndexProperty {
    /// The path to the property referenced in this index.
    pub path: String,

    /// The order of the composite index.
    ///
    /// For example, if you want to run the query "SELECT * FROM c ORDER BY c.age asc, c.height desc",
    /// then you'd specify the order for "/asc" to be *ascending* and the order for "/height" to be *descending*.
    pub order: CompositeIndexOrder,
}

/// Ordering values available for composite indexes.
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
pub enum CompositeIndexOrder {
    Ascending,
    Descending,
}

/// Represents a vector index
///
/// For more information, see <https://learn.microsoft.com/en-us/azure/cosmos-db/index-policy#vector-indexes>
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct VectorIndex {
    /// The path to the property referenced in this index.
    pub path: String,

    /// The type of the vector index.
    #[serde(rename = "type")] // "type" is a reserved word in Rust.
    pub index_type: VectorIndexType,
}

/// Types of vector indexes supported by Cosmos DB
#[derive(Clone, SafeDebug, Deserialize, Serialize, PartialEq, Eq)]
#[safe(true)]
#[serde(rename_all = "camelCase")]
pub enum VectorIndexType {
    /// Represents the `flat` vector index type.
    Flat,

    /// Represents the `quantizedFlat` vector index type.
    QuantizedFlat,

    /// Represents the `diskANN` vector index type.
    DiskANN,
}

#[cfg(test)]
mod tests {
    use crate::models::{
        CompositeIndex, CompositeIndexOrder, CompositeIndexProperty, IndexingMode, IndexingPolicy,
        PropertyPath, SpatialIndex, SpatialType, VectorIndex, VectorIndexType,
    };

    #[test]
    pub fn deserialize_indexing_policy() {
        // A fairly complete deserialization test that covers most of the indexing policies described in our docs.
        let policy = r#"
            {
                "indexingMode": "consistent",
                "includedPaths": [
                    {
                        "path": "/*"
                    }
                ],
                "excludedPaths": [
                    {
                        "path": "/path/to/single/excluded/property/?"
                    },
                    {
                        "path": "/path/to/root/of/multiple/excluded/properties/*"
                    }
                ],
                "spatialIndexes": [
                    {
                        "path": "/path/to/geojson/property/?",
                        "types": [
                            "Point",
                            "Polygon",
                            "MultiPolygon",
                            "LineString"
                        ]
                    }
                ],
                "vectorIndexes": [
                    {
                        "path": "/vector1",
                        "type": "quantizedFlat"
                    },
                    {
                        "path": "/vector2",
                        "type": "diskANN"
                    }
                ],
                "compositeIndexes":[
                    [
                        {
                            "path":"/name",
                            "order":"ascending"
                        },
                        {
                            "path":"/age",
                            "order":"descending"
                        }
                    ],
                    [
                        {
                            "path":"/name2",
                            "order":"descending"
                        },
                        {
                            "path":"/age2",
                            "order":"ascending"
                        }
                    ]
                ],
                "extraValueNotCurrentlyPresentInModel": {
                    "this": "should not fail"
                }
            }
        "#;

        let policy: IndexingPolicy = serde_json::from_str(policy).unwrap();

        assert_eq!(
            IndexingPolicy {
                automatic: false,
                indexing_mode: Some(IndexingMode::Consistent),
                included_paths: vec![PropertyPath {
                    path: "/*".to_string(),
                }],
                excluded_paths: vec![
                    PropertyPath {
                        path: "/path/to/single/excluded/property/?".to_string()
                    },
                    PropertyPath {
                        path: "/path/to/root/of/multiple/excluded/properties/*".to_string()
                    },
                ],
                spatial_indexes: vec![SpatialIndex {
                    path: "/path/to/geojson/property/?".to_string(),
                    types: vec![
                        SpatialType::Point,
                        SpatialType::Polygon,
                        SpatialType::MultiPolygon,
                        SpatialType::LineString,
                    ]
                }],
                composite_indexes: vec![
                    CompositeIndex {
                        properties: vec![
                            CompositeIndexProperty {
                                path: "/name".to_string(),
                                order: CompositeIndexOrder::Ascending,
                            },
                            CompositeIndexProperty {
                                path: "/age".to_string(),
                                order: CompositeIndexOrder::Descending,
                            },
                        ]
                    },
                    CompositeIndex {
                        properties: vec![
                            CompositeIndexProperty {
                                path: "/name2".to_string(),
                                order: CompositeIndexOrder::Descending,
                            },
                            CompositeIndexProperty {
                                path: "/age2".to_string(),
                                order: CompositeIndexOrder::Ascending,
                            },
                        ]
                    },
                ],
                vector_indexes: vec![
                    VectorIndex {
                        path: "/vector1".to_string(),
                        index_type: VectorIndexType::QuantizedFlat,
                    },
                    VectorIndex {
                        path: "/vector2".to_string(),
                        index_type: VectorIndexType::DiskANN,
                    }
                ]
            },
            policy
        );
    }

    #[test]
    pub fn serialize_indexing_policy() {
        let policy = IndexingPolicy {
            automatic: true,
            indexing_mode: None,
            included_paths: vec![PropertyPath {
                path: "/*".to_string(),
            }],
            excluded_paths: vec![
                PropertyPath {
                    path: "/path/to/single/excluded/property/?".to_string(),
                },
                PropertyPath {
                    path: "/path/to/root/of/multiple/excluded/properties/*".to_string(),
                },
            ],
            spatial_indexes: vec![
                SpatialIndex {
                    path: "/path/to/geojson/property/?".to_string(),
                    types: vec![
                        SpatialType::Point,
                        SpatialType::Polygon,
                        SpatialType::MultiPolygon,
                        SpatialType::LineString,
                    ],
                },
                SpatialIndex {
                    path: "/path/to/geojson/property2/?".to_string(),
                    types: vec![],
                },
            ],
            composite_indexes: vec![
                CompositeIndex {
                    properties: vec![
                        CompositeIndexProperty {
                            path: "/name".to_string(),
                            order: CompositeIndexOrder::Ascending,
                        },
                        CompositeIndexProperty {
                            path: "/age".to_string(),
                            order: CompositeIndexOrder::Descending,
                        },
                    ],
                },
                CompositeIndex { properties: vec![] },
            ],
            vector_indexes: vec![
                VectorIndex {
                    path: "/vector1".to_string(),
                    index_type: VectorIndexType::QuantizedFlat,
                },
                VectorIndex {
                    path: "/vector2".to_string(),
                    index_type: VectorIndexType::DiskANN,
                },
            ],
        };
        let json = serde_json::to_string(&policy).unwrap();

        assert_eq!(
            "{\"automatic\":true,\"includedPaths\":[{\"path\":\"/*\"}],\"excludedPaths\":[{\"path\":\"/path/to/single/excluded/property/?\"},{\"path\":\"/path/to/root/of/multiple/excluded/properties/*\"}],\"spatialIndexes\":[{\"path\":\"/path/to/geojson/property/?\",\"types\":[\"Point\",\"Polygon\",\"MultiPolygon\",\"LineString\"]},{\"path\":\"/path/to/geojson/property2/?\",\"types\":[]}],\"compositeIndexes\":[[{\"path\":\"/name\",\"order\":\"ascending\"},{\"path\":\"/age\",\"order\":\"descending\"}],[]],\"vectorIndexes\":[{\"path\":\"/vector1\",\"type\":\"quantizedFlat\"},{\"path\":\"/vector2\",\"type\":\"diskANN\"}]}",
            json
        );
    }
}