Skip to content

Commit e0e2b98

Browse files
authored
add index del_document and extended create_field (#9)
* add index del_document * update redis-module version to 0.6.0 * extend create field * pack tag options to TagOptions struct
1 parent d269e71 commit e0e2b98

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

examples/hello_redisearch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern crate redis_module;
33

44
use redis_module::{Context, NextArg, RedisError, RedisResult};
5-
use redisearch_api::{self, init, Document, FieldType, Index};
5+
use redisearch_api::{init, Document, FieldType, Index, TagOptions};
66

77
fn hello_redisearch(_: &Context, args: Vec<String>) -> RedisResult {
88
let mut args = args.into_iter().skip(1);
@@ -21,7 +21,7 @@ fn hello_redisearch(_: &Context, args: Vec<String>) -> RedisResult {
2121
let score = 1.0;
2222

2323
let index = Index::create(index_name);
24-
index.create_field(field_name);
24+
index.create_field(field_name, 1.0, TagOptions::default());
2525

2626
let doc = Document::create("doc1", score);
2727
doc.add_field(field_name, "bar", FieldType::FULLTEXT);

src/index.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::c_void;
12
use std::ffi::{CStr, CString};
23
use std::ptr;
34

@@ -6,15 +7,29 @@ use num_traits::ToPrimitive;
67
use crate::raw::{self, RSFieldID, RSResultsIterator, GC_POLICY_FORK, GC_POLICY_NONE};
78
use crate::{Document, FieldType};
89
use 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

1117
pub 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

2035
impl 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();

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod index;
1313
pub mod raw;
1414

1515
pub use document::Document;
16-
pub use index::Index;
16+
pub use index::{Index, TagOptions};
1717

1818
bitflags! {
1919
pub struct FieldType: u32 {

0 commit comments

Comments
 (0)