@@ -12,44 +12,75 @@ pub struct CollectionSchemaBuilder {
12
12
name : String ,
13
13
fields : Vec < Field > ,
14
14
default_sorting_field : Option < String > ,
15
+ token_separators : Option < Vec < String > > ,
16
+ enable_nested_fields : Option < bool > ,
17
+ symbols_to_index : Option < Vec < String > > ,
15
18
}
16
19
17
20
impl CollectionSchemaBuilder {
18
21
/// Create a builder for [CollectionSchema]
19
- pub fn new ( name : impl Into < String > ) -> Self {
22
+ #[ inline]
23
+ pub fn new ( name : impl Into < String > , fields : Vec < Field > ) -> Self {
20
24
Self {
21
25
name : name. into ( ) ,
26
+ fields,
22
27
..Default :: default ( )
23
28
}
24
29
}
30
+
25
31
/// Insert field
32
+ #[ inline]
26
33
pub fn field ( mut self , field : Field ) -> Self {
27
34
self . fields . push ( field) ;
28
35
self
29
36
}
30
37
31
38
/// Set fields
39
+ #[ inline]
32
40
pub fn fields ( mut self , fields : & [ Field ] ) -> Self {
33
41
self . fields . extend_from_slice ( fields) ;
34
42
self
35
43
}
36
44
37
- /// Set default_sorting_field
38
- pub fn default_sorting_field ( mut self , default_sorting_field : String ) -> Self {
39
- self . default_sorting_field = Some ( default_sorting_field) ;
45
+ /// Set default sorting field
46
+ #[ inline]
47
+ pub fn default_sorting_field ( mut self , default_sorting_field : impl Into < String > ) -> Self {
48
+ self . default_sorting_field = Some ( default_sorting_field. into ( ) ) ;
49
+ self
50
+ }
51
+
52
+ /// Set token separators
53
+ #[ inline]
54
+ pub fn token_separators ( mut self , token_separators : Vec < String > ) -> Self {
55
+ self . token_separators = Some ( token_separators) ;
56
+ self
57
+ }
58
+
59
+ /// Enable nested fields
60
+ #[ inline]
61
+ pub fn enable_nested_fields ( mut self , enable_nested_fields : Option < bool > ) -> Self {
62
+ self . enable_nested_fields = enable_nested_fields;
63
+ self
64
+ }
65
+
66
+ /// Set symbols to index
67
+ #[ inline]
68
+ pub fn symbols_to_index ( mut self , symbols_to_index : Vec < String > ) -> Self {
69
+ self . symbols_to_index = Some ( symbols_to_index) ;
40
70
self
41
71
}
42
72
43
73
/// Create a `CollectionSchema` with the current values of the builder,
44
74
/// It can fail if any of the required fields is not not defined.
75
+ #[ inline]
45
76
pub fn build ( self ) -> CollectionSchema {
46
77
CollectionSchema {
47
78
name : self . name ,
48
79
fields : self . fields ,
49
80
default_sorting_field : self . default_sorting_field ,
50
- token_separators : None ,
51
- enable_nested_fields : None ,
52
- symbols_to_index : None ,
81
+ token_separators : self . token_separators ,
82
+ enable_nested_fields : self . enable_nested_fields ,
83
+ symbols_to_index : self . symbols_to_index ,
53
84
}
54
85
}
55
86
}
@@ -63,17 +94,17 @@ mod test {
63
94
#[ test]
64
95
fn collection_schema_serializes_as_expected ( ) {
65
96
let fields = [
66
- ( "company_name" . to_owned ( ) , "string" . to_owned ( ) , None ) ,
67
- ( "num_employees" . to_owned ( ) , "int32" . to_owned ( ) , None ) ,
68
- ( "country" . to_owned ( ) , "string" . to_owned ( ) , Some ( true ) ) ,
97
+ ( "company_name" , "string" . to_owned ( ) , None ) ,
98
+ ( "num_employees" , "int32" . to_owned ( ) , None ) ,
99
+ ( "country" , "string" . to_owned ( ) , Some ( true ) ) ,
69
100
]
70
101
. map ( |( name, typesense_type, facet) | {
71
102
FieldBuilder :: new ( name, typesense_type) . facet ( facet) . build ( )
72
- } ) ;
103
+ } )
104
+ . to_vec ( ) ;
73
105
74
- let collection = CollectionSchemaBuilder :: new ( "companies" . to_owned ( ) )
106
+ let collection = CollectionSchemaBuilder :: new ( "companies" , fields )
75
107
. default_sorting_field ( "num_employees" . to_owned ( ) )
76
- . fields ( & fields)
77
108
. build ( ) ;
78
109
79
110
let expected = json ! (
0 commit comments