@@ -26,12 +26,50 @@ impl From<SortOrder> for Bson {
26
26
}
27
27
}
28
28
29
+ #[ derive( Clone , Debug ) ]
30
+ enum IndexKey {
31
+ SortIndex ( SortIndexKey ) ,
32
+ TextIndex ( TextIndexKey ) ,
33
+ }
34
+
35
+ impl IndexKey {
36
+ fn get_key_name ( & self ) -> String {
37
+ match self {
38
+ IndexKey :: SortIndex ( s) => match s. direction {
39
+ SortOrder :: Ascending => format ! ( "{}_1" , s. name) ,
40
+ SortOrder :: Descending => format ! ( "{}_-1" , s. name) ,
41
+ } ,
42
+
43
+ IndexKey :: TextIndex ( t) => format ! ( "{}_text" , t. name) ,
44
+ }
45
+ }
46
+
47
+ fn get_name ( & self ) -> String {
48
+ match self {
49
+ IndexKey :: SortIndex ( s) => s. name . to_string ( ) ,
50
+ IndexKey :: TextIndex ( t) => t. name . to_string ( ) ,
51
+ }
52
+ }
53
+
54
+ fn get_value ( & self ) -> Bson {
55
+ match self {
56
+ IndexKey :: SortIndex ( s) => s. direction . into ( ) ,
57
+ IndexKey :: TextIndex ( _) => "text" . into ( ) ,
58
+ }
59
+ }
60
+ }
61
+
29
62
#[ derive( Debug , Clone ) ]
30
- struct IndexKey {
63
+ struct SortIndexKey {
31
64
name : Cow < ' static , str > ,
32
65
direction : SortOrder ,
33
66
}
34
67
68
+ #[ derive( Debug , Clone ) ]
69
+ struct TextIndexKey {
70
+ name : Cow < ' static , str > ,
71
+ }
72
+
35
73
/// Specify field to be used for indexing and options.
36
74
///
37
75
/// [Mongo manual](https://docs.mongodb.com/manual/indexes/)
@@ -78,6 +116,15 @@ impl Index {
78
116
index
79
117
}
80
118
119
+ /// Make a new index for the given key with the text parameter.
120
+ ///
121
+ /// [Mongo manual](https://docs.mongodb.com/manual/core/index-single/)
122
+ pub fn new_with_text ( key : impl Into < Cow < ' static , str > > ) -> Self {
123
+ let mut index = Self :: default ( ) ;
124
+ index. add_key_with_text ( key) ;
125
+ index
126
+ }
127
+
81
128
/// Make this index compound adding the given key with ascending direction.
82
129
///
83
130
/// [Mongo manual](https://docs.mongodb.com/manual/core/index-compound/).
@@ -99,10 +146,18 @@ impl Index {
99
146
key : impl Into < Cow < ' static , str > > ,
100
147
direction : SortOrder ,
101
148
) {
102
- self . keys . push ( IndexKey {
149
+ self . keys . push ( IndexKey :: SortIndex ( SortIndexKey {
103
150
name : key. into ( ) ,
104
151
direction,
105
- } ) ;
152
+ } ) ) ;
153
+ }
154
+
155
+ /// Make this index compound adding the given key with text.
156
+ ///
157
+ /// [Mongo manual](https://docs.mongodb.com/manual/core/index-compound/).
158
+ pub fn add_key_with_text ( & mut self , key : impl Into < Cow < ' static , str > > ) {
159
+ self . keys
160
+ . push ( IndexKey :: TextIndex ( TextIndexKey { name : key. into ( ) } ) ) ;
106
161
}
107
162
108
163
/// Builder style method for `add_key_with_direction`.
@@ -140,13 +195,8 @@ impl Index {
140
195
let mut names = Vec :: with_capacity ( self . keys . len ( ) ) ;
141
196
let mut keys_doc = Document :: new ( ) ;
142
197
for key in self . keys {
143
- let key_name = match key. direction {
144
- SortOrder :: Ascending => format ! ( "{}_1" , key. name) ,
145
- SortOrder :: Descending => format ! ( "{}_-1" , key. name) ,
146
- } ;
147
- names. push ( key_name) ;
148
-
149
- keys_doc. insert ( key. name , key. direction ) ;
198
+ names. push ( key. get_key_name ( ) ) ;
199
+ keys_doc. insert ( key. get_name ( ) , key. get_value ( ) ) ;
150
200
}
151
201
152
202
let mut index_doc = doc ! { "key" : keys_doc } ;
@@ -232,6 +282,8 @@ pub enum IndexOption {
232
282
StorageEngine ( Document ) ,
233
283
/// Specifies the collation
234
284
Collation ( Document ) ,
285
+ /// Specifies the weights for text indexes
286
+ Weights ( Vec < ( String , i32 ) > ) ,
235
287
/// Specify a custom index option. This is present to provide forwards compatibility.
236
288
Custom { name : String , value : Bson } ,
237
289
}
@@ -247,6 +299,7 @@ impl IndexOption {
247
299
IndexOption :: ExpireAfterSeconds ( ..) => "expireAfterSeconds" ,
248
300
IndexOption :: StorageEngine ( ..) => "storageEngine" ,
249
301
IndexOption :: Collation ( ..) => "collation" ,
302
+ IndexOption :: Weights ( ..) => "weights" ,
250
303
IndexOption :: Custom { name, .. } => name. as_str ( ) ,
251
304
}
252
305
}
@@ -261,6 +314,13 @@ impl IndexOption {
261
314
IndexOption :: PartialFilterExpression ( doc)
262
315
| IndexOption :: StorageEngine ( doc)
263
316
| IndexOption :: Collation ( doc) => Bson :: Document ( doc) ,
317
+ IndexOption :: Weights ( w) => {
318
+ let mut doc = Document :: new ( ) ;
319
+ w. into_iter ( ) . for_each ( |( k, v) | {
320
+ doc. insert ( k, Bson :: from ( v) ) ;
321
+ } ) ;
322
+ Bson :: Document ( doc)
323
+ }
264
324
IndexOption :: Custom { value, .. } => value,
265
325
}
266
326
}
0 commit comments