Skip to content

add index del_document and extended create_field #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 18, 2020
4 changes: 2 additions & 2 deletions examples/hello_redisearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extern crate redis_module;

use redis_module::{Context, NextArg, RedisError, RedisResult};
use redisearch_api::{self, init, Document, FieldType, Index};
use redisearch_api::{init, Document, FieldType, Index, TagOptions};

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

let index = Index::create(index_name);
index.create_field(field_name);
index.create_field(field_name, 1.0, TagOptions::default());

let doc = Document::create("doc1", score);
doc.add_field(field_name, "bar", FieldType::FULLTEXT);
Expand Down
56 changes: 50 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::c_void;
use std::ffi::{CStr, CString};
use std::ptr;

Expand All @@ -6,15 +7,29 @@ use num_traits::ToPrimitive;
use crate::raw::{self, RSFieldID, RSResultsIterator, GC_POLICY_FORK, GC_POLICY_NONE};
use crate::{Document, FieldType};
use redis_module::RedisError;
use std::os::raw::c_char;
use std::os::raw::{c_char, c_int};

pub struct Field<'a> {
index: &'a Index,
field_id: RSFieldID,
}

pub struct Index {
inner: *mut raw::RSIndex,
}

pub struct Field<'a> {
index: &'a Index,
field_id: RSFieldID,
pub struct TagOptions {
tag_separator: Option<char>,
tag_case_sensitive: bool,
}

impl Default for TagOptions {
fn default() -> Self {
Self {
tag_separator: None,
tag_case_sensitive: false,
}
}
}

impl Index {
Expand All @@ -24,7 +39,7 @@ impl Index {
Self { inner: index }
}

pub fn create_with_options(name: &str, options: &IndexOptions) -> Self {
pub fn create_with_options(name: &str, options: IndexOptions) -> Self {
let index_options =
unsafe { raw::RediSearch_CreateIndexOptions().as_mut() }.expect("null IndexOptions");

Expand All @@ -38,7 +53,7 @@ impl Index {
Self { inner: index }
}

pub fn create_field(&self, name: &str) -> Field {
pub fn create_field(&self, name: &str, weight: f64, tag_options: TagOptions) -> Field {
let name = CString::new(name).unwrap();

// We want to let the document decide the type, so we support all types.
Expand All @@ -47,6 +62,19 @@ impl Index {

let field_id =
unsafe { raw::RediSearch_CreateField(self.inner, name.as_ptr(), ftype.bits, fopt) };
unsafe {
raw::RediSearch_TextFieldSetWeight(self.inner, field_id, weight);
if let Some(separator) = tag_options.tag_separator {
raw::RediSearch_TagFieldSetSeparator(self.inner, field_id, separator as c_char);
}
if tag_options.tag_case_sensitive {
raw::RediSearch_TagFieldSetCaseSensitive(
self.inner,
field_id,
tag_options.tag_case_sensitive as c_int,
);
}
}

Field {
index: self,
Expand All @@ -71,6 +99,22 @@ impl Index {
}
}

pub fn del_document(&self, key: &str) -> Result<(), RedisError> {
let status = unsafe {
raw::RediSearch_DeleteDocument(
self.inner,
CString::new(key).unwrap().as_ptr() as *const c_void,
key.len(),
)
};

if status == redis_module::raw::REDISMODULE_ERR as i32 {
Err(RedisError::Str("error deleting document"))
} else {
Ok(())
}
}

pub fn search(&self, query_string: &str) -> Result<ResultsIterator, RedisError> {
let c_query = CString::new(query_string).unwrap();
let mut err_ptr = ptr::null_mut();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod index;
pub mod raw;

pub use document::Document;
pub use index::Index;
pub use index::{Index, TagOptions};

bitflags! {
pub struct FieldType: u32 {
Expand Down