Skip to content

Commit 67b528c

Browse files
committed
Support RediSearch field types
1 parent b563ede commit 67b528c

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

Diff for: examples/hello_redisearch.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate redismodule;
33

4-
use redisearch_api::{self, init, Document, Index};
4+
use redisearch_api::{self, init, Document, FieldType, Index};
55
use redismodule::{Context, NextArg, RedisError, RedisResult};
66

77
fn hello_redisearch(_: &Context, args: Vec<String>) -> RedisResult {
@@ -24,11 +24,11 @@ fn hello_redisearch(_: &Context, args: Vec<String>) -> RedisResult {
2424
index.create_field(field_name);
2525

2626
let doc = Document::create("doc1", score);
27-
doc.add_field(field_name, "bar");
27+
doc.add_field(field_name, "bar", FieldType::FULLTEXT);
2828
index.add_document(&doc)?;
2929

3030
let doc2 = Document::create("doc2", score);
31-
doc2.add_field(field_name, "quux");
31+
doc2.add_field(field_name, "quux", FieldType::FULLTEXT);
3232
index.add_document(&doc2)?;
3333

3434
let keys: Vec<_> = index.search(search_term.as_str())?.collect();

Diff for: src/document.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ffi::CString;
22
use std::os::raw::c_void;
33
use std::ptr;
44

5-
use crate::raw;
5+
use crate::{raw, FieldType};
66

77
pub struct Document {
88
pub(crate) inner: *mut raw::RSDoc,
@@ -20,7 +20,7 @@ impl Document {
2020
Self { inner: doc }
2121
}
2222

23-
pub fn add_field(&self, name: &str, value: &str) {
23+
pub fn add_field(&self, name: &str, value: &str, field_type: FieldType) {
2424
let name = CString::new(name).unwrap();
2525
let c_value = CString::new(value).unwrap();
2626
unsafe {
@@ -29,7 +29,7 @@ impl Document {
2929
name.as_ptr(),
3030
c_value.as_ptr(),
3131
value.len(),
32-
raw::RSFLDTYPE_FULLTEXT,
32+
field_type.bits,
3333
);
3434
}
3535
}

Diff for: src/index.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ptr;
44
use num_traits::ToPrimitive;
55

66
use crate::raw::{self, RSFieldID, RSResultsIterator, GC_POLICY_FORK, GC_POLICY_NONE};
7-
use crate::Document;
7+
use crate::{Document, FieldType};
88
use redismodule::RedisError;
99
use std::os::raw::c_char;
1010

@@ -51,11 +51,12 @@ impl Index {
5151
debug!("Creating index field '{}'", name);
5252
let name = CString::new(name).unwrap();
5353

54-
let ftype = raw::RSFLDTYPE_FULLTEXT;
54+
// We want to let the document decide the type, so we support all types.
55+
let ftype = FieldType::FULLTEXT | FieldType::NUMERIC | FieldType::TAG;
5556
let fopt = raw::RSFLDOPT_NONE;
5657

5758
let field_id =
58-
unsafe { raw::RediSearch_CreateField(self.inner, name.as_ptr(), ftype, fopt) };
59+
unsafe { raw::RediSearch_CreateField(self.inner, name.as_ptr(), ftype.bits, fopt) };
5960

6061
Field {
6162
index: self,

Diff for: src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::os::raw::c_int;
22

3+
#[macro_use]
4+
extern crate bitflags;
5+
36
#[macro_use]
47
extern crate enum_primitive_derive;
58

@@ -12,6 +15,15 @@ pub mod raw;
1215
pub use document::Document;
1316
pub use index::Index;
1417

18+
bitflags! {
19+
pub struct FieldType: u32 {
20+
const FULLTEXT = raw::RSFLDTYPE_FULLTEXT;
21+
const NUMERIC = raw::RSFLDTYPE_NUMERIC;
22+
const GEO = raw::RSFLDTYPE_GEO;
23+
const TAG = raw::RSFLDTYPE_TAG;
24+
}
25+
}
26+
1527
pub fn get_c_api_version() -> i32 {
1628
unsafe { raw::RediSearch_GetCApiVersion() }
1729
}

0 commit comments

Comments
 (0)