1+ use std:: ffi:: c_void;
12use std:: ffi:: { CStr , CString } ;
23use std:: ptr;
34
@@ -6,15 +7,29 @@ use num_traits::ToPrimitive;
67use crate :: raw:: { self , RSFieldID , RSResultsIterator , GC_POLICY_FORK , GC_POLICY_NONE } ;
78use crate :: { Document , FieldType } ;
89use redis_module:: RedisError ;
9- use std:: os:: raw:: c_char;
10+ use std:: os:: raw:: { c_char, c_int} ;
11+
12+ pub struct Field < ' a > {
13+ index : & ' a Index ,
14+ field_id : RSFieldID ,
15+ }
1016
1117pub struct Index {
1218 inner : * mut raw:: RSIndex ,
1319}
1420
15- pub struct Field < ' a > {
16- index : & ' a Index ,
17- field_id : RSFieldID ,
21+ pub struct TagOptions {
22+ tag_separator : Option < char > ,
23+ tag_case_sensitive : bool ,
24+ }
25+
26+ impl Default for TagOptions {
27+ fn default ( ) -> Self {
28+ Self {
29+ tag_separator : None ,
30+ tag_case_sensitive : false ,
31+ }
32+ }
1833}
1934
2035impl Index {
@@ -24,7 +39,7 @@ impl Index {
2439 Self { inner : index }
2540 }
2641
27- pub fn create_with_options ( name : & str , options : & IndexOptions ) -> Self {
42+ pub fn create_with_options ( name : & str , options : IndexOptions ) -> Self {
2843 let index_options =
2944 unsafe { raw:: RediSearch_CreateIndexOptions ( ) . as_mut ( ) } . expect ( "null IndexOptions" ) ;
3045
@@ -38,7 +53,7 @@ impl Index {
3853 Self { inner : index }
3954 }
4055
41- pub fn create_field ( & self , name : & str ) -> Field {
56+ pub fn create_field ( & self , name : & str , weight : f64 , tag_options : TagOptions ) -> Field {
4257 let name = CString :: new ( name) . unwrap ( ) ;
4358
4459 // We want to let the document decide the type, so we support all types.
@@ -47,6 +62,19 @@ impl Index {
4762
4863 let field_id =
4964 unsafe { raw:: RediSearch_CreateField ( self . inner , name. as_ptr ( ) , ftype. bits , fopt) } ;
65+ unsafe {
66+ raw:: RediSearch_TextFieldSetWeight ( self . inner , field_id, weight) ;
67+ if let Some ( separator) = tag_options. tag_separator {
68+ raw:: RediSearch_TagFieldSetSeparator ( self . inner , field_id, separator as c_char ) ;
69+ }
70+ if tag_options. tag_case_sensitive {
71+ raw:: RediSearch_TagFieldSetCaseSensitive (
72+ self . inner ,
73+ field_id,
74+ tag_options. tag_case_sensitive as c_int ,
75+ ) ;
76+ }
77+ }
5078
5179 Field {
5280 index : self ,
@@ -71,6 +99,22 @@ impl Index {
7199 }
72100 }
73101
102+ pub fn del_document ( & self , key : & str ) -> Result < ( ) , RedisError > {
103+ let status = unsafe {
104+ raw:: RediSearch_DeleteDocument (
105+ self . inner ,
106+ CString :: new ( key) . unwrap ( ) . as_ptr ( ) as * const c_void ,
107+ key. len ( ) ,
108+ )
109+ } ;
110+
111+ if status == redis_module:: raw:: REDISMODULE_ERR as i32 {
112+ Err ( RedisError :: Str ( "error deleting document" ) )
113+ } else {
114+ Ok ( ( ) )
115+ }
116+ }
117+
74118 pub fn search ( & self , query_string : & str ) -> Result < ResultsIterator , RedisError > {
75119 let c_query = CString :: new ( query_string) . unwrap ( ) ;
76120 let mut err_ptr = ptr:: null_mut ( ) ;
0 commit comments