-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhello_redisearch.rs
47 lines (36 loc) · 1.23 KB
/
hello_redisearch.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#[macro_use]
extern crate redis_module;
use redis_module::{Context, NextArg, RedisError, RedisResult};
use redisearch_api::{init, Document, FieldType, Index, TagOptions};
fn hello_redisearch(_: &Context, args: Vec<String>) -> RedisResult {
let mut args = args.into_iter().skip(1);
let search_term = args.next_string()?;
// FT.CREATE my_index
// SCHEMA
// $a.b.c TEXT WEIGHT 5.0
// $a.b.d TEXT
// $b.a TEXT
// see RediSearch: t_llapi.cpp
let index_name = "index";
let field_name = "foo";
let score = 1.0;
let index = Index::create(index_name);
index.create_field(field_name, 1.0, TagOptions::default());
let doc = Document::create("doc1", score);
doc.add_field(field_name, "bar", FieldType::FULLTEXT);
index.add_document(&doc)?;
let doc2 = Document::create("doc2", score);
doc2.add_field(field_name, "quux", FieldType::FULLTEXT);
index.add_document(&doc2)?;
let keys: Vec<_> = index.search(search_term.as_str())?.collect();
Ok(keys.into())
}
redis_module! {
name: "hello_redisearch",
version: 1,
data_types: [],
init: init,
commands: [
["hello_redisearch", hello_redisearch, "readonly", 1, 1, 1],
],
}