pub struct Surf { /* private fields */ }
Expand description
Surfer: Client API
Implementations§
Source§impl Surf
impl Surf
pub fn new(surfer: Surfer) -> Self
Sourcepub fn apply<T: Serialize + DeserializeOwned>(
&mut self,
index_name: &str,
conditions: &Vec<OrCondition>,
limit: Option<usize>,
score: Option<f32>,
) -> Result<Option<Vec<T>>, IndexError>
pub fn apply<T: Serialize + DeserializeOwned>( &mut self, index_name: &str, conditions: &Vec<OrCondition>, limit: Option<usize>, score: Option<f32>, ) -> Result<Option<Vec<T>>, IndexError>
Use apply to manage and limit the output
Sourcepub fn select<T: Serialize + DeserializeOwned>(
&mut self,
index_name: &str,
conditions: &Vec<OrCondition>,
) -> Result<Option<Vec<T>>, IndexError>
pub fn select<T: Serialize + DeserializeOwned>( &mut self, index_name: &str, conditions: &Vec<OrCondition>, ) -> Result<Option<Vec<T>>, IndexError>
Similar to SQL Select
Examples found in repository?
examples/00_usage.rs (line 93)
40fn main() {
41 // Specify home location for indexes
42 let home = ".store".to_string();
43 // Specify index name
44 let index_name = "usage".to_string();
45
46 // Prepare builder
47 let mut builder = SurferBuilder::default();
48 builder.set_home(&home);
49
50 let data = UserInfo::default();
51 builder.add_struct(index_name.clone(), &data);
52
53 // Prepare Surf
54 let mut surf = Surf::try_from(builder).unwrap();
55
56 // Prepare data to insert & search
57
58 // User 1: John Doe
59 let first = "John".to_string();
60 let last = "Doe".to_string();
61 let age = 20u8;
62 let john_doe = UserInfo::new(first, last, age);
63
64 // User 2: Jane Doe
65 let first = "Jane".to_string();
66 let last = "Doe".to_string();
67 let age = 18u8;
68 let jane_doe = UserInfo::new(first, last, age);
69
70 // See examples for more options
71 let users = vec![john_doe.clone(), jane_doe.clone()];
72 let _ = surf.insert(&index_name, &users).unwrap();
73
74 block_thread(1);
75
76 // See examples for more options
77 // Similar to SELECT * FROM users WHERE (age = 20 AND last = "Doe") OR (first = "Jane")
78 let conditions = vec![
79 OrCondition::new(
80 // (age = 20 AND last = "Doe")
81 vec![
82 AndCondition::new("age".to_string(), "20".to_string()),
83 AndCondition::new("last".to_string(), "doe".to_string())
84 ]),
85 OrCondition::new(
86 // (first = "Jane")
87 vec![
88 AndCondition::new("first".to_string(), "jane".to_string())
89 ])
90 ];
91
92 // Validate John and Jane Doe
93 let mut computed = surf.select::<UserInfo>(&index_name, &conditions).unwrap().unwrap();
94 let mut expected = vec![john_doe.clone(), jane_doe.clone(), ];
95 expected.sort();
96 computed.sort();
97 assert_eq!(expected, computed);
98
99 // Validated John's record - Alternate shortcut for query using one field only
100 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
101 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
102 assert_eq!(vec![john_doe], computed);
103
104 // Delete John's record
105 let result = surf.delete(&index_name, "age", "20");
106 assert!(result.is_ok());
107
108 // John's was removed
109 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
110 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
111 assert!(computed.is_empty());
112
113
114 // Clean-up
115 let path = surf.which_index(&index_name).unwrap();
116 let _ = remove_dir_all(&path);
117 let _ = remove_dir_all(&home);
118}
Sourcepub fn insert<T: Serialize>(
&mut self,
name: &str,
payload: &Vec<T>,
) -> Result<(), IndexError>
pub fn insert<T: Serialize>( &mut self, name: &str, payload: &Vec<T>, ) -> Result<(), IndexError>
Similar to SQL Insert
Examples found in repository?
examples/00_usage.rs (line 72)
40fn main() {
41 // Specify home location for indexes
42 let home = ".store".to_string();
43 // Specify index name
44 let index_name = "usage".to_string();
45
46 // Prepare builder
47 let mut builder = SurferBuilder::default();
48 builder.set_home(&home);
49
50 let data = UserInfo::default();
51 builder.add_struct(index_name.clone(), &data);
52
53 // Prepare Surf
54 let mut surf = Surf::try_from(builder).unwrap();
55
56 // Prepare data to insert & search
57
58 // User 1: John Doe
59 let first = "John".to_string();
60 let last = "Doe".to_string();
61 let age = 20u8;
62 let john_doe = UserInfo::new(first, last, age);
63
64 // User 2: Jane Doe
65 let first = "Jane".to_string();
66 let last = "Doe".to_string();
67 let age = 18u8;
68 let jane_doe = UserInfo::new(first, last, age);
69
70 // See examples for more options
71 let users = vec![john_doe.clone(), jane_doe.clone()];
72 let _ = surf.insert(&index_name, &users).unwrap();
73
74 block_thread(1);
75
76 // See examples for more options
77 // Similar to SELECT * FROM users WHERE (age = 20 AND last = "Doe") OR (first = "Jane")
78 let conditions = vec![
79 OrCondition::new(
80 // (age = 20 AND last = "Doe")
81 vec![
82 AndCondition::new("age".to_string(), "20".to_string()),
83 AndCondition::new("last".to_string(), "doe".to_string())
84 ]),
85 OrCondition::new(
86 // (first = "Jane")
87 vec![
88 AndCondition::new("first".to_string(), "jane".to_string())
89 ])
90 ];
91
92 // Validate John and Jane Doe
93 let mut computed = surf.select::<UserInfo>(&index_name, &conditions).unwrap().unwrap();
94 let mut expected = vec![john_doe.clone(), jane_doe.clone(), ];
95 expected.sort();
96 computed.sort();
97 assert_eq!(expected, computed);
98
99 // Validated John's record - Alternate shortcut for query using one field only
100 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
101 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
102 assert_eq!(vec![john_doe], computed);
103
104 // Delete John's record
105 let result = surf.delete(&index_name, "age", "20");
106 assert!(result.is_ok());
107
108 // John's was removed
109 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
110 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
111 assert!(computed.is_empty());
112
113
114 // Clean-up
115 let path = surf.which_index(&index_name).unwrap();
116 let _ = remove_dir_all(&path);
117 let _ = remove_dir_all(&home);
118}
Sourcepub fn delete(
&mut self,
index_name: &str,
field_name: &str,
field_value: &str,
) -> Result<(), IndexError>
pub fn delete( &mut self, index_name: &str, field_name: &str, field_value: &str, ) -> Result<(), IndexError>
Aims to be similar to SQL Delete
Examples found in repository?
examples/00_usage.rs (line 105)
40fn main() {
41 // Specify home location for indexes
42 let home = ".store".to_string();
43 // Specify index name
44 let index_name = "usage".to_string();
45
46 // Prepare builder
47 let mut builder = SurferBuilder::default();
48 builder.set_home(&home);
49
50 let data = UserInfo::default();
51 builder.add_struct(index_name.clone(), &data);
52
53 // Prepare Surf
54 let mut surf = Surf::try_from(builder).unwrap();
55
56 // Prepare data to insert & search
57
58 // User 1: John Doe
59 let first = "John".to_string();
60 let last = "Doe".to_string();
61 let age = 20u8;
62 let john_doe = UserInfo::new(first, last, age);
63
64 // User 2: Jane Doe
65 let first = "Jane".to_string();
66 let last = "Doe".to_string();
67 let age = 18u8;
68 let jane_doe = UserInfo::new(first, last, age);
69
70 // See examples for more options
71 let users = vec![john_doe.clone(), jane_doe.clone()];
72 let _ = surf.insert(&index_name, &users).unwrap();
73
74 block_thread(1);
75
76 // See examples for more options
77 // Similar to SELECT * FROM users WHERE (age = 20 AND last = "Doe") OR (first = "Jane")
78 let conditions = vec![
79 OrCondition::new(
80 // (age = 20 AND last = "Doe")
81 vec![
82 AndCondition::new("age".to_string(), "20".to_string()),
83 AndCondition::new("last".to_string(), "doe".to_string())
84 ]),
85 OrCondition::new(
86 // (first = "Jane")
87 vec![
88 AndCondition::new("first".to_string(), "jane".to_string())
89 ])
90 ];
91
92 // Validate John and Jane Doe
93 let mut computed = surf.select::<UserInfo>(&index_name, &conditions).unwrap().unwrap();
94 let mut expected = vec![john_doe.clone(), jane_doe.clone(), ];
95 expected.sort();
96 computed.sort();
97 assert_eq!(expected, computed);
98
99 // Validated John's record - Alternate shortcut for query using one field only
100 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
101 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
102 assert_eq!(vec![john_doe], computed);
103
104 // Delete John's record
105 let result = surf.delete(&index_name, "age", "20");
106 assert!(result.is_ok());
107
108 // John's was removed
109 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
110 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
111 assert!(computed.is_empty());
112
113
114 // Clean-up
115 let path = surf.which_index(&index_name).unwrap();
116 let _ = remove_dir_all(&path);
117 let _ = remove_dir_all(&home);
118}
Methods from Deref<Target = Surfer>§
Sourcepub fn resolve_schema(&self, name: &str) -> Option<&SurferSchema>
pub fn resolve_schema(&self, name: &str) -> Option<&SurferSchema>
Access to Surfer Schema
Sourcepub fn which_index(&self, name: &str) -> Option<String>
pub fn which_index(&self, name: &str) -> Option<String>
Location of Index
Examples found in repository?
examples/03_fuzzy_user_name.rs (line 96)
33fn main() {
34 let home = ".store".to_string();
35 let name = "fuzzy".to_string();
36
37 let mut builder = SurferBuilder::default();
38 builder.set_home(&home);
39
40 let data = User::default();
41 builder.add_struct(name.clone(), &data);
42
43 let mut surfer = Surfer::try_from(builder).unwrap();
44
45 let first = "John".to_string();
46 let last = "Doe".to_string();
47 let john_doe = User::new(first, last);
48
49 let first = "Jane".to_string();
50 let last = "Doe".to_string();
51 let jane_doe = User::new(first, last);
52
53 let users = vec![john_doe.clone(), jane_doe.clone()];
54 let _ = surfer.insert_structs(&name, &users).unwrap();
55 println!("===========================");
56 println!("Insert: John & Jane Doe");
57 println!("---------------------------");
58 println!("{:#?}", users);
59 println!("---------------------------");
60
61 block_thread(2);
62
63 let query = "deo";
64 let mut computed = surfer.read_structs::<User>(&name, query, Some(100), None).unwrap().unwrap();
65 computed.sort();
66 let mut expected = vec![];
67 expected.sort();
68 assert_eq!(computed, expected);
69
70 println!("====================================");
71 println!("Incorrect Query: '{}' Select: None", query);
72 println!("------------------------------------");
73 println!("{:#?}", computed);
74 println!("------------------------------------");
75
76 let fuzz = FuzzyWord::default();
77 let mod_query = fuzz.lookup(query);
78 assert!(mod_query.is_some());
79 let adjusted = mod_query.unwrap();
80 assert!(adjusted.len() >= 1);
81 let adjusted = adjusted.get(0).unwrap();
82 let mut computed = surfer.read_structs::<User>(&name, adjusted, Some(100), None).unwrap().unwrap();
83 computed.sort();
84 let mut expected = vec![john_doe.clone(), jane_doe.clone()];
85 expected.sort();
86 assert_eq!(computed, expected);
87
88 println!("====================================");
89 println!("Adjusted Query: '{}' Select: Jane & John Doe", adjusted);
90 println!("------------------------------------");
91 println!("{:#?}", computed);
92 println!("------------------------------------");
93
94
95 // Clean-up
96 let path = surfer.which_index(&name).unwrap();
97 let _ = remove_dir_all(&path);
98 let _ = remove_dir_all(&home);
99}
More examples
examples/00_usage.rs (line 115)
40fn main() {
41 // Specify home location for indexes
42 let home = ".store".to_string();
43 // Specify index name
44 let index_name = "usage".to_string();
45
46 // Prepare builder
47 let mut builder = SurferBuilder::default();
48 builder.set_home(&home);
49
50 let data = UserInfo::default();
51 builder.add_struct(index_name.clone(), &data);
52
53 // Prepare Surf
54 let mut surf = Surf::try_from(builder).unwrap();
55
56 // Prepare data to insert & search
57
58 // User 1: John Doe
59 let first = "John".to_string();
60 let last = "Doe".to_string();
61 let age = 20u8;
62 let john_doe = UserInfo::new(first, last, age);
63
64 // User 2: Jane Doe
65 let first = "Jane".to_string();
66 let last = "Doe".to_string();
67 let age = 18u8;
68 let jane_doe = UserInfo::new(first, last, age);
69
70 // See examples for more options
71 let users = vec![john_doe.clone(), jane_doe.clone()];
72 let _ = surf.insert(&index_name, &users).unwrap();
73
74 block_thread(1);
75
76 // See examples for more options
77 // Similar to SELECT * FROM users WHERE (age = 20 AND last = "Doe") OR (first = "Jane")
78 let conditions = vec![
79 OrCondition::new(
80 // (age = 20 AND last = "Doe")
81 vec![
82 AndCondition::new("age".to_string(), "20".to_string()),
83 AndCondition::new("last".to_string(), "doe".to_string())
84 ]),
85 OrCondition::new(
86 // (first = "Jane")
87 vec![
88 AndCondition::new("first".to_string(), "jane".to_string())
89 ])
90 ];
91
92 // Validate John and Jane Doe
93 let mut computed = surf.select::<UserInfo>(&index_name, &conditions).unwrap().unwrap();
94 let mut expected = vec![john_doe.clone(), jane_doe.clone(), ];
95 expected.sort();
96 computed.sort();
97 assert_eq!(expected, computed);
98
99 // Validated John's record - Alternate shortcut for query using one field only
100 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
101 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
102 assert_eq!(vec![john_doe], computed);
103
104 // Delete John's record
105 let result = surf.delete(&index_name, "age", "20");
106 assert!(result.is_ok());
107
108 // John's was removed
109 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
110 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
111 assert!(computed.is_empty());
112
113
114 // Clean-up
115 let path = surf.which_index(&index_name).unwrap();
116 let _ = remove_dir_all(&path);
117 let _ = remove_dir_all(&home);
118}
examples/02_example_from_tantivy.rs (line 110)
33fn main() {
34 let home = ".store".to_string();
35 let name = "tantivy".to_string();
36
37 // Mostly empty but can be a real flat struct
38 let data = OldMan::default();
39
40 // Prepare the builder instance
41 let mut builder = SurferBuilder::default();
42 // By default everything goes to directory indexes
43 builder.set_home(&home);
44 builder.add_struct(name.clone(), &data);
45
46 // Make the Surfer
47 let mut surfer = Surfer::try_from(builder).unwrap();
48
49 // Prepare your data or get it from somewhere
50 let title = "The Old Man and the Sea".to_string();
51 let body = "He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish.".to_string();
52 let old_man = OldMan::new(title, body);
53
54 // Insert the data so that store as only one document
55 let _ = surfer.insert_struct(&name, &old_man).unwrap();
56 println!("Inserting document: 1");
57
58 // Give some time to indexing to complete
59 block_thread(2);
60
61 // Lets query our one document
62 let query = "sea whale";
63 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
64 // Check one document
65 println!("Total documents found: {}", computed.len());
66 assert_eq!(computed, vec![old_man.clone()]);
67
68 // Insert the data so that store as two document
69 let _ = surfer.insert_struct(&name, &old_man).unwrap();
70 println!("Inserting document: 1");
71
72 // Give some time to indexing to complete
73 block_thread(2);
74
75 // Lets query again for two documents
76 let query = "sea whale";
77 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
78 // Check two documents
79 println!("Total documents found: {}", computed.len());
80 assert_eq!(computed, vec![old_man.clone(), old_man.clone()]);
81
82 // Lets add 100 more documents
83 let mut i = 0;
84 let mut documents = Vec::with_capacity(50);
85 while i < 50 {
86 documents.push(old_man.clone());
87 i = i + 1;
88 };
89 let _ = surfer.insert_structs(&name, &documents).unwrap();
90 println!("Inserting document: 50");
91
92 // Give some time to indexing to complete
93 block_thread(3);
94
95 // Lets query again for to get first 10 only
96 let query = "sea whale";
97 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
98 // Check 10 documents
99 println!("Total documents found: {} due to default limit = 10", computed.len());
100 assert_eq!(computed.len(), 10);
101
102 // Lets query again for to get first 10 only
103 let query = "sea whale";
104 let computed = surfer.read_structs::<OldMan>(&name, query, Some(100), None).unwrap().unwrap();
105 // Check 10 documents
106 println!("Total documents found: {} with limit = 100", computed.len());
107 assert_eq!(computed.len(), 52);
108
109 // Clean-up
110 let path = surfer.which_index(&name).unwrap();
111 let _ = remove_dir_all(&path);
112 let _ = remove_dir_all(&home);
113}
examples/01_helloworld.rs (line 156)
42fn main() {
43 // Specify home location for indexes
44 let home = ".store".to_string();
45 // Specify index name
46 let index_name = "test_user_info".to_string();
47
48 // Prepare builder
49 let mut builder = SurferBuilder::default();
50 builder.set_home(&home);
51
52 let data = UserInfo::default();
53 builder.add_struct(index_name.clone(), &data);
54
55 // Prepare Surfer
56 let mut surfer = Surfer::try_from(builder).unwrap();
57
58 // Prepare data to insert & search
59
60 // User 1: John Doe
61 let first = "John".to_string();
62 let last = "Doe".to_string();
63 let age = 20u8;
64 let john_doe = UserInfo::new(first, last, age);
65
66 // User 2: Jane Doe
67 let first = "Jane".to_string();
68 let last = "Doe".to_string();
69 let age = 18u8;
70 let jane_doe = UserInfo::new(first, last, age);
71
72 // User 3: Jonny Doe
73 let first = "Jonny".to_string();
74 let last = "Doe".to_string();
75 let age = 10u8;
76 let jonny_doe = UserInfo::new(first, last, age);
77
78 // User 4: Jinny Doe
79 let first = "Jinny".to_string();
80 let last = "Doe".to_string();
81 let age = 10u8;
82 let jinny_doe = UserInfo::new(first, last, age);
83
84 // Writing structs
85
86 // Option 1: One struct at a time
87 let _ = surfer.insert_struct(&index_name, &john_doe).unwrap();
88 let _ = surfer.insert_struct(&index_name, &jane_doe).unwrap();
89
90 // Option 2: Write all structs together
91 let users = vec![jonny_doe.clone(), jinny_doe.clone()];
92 let _ = surfer.insert_structs(&index_name, &users).unwrap();
93
94 block_thread(1);
95
96 // Reading structs
97
98 // Option 1: Full text search
99 let expected = vec![john_doe.clone()];
100 let computed = surfer.read_all_structs::<UserInfo>(&index_name, "John").unwrap().unwrap();
101 assert_eq!(expected, computed);
102
103 let mut expected = vec![john_doe.clone(), jane_doe.clone(), jonny_doe.clone(), jinny_doe.clone()];
104 expected.sort();
105 let mut computed = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
106 computed.sort();
107 assert_eq!(expected, computed);
108
109 // Option 2: Term search
110 let mut expected = vec![jonny_doe.clone(), jinny_doe.clone()];
111 expected.sort();
112 let mut computed = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
113 computed.sort();
114 assert_eq!(expected, computed);
115
116 // Delete structs
117
118 // Option 1: Delete based on all text fields
119 // Before delete
120 let before = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
121 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
122
123 // Delete any occurrence of John (Actual call to delete)
124 surfer.delete_structs(&index_name, "john").unwrap();
125
126 // After delete
127 let after = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
128 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
129 // Check difference
130 let computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
131 // Only John should be deleted
132 let expected = vec![john_doe];
133 assert_eq!(expected, computed);
134
135 // Option 2: Delete based on a specific field
136 // Before delete
137 let before = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
138 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
139
140 // Delete any occurrence where age = 10 (Actual call to delete)
141 surfer.delete_structs_by_field(&index_name, "age", "10").unwrap();
142
143 // After delete
144 let after = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
145 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
146 // Check difference
147 let mut computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
148 computed.sort();
149 // Both Jonny & Jinny should be deleted
150 let mut expected = vec![jonny_doe, jinny_doe];
151 expected.sort();
152 assert_eq!(expected, computed);
153
154
155 // Clean-up
156 let path = surfer.which_index(&index_name).unwrap();
157 let _ = remove_dir_all(&path);
158 let _ = remove_dir_all(&home);
159}
Sourcepub fn resolve_index(&self, name: &str) -> Option<&Index>
pub fn resolve_index(&self, name: &str) -> Option<&Index>
Access to Index
Sourcepub fn insert_struct<T: Serialize>(
&mut self,
name: &str,
data: &T,
) -> Result<(), IndexError>
pub fn insert_struct<T: Serialize>( &mut self, name: &str, data: &T, ) -> Result<(), IndexError>
Inserts a struct
Examples found in repository?
examples/02_example_from_tantivy.rs (line 55)
33fn main() {
34 let home = ".store".to_string();
35 let name = "tantivy".to_string();
36
37 // Mostly empty but can be a real flat struct
38 let data = OldMan::default();
39
40 // Prepare the builder instance
41 let mut builder = SurferBuilder::default();
42 // By default everything goes to directory indexes
43 builder.set_home(&home);
44 builder.add_struct(name.clone(), &data);
45
46 // Make the Surfer
47 let mut surfer = Surfer::try_from(builder).unwrap();
48
49 // Prepare your data or get it from somewhere
50 let title = "The Old Man and the Sea".to_string();
51 let body = "He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish.".to_string();
52 let old_man = OldMan::new(title, body);
53
54 // Insert the data so that store as only one document
55 let _ = surfer.insert_struct(&name, &old_man).unwrap();
56 println!("Inserting document: 1");
57
58 // Give some time to indexing to complete
59 block_thread(2);
60
61 // Lets query our one document
62 let query = "sea whale";
63 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
64 // Check one document
65 println!("Total documents found: {}", computed.len());
66 assert_eq!(computed, vec![old_man.clone()]);
67
68 // Insert the data so that store as two document
69 let _ = surfer.insert_struct(&name, &old_man).unwrap();
70 println!("Inserting document: 1");
71
72 // Give some time to indexing to complete
73 block_thread(2);
74
75 // Lets query again for two documents
76 let query = "sea whale";
77 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
78 // Check two documents
79 println!("Total documents found: {}", computed.len());
80 assert_eq!(computed, vec![old_man.clone(), old_man.clone()]);
81
82 // Lets add 100 more documents
83 let mut i = 0;
84 let mut documents = Vec::with_capacity(50);
85 while i < 50 {
86 documents.push(old_man.clone());
87 i = i + 1;
88 };
89 let _ = surfer.insert_structs(&name, &documents).unwrap();
90 println!("Inserting document: 50");
91
92 // Give some time to indexing to complete
93 block_thread(3);
94
95 // Lets query again for to get first 10 only
96 let query = "sea whale";
97 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
98 // Check 10 documents
99 println!("Total documents found: {} due to default limit = 10", computed.len());
100 assert_eq!(computed.len(), 10);
101
102 // Lets query again for to get first 10 only
103 let query = "sea whale";
104 let computed = surfer.read_structs::<OldMan>(&name, query, Some(100), None).unwrap().unwrap();
105 // Check 10 documents
106 println!("Total documents found: {} with limit = 100", computed.len());
107 assert_eq!(computed.len(), 52);
108
109 // Clean-up
110 let path = surfer.which_index(&name).unwrap();
111 let _ = remove_dir_all(&path);
112 let _ = remove_dir_all(&home);
113}
More examples
examples/01_helloworld.rs (line 87)
42fn main() {
43 // Specify home location for indexes
44 let home = ".store".to_string();
45 // Specify index name
46 let index_name = "test_user_info".to_string();
47
48 // Prepare builder
49 let mut builder = SurferBuilder::default();
50 builder.set_home(&home);
51
52 let data = UserInfo::default();
53 builder.add_struct(index_name.clone(), &data);
54
55 // Prepare Surfer
56 let mut surfer = Surfer::try_from(builder).unwrap();
57
58 // Prepare data to insert & search
59
60 // User 1: John Doe
61 let first = "John".to_string();
62 let last = "Doe".to_string();
63 let age = 20u8;
64 let john_doe = UserInfo::new(first, last, age);
65
66 // User 2: Jane Doe
67 let first = "Jane".to_string();
68 let last = "Doe".to_string();
69 let age = 18u8;
70 let jane_doe = UserInfo::new(first, last, age);
71
72 // User 3: Jonny Doe
73 let first = "Jonny".to_string();
74 let last = "Doe".to_string();
75 let age = 10u8;
76 let jonny_doe = UserInfo::new(first, last, age);
77
78 // User 4: Jinny Doe
79 let first = "Jinny".to_string();
80 let last = "Doe".to_string();
81 let age = 10u8;
82 let jinny_doe = UserInfo::new(first, last, age);
83
84 // Writing structs
85
86 // Option 1: One struct at a time
87 let _ = surfer.insert_struct(&index_name, &john_doe).unwrap();
88 let _ = surfer.insert_struct(&index_name, &jane_doe).unwrap();
89
90 // Option 2: Write all structs together
91 let users = vec![jonny_doe.clone(), jinny_doe.clone()];
92 let _ = surfer.insert_structs(&index_name, &users).unwrap();
93
94 block_thread(1);
95
96 // Reading structs
97
98 // Option 1: Full text search
99 let expected = vec![john_doe.clone()];
100 let computed = surfer.read_all_structs::<UserInfo>(&index_name, "John").unwrap().unwrap();
101 assert_eq!(expected, computed);
102
103 let mut expected = vec![john_doe.clone(), jane_doe.clone(), jonny_doe.clone(), jinny_doe.clone()];
104 expected.sort();
105 let mut computed = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
106 computed.sort();
107 assert_eq!(expected, computed);
108
109 // Option 2: Term search
110 let mut expected = vec![jonny_doe.clone(), jinny_doe.clone()];
111 expected.sort();
112 let mut computed = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
113 computed.sort();
114 assert_eq!(expected, computed);
115
116 // Delete structs
117
118 // Option 1: Delete based on all text fields
119 // Before delete
120 let before = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
121 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
122
123 // Delete any occurrence of John (Actual call to delete)
124 surfer.delete_structs(&index_name, "john").unwrap();
125
126 // After delete
127 let after = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
128 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
129 // Check difference
130 let computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
131 // Only John should be deleted
132 let expected = vec![john_doe];
133 assert_eq!(expected, computed);
134
135 // Option 2: Delete based on a specific field
136 // Before delete
137 let before = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
138 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
139
140 // Delete any occurrence where age = 10 (Actual call to delete)
141 surfer.delete_structs_by_field(&index_name, "age", "10").unwrap();
142
143 // After delete
144 let after = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
145 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
146 // Check difference
147 let mut computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
148 computed.sort();
149 // Both Jonny & Jinny should be deleted
150 let mut expected = vec![jonny_doe, jinny_doe];
151 expected.sort();
152 assert_eq!(expected, computed);
153
154
155 // Clean-up
156 let path = surfer.which_index(&index_name).unwrap();
157 let _ = remove_dir_all(&path);
158 let _ = remove_dir_all(&home);
159}
Sourcepub fn insert_structs<T: Serialize>(
&mut self,
name: &str,
payload: &Vec<T>,
) -> Result<(), IndexError>
pub fn insert_structs<T: Serialize>( &mut self, name: &str, payload: &Vec<T>, ) -> Result<(), IndexError>
Inserts a structs
Examples found in repository?
examples/03_fuzzy_user_name.rs (line 54)
33fn main() {
34 let home = ".store".to_string();
35 let name = "fuzzy".to_string();
36
37 let mut builder = SurferBuilder::default();
38 builder.set_home(&home);
39
40 let data = User::default();
41 builder.add_struct(name.clone(), &data);
42
43 let mut surfer = Surfer::try_from(builder).unwrap();
44
45 let first = "John".to_string();
46 let last = "Doe".to_string();
47 let john_doe = User::new(first, last);
48
49 let first = "Jane".to_string();
50 let last = "Doe".to_string();
51 let jane_doe = User::new(first, last);
52
53 let users = vec![john_doe.clone(), jane_doe.clone()];
54 let _ = surfer.insert_structs(&name, &users).unwrap();
55 println!("===========================");
56 println!("Insert: John & Jane Doe");
57 println!("---------------------------");
58 println!("{:#?}", users);
59 println!("---------------------------");
60
61 block_thread(2);
62
63 let query = "deo";
64 let mut computed = surfer.read_structs::<User>(&name, query, Some(100), None).unwrap().unwrap();
65 computed.sort();
66 let mut expected = vec![];
67 expected.sort();
68 assert_eq!(computed, expected);
69
70 println!("====================================");
71 println!("Incorrect Query: '{}' Select: None", query);
72 println!("------------------------------------");
73 println!("{:#?}", computed);
74 println!("------------------------------------");
75
76 let fuzz = FuzzyWord::default();
77 let mod_query = fuzz.lookup(query);
78 assert!(mod_query.is_some());
79 let adjusted = mod_query.unwrap();
80 assert!(adjusted.len() >= 1);
81 let adjusted = adjusted.get(0).unwrap();
82 let mut computed = surfer.read_structs::<User>(&name, adjusted, Some(100), None).unwrap().unwrap();
83 computed.sort();
84 let mut expected = vec![john_doe.clone(), jane_doe.clone()];
85 expected.sort();
86 assert_eq!(computed, expected);
87
88 println!("====================================");
89 println!("Adjusted Query: '{}' Select: Jane & John Doe", adjusted);
90 println!("------------------------------------");
91 println!("{:#?}", computed);
92 println!("------------------------------------");
93
94
95 // Clean-up
96 let path = surfer.which_index(&name).unwrap();
97 let _ = remove_dir_all(&path);
98 let _ = remove_dir_all(&home);
99}
More examples
examples/02_example_from_tantivy.rs (line 89)
33fn main() {
34 let home = ".store".to_string();
35 let name = "tantivy".to_string();
36
37 // Mostly empty but can be a real flat struct
38 let data = OldMan::default();
39
40 // Prepare the builder instance
41 let mut builder = SurferBuilder::default();
42 // By default everything goes to directory indexes
43 builder.set_home(&home);
44 builder.add_struct(name.clone(), &data);
45
46 // Make the Surfer
47 let mut surfer = Surfer::try_from(builder).unwrap();
48
49 // Prepare your data or get it from somewhere
50 let title = "The Old Man and the Sea".to_string();
51 let body = "He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish.".to_string();
52 let old_man = OldMan::new(title, body);
53
54 // Insert the data so that store as only one document
55 let _ = surfer.insert_struct(&name, &old_man).unwrap();
56 println!("Inserting document: 1");
57
58 // Give some time to indexing to complete
59 block_thread(2);
60
61 // Lets query our one document
62 let query = "sea whale";
63 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
64 // Check one document
65 println!("Total documents found: {}", computed.len());
66 assert_eq!(computed, vec![old_man.clone()]);
67
68 // Insert the data so that store as two document
69 let _ = surfer.insert_struct(&name, &old_man).unwrap();
70 println!("Inserting document: 1");
71
72 // Give some time to indexing to complete
73 block_thread(2);
74
75 // Lets query again for two documents
76 let query = "sea whale";
77 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
78 // Check two documents
79 println!("Total documents found: {}", computed.len());
80 assert_eq!(computed, vec![old_man.clone(), old_man.clone()]);
81
82 // Lets add 100 more documents
83 let mut i = 0;
84 let mut documents = Vec::with_capacity(50);
85 while i < 50 {
86 documents.push(old_man.clone());
87 i = i + 1;
88 };
89 let _ = surfer.insert_structs(&name, &documents).unwrap();
90 println!("Inserting document: 50");
91
92 // Give some time to indexing to complete
93 block_thread(3);
94
95 // Lets query again for to get first 10 only
96 let query = "sea whale";
97 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
98 // Check 10 documents
99 println!("Total documents found: {} due to default limit = 10", computed.len());
100 assert_eq!(computed.len(), 10);
101
102 // Lets query again for to get first 10 only
103 let query = "sea whale";
104 let computed = surfer.read_structs::<OldMan>(&name, query, Some(100), None).unwrap().unwrap();
105 // Check 10 documents
106 println!("Total documents found: {} with limit = 100", computed.len());
107 assert_eq!(computed.len(), 52);
108
109 // Clean-up
110 let path = surfer.which_index(&name).unwrap();
111 let _ = remove_dir_all(&path);
112 let _ = remove_dir_all(&home);
113}
examples/01_helloworld.rs (line 92)
42fn main() {
43 // Specify home location for indexes
44 let home = ".store".to_string();
45 // Specify index name
46 let index_name = "test_user_info".to_string();
47
48 // Prepare builder
49 let mut builder = SurferBuilder::default();
50 builder.set_home(&home);
51
52 let data = UserInfo::default();
53 builder.add_struct(index_name.clone(), &data);
54
55 // Prepare Surfer
56 let mut surfer = Surfer::try_from(builder).unwrap();
57
58 // Prepare data to insert & search
59
60 // User 1: John Doe
61 let first = "John".to_string();
62 let last = "Doe".to_string();
63 let age = 20u8;
64 let john_doe = UserInfo::new(first, last, age);
65
66 // User 2: Jane Doe
67 let first = "Jane".to_string();
68 let last = "Doe".to_string();
69 let age = 18u8;
70 let jane_doe = UserInfo::new(first, last, age);
71
72 // User 3: Jonny Doe
73 let first = "Jonny".to_string();
74 let last = "Doe".to_string();
75 let age = 10u8;
76 let jonny_doe = UserInfo::new(first, last, age);
77
78 // User 4: Jinny Doe
79 let first = "Jinny".to_string();
80 let last = "Doe".to_string();
81 let age = 10u8;
82 let jinny_doe = UserInfo::new(first, last, age);
83
84 // Writing structs
85
86 // Option 1: One struct at a time
87 let _ = surfer.insert_struct(&index_name, &john_doe).unwrap();
88 let _ = surfer.insert_struct(&index_name, &jane_doe).unwrap();
89
90 // Option 2: Write all structs together
91 let users = vec![jonny_doe.clone(), jinny_doe.clone()];
92 let _ = surfer.insert_structs(&index_name, &users).unwrap();
93
94 block_thread(1);
95
96 // Reading structs
97
98 // Option 1: Full text search
99 let expected = vec![john_doe.clone()];
100 let computed = surfer.read_all_structs::<UserInfo>(&index_name, "John").unwrap().unwrap();
101 assert_eq!(expected, computed);
102
103 let mut expected = vec![john_doe.clone(), jane_doe.clone(), jonny_doe.clone(), jinny_doe.clone()];
104 expected.sort();
105 let mut computed = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
106 computed.sort();
107 assert_eq!(expected, computed);
108
109 // Option 2: Term search
110 let mut expected = vec![jonny_doe.clone(), jinny_doe.clone()];
111 expected.sort();
112 let mut computed = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
113 computed.sort();
114 assert_eq!(expected, computed);
115
116 // Delete structs
117
118 // Option 1: Delete based on all text fields
119 // Before delete
120 let before = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
121 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
122
123 // Delete any occurrence of John (Actual call to delete)
124 surfer.delete_structs(&index_name, "john").unwrap();
125
126 // After delete
127 let after = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
128 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
129 // Check difference
130 let computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
131 // Only John should be deleted
132 let expected = vec![john_doe];
133 assert_eq!(expected, computed);
134
135 // Option 2: Delete based on a specific field
136 // Before delete
137 let before = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
138 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
139
140 // Delete any occurrence where age = 10 (Actual call to delete)
141 surfer.delete_structs_by_field(&index_name, "age", "10").unwrap();
142
143 // After delete
144 let after = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
145 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
146 // Check difference
147 let mut computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
148 computed.sort();
149 // Both Jonny & Jinny should be deleted
150 let mut expected = vec![jonny_doe, jinny_doe];
151 expected.sort();
152 assert_eq!(expected, computed);
153
154
155 // Clean-up
156 let path = surfer.which_index(&index_name).unwrap();
157 let _ = remove_dir_all(&path);
158 let _ = remove_dir_all(&home);
159}
Sourcepub fn delete_structs_by_field(
&mut self,
index_name: &str,
field_name: &str,
field_value: &str,
) -> Result<(), IndexError>
pub fn delete_structs_by_field( &mut self, index_name: &str, field_name: &str, field_value: &str, ) -> Result<(), IndexError>
Uses term search
Examples found in repository?
examples/01_helloworld.rs (line 141)
42fn main() {
43 // Specify home location for indexes
44 let home = ".store".to_string();
45 // Specify index name
46 let index_name = "test_user_info".to_string();
47
48 // Prepare builder
49 let mut builder = SurferBuilder::default();
50 builder.set_home(&home);
51
52 let data = UserInfo::default();
53 builder.add_struct(index_name.clone(), &data);
54
55 // Prepare Surfer
56 let mut surfer = Surfer::try_from(builder).unwrap();
57
58 // Prepare data to insert & search
59
60 // User 1: John Doe
61 let first = "John".to_string();
62 let last = "Doe".to_string();
63 let age = 20u8;
64 let john_doe = UserInfo::new(first, last, age);
65
66 // User 2: Jane Doe
67 let first = "Jane".to_string();
68 let last = "Doe".to_string();
69 let age = 18u8;
70 let jane_doe = UserInfo::new(first, last, age);
71
72 // User 3: Jonny Doe
73 let first = "Jonny".to_string();
74 let last = "Doe".to_string();
75 let age = 10u8;
76 let jonny_doe = UserInfo::new(first, last, age);
77
78 // User 4: Jinny Doe
79 let first = "Jinny".to_string();
80 let last = "Doe".to_string();
81 let age = 10u8;
82 let jinny_doe = UserInfo::new(first, last, age);
83
84 // Writing structs
85
86 // Option 1: One struct at a time
87 let _ = surfer.insert_struct(&index_name, &john_doe).unwrap();
88 let _ = surfer.insert_struct(&index_name, &jane_doe).unwrap();
89
90 // Option 2: Write all structs together
91 let users = vec![jonny_doe.clone(), jinny_doe.clone()];
92 let _ = surfer.insert_structs(&index_name, &users).unwrap();
93
94 block_thread(1);
95
96 // Reading structs
97
98 // Option 1: Full text search
99 let expected = vec![john_doe.clone()];
100 let computed = surfer.read_all_structs::<UserInfo>(&index_name, "John").unwrap().unwrap();
101 assert_eq!(expected, computed);
102
103 let mut expected = vec![john_doe.clone(), jane_doe.clone(), jonny_doe.clone(), jinny_doe.clone()];
104 expected.sort();
105 let mut computed = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
106 computed.sort();
107 assert_eq!(expected, computed);
108
109 // Option 2: Term search
110 let mut expected = vec![jonny_doe.clone(), jinny_doe.clone()];
111 expected.sort();
112 let mut computed = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
113 computed.sort();
114 assert_eq!(expected, computed);
115
116 // Delete structs
117
118 // Option 1: Delete based on all text fields
119 // Before delete
120 let before = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
121 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
122
123 // Delete any occurrence of John (Actual call to delete)
124 surfer.delete_structs(&index_name, "john").unwrap();
125
126 // After delete
127 let after = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
128 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
129 // Check difference
130 let computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
131 // Only John should be deleted
132 let expected = vec![john_doe];
133 assert_eq!(expected, computed);
134
135 // Option 2: Delete based on a specific field
136 // Before delete
137 let before = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
138 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
139
140 // Delete any occurrence where age = 10 (Actual call to delete)
141 surfer.delete_structs_by_field(&index_name, "age", "10").unwrap();
142
143 // After delete
144 let after = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
145 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
146 // Check difference
147 let mut computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
148 computed.sort();
149 // Both Jonny & Jinny should be deleted
150 let mut expected = vec![jonny_doe, jinny_doe];
151 expected.sort();
152 assert_eq!(expected, computed);
153
154
155 // Clean-up
156 let path = surfer.which_index(&index_name).unwrap();
157 let _ = remove_dir_all(&path);
158 let _ = remove_dir_all(&home);
159}
Sourcepub fn delete_structs(
&mut self,
index_name: &str,
field_value: &str,
) -> Result<(), IndexError>
pub fn delete_structs( &mut self, index_name: &str, field_value: &str, ) -> Result<(), IndexError>
Uses full text serach
Examples found in repository?
examples/01_helloworld.rs (line 124)
42fn main() {
43 // Specify home location for indexes
44 let home = ".store".to_string();
45 // Specify index name
46 let index_name = "test_user_info".to_string();
47
48 // Prepare builder
49 let mut builder = SurferBuilder::default();
50 builder.set_home(&home);
51
52 let data = UserInfo::default();
53 builder.add_struct(index_name.clone(), &data);
54
55 // Prepare Surfer
56 let mut surfer = Surfer::try_from(builder).unwrap();
57
58 // Prepare data to insert & search
59
60 // User 1: John Doe
61 let first = "John".to_string();
62 let last = "Doe".to_string();
63 let age = 20u8;
64 let john_doe = UserInfo::new(first, last, age);
65
66 // User 2: Jane Doe
67 let first = "Jane".to_string();
68 let last = "Doe".to_string();
69 let age = 18u8;
70 let jane_doe = UserInfo::new(first, last, age);
71
72 // User 3: Jonny Doe
73 let first = "Jonny".to_string();
74 let last = "Doe".to_string();
75 let age = 10u8;
76 let jonny_doe = UserInfo::new(first, last, age);
77
78 // User 4: Jinny Doe
79 let first = "Jinny".to_string();
80 let last = "Doe".to_string();
81 let age = 10u8;
82 let jinny_doe = UserInfo::new(first, last, age);
83
84 // Writing structs
85
86 // Option 1: One struct at a time
87 let _ = surfer.insert_struct(&index_name, &john_doe).unwrap();
88 let _ = surfer.insert_struct(&index_name, &jane_doe).unwrap();
89
90 // Option 2: Write all structs together
91 let users = vec![jonny_doe.clone(), jinny_doe.clone()];
92 let _ = surfer.insert_structs(&index_name, &users).unwrap();
93
94 block_thread(1);
95
96 // Reading structs
97
98 // Option 1: Full text search
99 let expected = vec![john_doe.clone()];
100 let computed = surfer.read_all_structs::<UserInfo>(&index_name, "John").unwrap().unwrap();
101 assert_eq!(expected, computed);
102
103 let mut expected = vec![john_doe.clone(), jane_doe.clone(), jonny_doe.clone(), jinny_doe.clone()];
104 expected.sort();
105 let mut computed = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
106 computed.sort();
107 assert_eq!(expected, computed);
108
109 // Option 2: Term search
110 let mut expected = vec![jonny_doe.clone(), jinny_doe.clone()];
111 expected.sort();
112 let mut computed = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
113 computed.sort();
114 assert_eq!(expected, computed);
115
116 // Delete structs
117
118 // Option 1: Delete based on all text fields
119 // Before delete
120 let before = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
121 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
122
123 // Delete any occurrence of John (Actual call to delete)
124 surfer.delete_structs(&index_name, "john").unwrap();
125
126 // After delete
127 let after = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
128 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
129 // Check difference
130 let computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
131 // Only John should be deleted
132 let expected = vec![john_doe];
133 assert_eq!(expected, computed);
134
135 // Option 2: Delete based on a specific field
136 // Before delete
137 let before = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
138 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
139
140 // Delete any occurrence where age = 10 (Actual call to delete)
141 surfer.delete_structs_by_field(&index_name, "age", "10").unwrap();
142
143 // After delete
144 let after = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
145 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
146 // Check difference
147 let mut computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
148 computed.sort();
149 // Both Jonny & Jinny should be deleted
150 let mut expected = vec![jonny_doe, jinny_doe];
151 expected.sort();
152 assert_eq!(expected, computed);
153
154
155 // Clean-up
156 let path = surfer.which_index(&index_name).unwrap();
157 let _ = remove_dir_all(&path);
158 let _ = remove_dir_all(&home);
159}
Sourcepub fn read_all_structs_by_field<T: Serialize + DeserializeOwned>(
&mut self,
index_name: &str,
field_name: &str,
field_value: &str,
) -> Result<Option<Vec<T>>, IndexError>
pub fn read_all_structs_by_field<T: Serialize + DeserializeOwned>( &mut self, index_name: &str, field_name: &str, field_value: &str, ) -> Result<Option<Vec<T>>, IndexError>
Uses term search
Examples found in repository?
examples/00_usage.rs (line 100)
40fn main() {
41 // Specify home location for indexes
42 let home = ".store".to_string();
43 // Specify index name
44 let index_name = "usage".to_string();
45
46 // Prepare builder
47 let mut builder = SurferBuilder::default();
48 builder.set_home(&home);
49
50 let data = UserInfo::default();
51 builder.add_struct(index_name.clone(), &data);
52
53 // Prepare Surf
54 let mut surf = Surf::try_from(builder).unwrap();
55
56 // Prepare data to insert & search
57
58 // User 1: John Doe
59 let first = "John".to_string();
60 let last = "Doe".to_string();
61 let age = 20u8;
62 let john_doe = UserInfo::new(first, last, age);
63
64 // User 2: Jane Doe
65 let first = "Jane".to_string();
66 let last = "Doe".to_string();
67 let age = 18u8;
68 let jane_doe = UserInfo::new(first, last, age);
69
70 // See examples for more options
71 let users = vec![john_doe.clone(), jane_doe.clone()];
72 let _ = surf.insert(&index_name, &users).unwrap();
73
74 block_thread(1);
75
76 // See examples for more options
77 // Similar to SELECT * FROM users WHERE (age = 20 AND last = "Doe") OR (first = "Jane")
78 let conditions = vec![
79 OrCondition::new(
80 // (age = 20 AND last = "Doe")
81 vec![
82 AndCondition::new("age".to_string(), "20".to_string()),
83 AndCondition::new("last".to_string(), "doe".to_string())
84 ]),
85 OrCondition::new(
86 // (first = "Jane")
87 vec![
88 AndCondition::new("first".to_string(), "jane".to_string())
89 ])
90 ];
91
92 // Validate John and Jane Doe
93 let mut computed = surf.select::<UserInfo>(&index_name, &conditions).unwrap().unwrap();
94 let mut expected = vec![john_doe.clone(), jane_doe.clone(), ];
95 expected.sort();
96 computed.sort();
97 assert_eq!(expected, computed);
98
99 // Validated John's record - Alternate shortcut for query using one field only
100 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
101 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
102 assert_eq!(vec![john_doe], computed);
103
104 // Delete John's record
105 let result = surf.delete(&index_name, "age", "20");
106 assert!(result.is_ok());
107
108 // John's was removed
109 let computed = surf.read_all_structs_by_field(&index_name, "age", "20");
110 let computed: Vec<UserInfo> = computed.unwrap().unwrap();
111 assert!(computed.is_empty());
112
113
114 // Clean-up
115 let path = surf.which_index(&index_name).unwrap();
116 let _ = remove_dir_all(&path);
117 let _ = remove_dir_all(&home);
118}
More examples
examples/01_helloworld.rs (line 112)
42fn main() {
43 // Specify home location for indexes
44 let home = ".store".to_string();
45 // Specify index name
46 let index_name = "test_user_info".to_string();
47
48 // Prepare builder
49 let mut builder = SurferBuilder::default();
50 builder.set_home(&home);
51
52 let data = UserInfo::default();
53 builder.add_struct(index_name.clone(), &data);
54
55 // Prepare Surfer
56 let mut surfer = Surfer::try_from(builder).unwrap();
57
58 // Prepare data to insert & search
59
60 // User 1: John Doe
61 let first = "John".to_string();
62 let last = "Doe".to_string();
63 let age = 20u8;
64 let john_doe = UserInfo::new(first, last, age);
65
66 // User 2: Jane Doe
67 let first = "Jane".to_string();
68 let last = "Doe".to_string();
69 let age = 18u8;
70 let jane_doe = UserInfo::new(first, last, age);
71
72 // User 3: Jonny Doe
73 let first = "Jonny".to_string();
74 let last = "Doe".to_string();
75 let age = 10u8;
76 let jonny_doe = UserInfo::new(first, last, age);
77
78 // User 4: Jinny Doe
79 let first = "Jinny".to_string();
80 let last = "Doe".to_string();
81 let age = 10u8;
82 let jinny_doe = UserInfo::new(first, last, age);
83
84 // Writing structs
85
86 // Option 1: One struct at a time
87 let _ = surfer.insert_struct(&index_name, &john_doe).unwrap();
88 let _ = surfer.insert_struct(&index_name, &jane_doe).unwrap();
89
90 // Option 2: Write all structs together
91 let users = vec![jonny_doe.clone(), jinny_doe.clone()];
92 let _ = surfer.insert_structs(&index_name, &users).unwrap();
93
94 block_thread(1);
95
96 // Reading structs
97
98 // Option 1: Full text search
99 let expected = vec![john_doe.clone()];
100 let computed = surfer.read_all_structs::<UserInfo>(&index_name, "John").unwrap().unwrap();
101 assert_eq!(expected, computed);
102
103 let mut expected = vec![john_doe.clone(), jane_doe.clone(), jonny_doe.clone(), jinny_doe.clone()];
104 expected.sort();
105 let mut computed = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
106 computed.sort();
107 assert_eq!(expected, computed);
108
109 // Option 2: Term search
110 let mut expected = vec![jonny_doe.clone(), jinny_doe.clone()];
111 expected.sort();
112 let mut computed = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
113 computed.sort();
114 assert_eq!(expected, computed);
115
116 // Delete structs
117
118 // Option 1: Delete based on all text fields
119 // Before delete
120 let before = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
121 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
122
123 // Delete any occurrence of John (Actual call to delete)
124 surfer.delete_structs(&index_name, "john").unwrap();
125
126 // After delete
127 let after = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
128 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
129 // Check difference
130 let computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
131 // Only John should be deleted
132 let expected = vec![john_doe];
133 assert_eq!(expected, computed);
134
135 // Option 2: Delete based on a specific field
136 // Before delete
137 let before = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
138 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
139
140 // Delete any occurrence where age = 10 (Actual call to delete)
141 surfer.delete_structs_by_field(&index_name, "age", "10").unwrap();
142
143 // After delete
144 let after = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
145 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
146 // Check difference
147 let mut computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
148 computed.sort();
149 // Both Jonny & Jinny should be deleted
150 let mut expected = vec![jonny_doe, jinny_doe];
151 expected.sort();
152 assert_eq!(expected, computed);
153
154
155 // Clean-up
156 let path = surfer.which_index(&index_name).unwrap();
157 let _ = remove_dir_all(&path);
158 let _ = remove_dir_all(&home);
159}
Sourcepub fn read_structs_by_field<T: Serialize + DeserializeOwned>(
&mut self,
index_name: &str,
field_name: &str,
field_value: &str,
limit: Option<usize>,
score: Option<f32>,
) -> Result<Option<Vec<T>>, IndexError>
pub fn read_structs_by_field<T: Serialize + DeserializeOwned>( &mut self, index_name: &str, field_name: &str, field_value: &str, limit: Option<usize>, score: Option<f32>, ) -> Result<Option<Vec<T>>, IndexError>
Uses term search
Sourcepub fn read_string(
&mut self,
name: &str,
query: &str,
limit: Option<usize>,
score: Option<f32>,
) -> Result<Option<Vec<String>>, IndexError>
pub fn read_string( &mut self, name: &str, query: &str, limit: Option<usize>, score: Option<f32>, ) -> Result<Option<Vec<String>>, IndexError>
Reads as string
Sourcepub fn read_all_structs<T: Serialize + DeserializeOwned>(
&mut self,
name: &str,
query: &str,
) -> Result<Option<Vec<T>>, IndexError>
pub fn read_all_structs<T: Serialize + DeserializeOwned>( &mut self, name: &str, query: &str, ) -> Result<Option<Vec<T>>, IndexError>
Reads as struct
Examples found in repository?
examples/01_helloworld.rs (line 100)
42fn main() {
43 // Specify home location for indexes
44 let home = ".store".to_string();
45 // Specify index name
46 let index_name = "test_user_info".to_string();
47
48 // Prepare builder
49 let mut builder = SurferBuilder::default();
50 builder.set_home(&home);
51
52 let data = UserInfo::default();
53 builder.add_struct(index_name.clone(), &data);
54
55 // Prepare Surfer
56 let mut surfer = Surfer::try_from(builder).unwrap();
57
58 // Prepare data to insert & search
59
60 // User 1: John Doe
61 let first = "John".to_string();
62 let last = "Doe".to_string();
63 let age = 20u8;
64 let john_doe = UserInfo::new(first, last, age);
65
66 // User 2: Jane Doe
67 let first = "Jane".to_string();
68 let last = "Doe".to_string();
69 let age = 18u8;
70 let jane_doe = UserInfo::new(first, last, age);
71
72 // User 3: Jonny Doe
73 let first = "Jonny".to_string();
74 let last = "Doe".to_string();
75 let age = 10u8;
76 let jonny_doe = UserInfo::new(first, last, age);
77
78 // User 4: Jinny Doe
79 let first = "Jinny".to_string();
80 let last = "Doe".to_string();
81 let age = 10u8;
82 let jinny_doe = UserInfo::new(first, last, age);
83
84 // Writing structs
85
86 // Option 1: One struct at a time
87 let _ = surfer.insert_struct(&index_name, &john_doe).unwrap();
88 let _ = surfer.insert_struct(&index_name, &jane_doe).unwrap();
89
90 // Option 2: Write all structs together
91 let users = vec![jonny_doe.clone(), jinny_doe.clone()];
92 let _ = surfer.insert_structs(&index_name, &users).unwrap();
93
94 block_thread(1);
95
96 // Reading structs
97
98 // Option 1: Full text search
99 let expected = vec![john_doe.clone()];
100 let computed = surfer.read_all_structs::<UserInfo>(&index_name, "John").unwrap().unwrap();
101 assert_eq!(expected, computed);
102
103 let mut expected = vec![john_doe.clone(), jane_doe.clone(), jonny_doe.clone(), jinny_doe.clone()];
104 expected.sort();
105 let mut computed = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
106 computed.sort();
107 assert_eq!(expected, computed);
108
109 // Option 2: Term search
110 let mut expected = vec![jonny_doe.clone(), jinny_doe.clone()];
111 expected.sort();
112 let mut computed = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
113 computed.sort();
114 assert_eq!(expected, computed);
115
116 // Delete structs
117
118 // Option 1: Delete based on all text fields
119 // Before delete
120 let before = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
121 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
122
123 // Delete any occurrence of John (Actual call to delete)
124 surfer.delete_structs(&index_name, "john").unwrap();
125
126 // After delete
127 let after = surfer.read_all_structs::<UserInfo>(&index_name, "doe").unwrap().unwrap();
128 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
129 // Check difference
130 let computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
131 // Only John should be deleted
132 let expected = vec![john_doe];
133 assert_eq!(expected, computed);
134
135 // Option 2: Delete based on a specific field
136 // Before delete
137 let before = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
138 let before: HashSet<UserInfo> = HashSet::from_iter(before.into_iter());
139
140 // Delete any occurrence where age = 10 (Actual call to delete)
141 surfer.delete_structs_by_field(&index_name, "age", "10").unwrap();
142
143 // After delete
144 let after = surfer.read_all_structs_by_field::<UserInfo>(&index_name, "age", "10").unwrap().unwrap();
145 let after: HashSet<UserInfo> = HashSet::from_iter(after.into_iter());
146 // Check difference
147 let mut computed: Vec<UserInfo> = before.difference(&after).map(|e| e.clone()).collect();
148 computed.sort();
149 // Both Jonny & Jinny should be deleted
150 let mut expected = vec![jonny_doe, jinny_doe];
151 expected.sort();
152 assert_eq!(expected, computed);
153
154
155 // Clean-up
156 let path = surfer.which_index(&index_name).unwrap();
157 let _ = remove_dir_all(&path);
158 let _ = remove_dir_all(&home);
159}
Sourcepub fn read_structs<T: Serialize + DeserializeOwned>(
&mut self,
name: &str,
query: &str,
limit: Option<usize>,
score: Option<f32>,
) -> Result<Option<Vec<T>>, IndexError>
pub fn read_structs<T: Serialize + DeserializeOwned>( &mut self, name: &str, query: &str, limit: Option<usize>, score: Option<f32>, ) -> Result<Option<Vec<T>>, IndexError>
Reads as struct
Examples found in repository?
examples/03_fuzzy_user_name.rs (line 64)
33fn main() {
34 let home = ".store".to_string();
35 let name = "fuzzy".to_string();
36
37 let mut builder = SurferBuilder::default();
38 builder.set_home(&home);
39
40 let data = User::default();
41 builder.add_struct(name.clone(), &data);
42
43 let mut surfer = Surfer::try_from(builder).unwrap();
44
45 let first = "John".to_string();
46 let last = "Doe".to_string();
47 let john_doe = User::new(first, last);
48
49 let first = "Jane".to_string();
50 let last = "Doe".to_string();
51 let jane_doe = User::new(first, last);
52
53 let users = vec![john_doe.clone(), jane_doe.clone()];
54 let _ = surfer.insert_structs(&name, &users).unwrap();
55 println!("===========================");
56 println!("Insert: John & Jane Doe");
57 println!("---------------------------");
58 println!("{:#?}", users);
59 println!("---------------------------");
60
61 block_thread(2);
62
63 let query = "deo";
64 let mut computed = surfer.read_structs::<User>(&name, query, Some(100), None).unwrap().unwrap();
65 computed.sort();
66 let mut expected = vec![];
67 expected.sort();
68 assert_eq!(computed, expected);
69
70 println!("====================================");
71 println!("Incorrect Query: '{}' Select: None", query);
72 println!("------------------------------------");
73 println!("{:#?}", computed);
74 println!("------------------------------------");
75
76 let fuzz = FuzzyWord::default();
77 let mod_query = fuzz.lookup(query);
78 assert!(mod_query.is_some());
79 let adjusted = mod_query.unwrap();
80 assert!(adjusted.len() >= 1);
81 let adjusted = adjusted.get(0).unwrap();
82 let mut computed = surfer.read_structs::<User>(&name, adjusted, Some(100), None).unwrap().unwrap();
83 computed.sort();
84 let mut expected = vec![john_doe.clone(), jane_doe.clone()];
85 expected.sort();
86 assert_eq!(computed, expected);
87
88 println!("====================================");
89 println!("Adjusted Query: '{}' Select: Jane & John Doe", adjusted);
90 println!("------------------------------------");
91 println!("{:#?}", computed);
92 println!("------------------------------------");
93
94
95 // Clean-up
96 let path = surfer.which_index(&name).unwrap();
97 let _ = remove_dir_all(&path);
98 let _ = remove_dir_all(&home);
99}
More examples
examples/02_example_from_tantivy.rs (line 63)
33fn main() {
34 let home = ".store".to_string();
35 let name = "tantivy".to_string();
36
37 // Mostly empty but can be a real flat struct
38 let data = OldMan::default();
39
40 // Prepare the builder instance
41 let mut builder = SurferBuilder::default();
42 // By default everything goes to directory indexes
43 builder.set_home(&home);
44 builder.add_struct(name.clone(), &data);
45
46 // Make the Surfer
47 let mut surfer = Surfer::try_from(builder).unwrap();
48
49 // Prepare your data or get it from somewhere
50 let title = "The Old Man and the Sea".to_string();
51 let body = "He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish.".to_string();
52 let old_man = OldMan::new(title, body);
53
54 // Insert the data so that store as only one document
55 let _ = surfer.insert_struct(&name, &old_man).unwrap();
56 println!("Inserting document: 1");
57
58 // Give some time to indexing to complete
59 block_thread(2);
60
61 // Lets query our one document
62 let query = "sea whale";
63 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
64 // Check one document
65 println!("Total documents found: {}", computed.len());
66 assert_eq!(computed, vec![old_man.clone()]);
67
68 // Insert the data so that store as two document
69 let _ = surfer.insert_struct(&name, &old_man).unwrap();
70 println!("Inserting document: 1");
71
72 // Give some time to indexing to complete
73 block_thread(2);
74
75 // Lets query again for two documents
76 let query = "sea whale";
77 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
78 // Check two documents
79 println!("Total documents found: {}", computed.len());
80 assert_eq!(computed, vec![old_man.clone(), old_man.clone()]);
81
82 // Lets add 100 more documents
83 let mut i = 0;
84 let mut documents = Vec::with_capacity(50);
85 while i < 50 {
86 documents.push(old_man.clone());
87 i = i + 1;
88 };
89 let _ = surfer.insert_structs(&name, &documents).unwrap();
90 println!("Inserting document: 50");
91
92 // Give some time to indexing to complete
93 block_thread(3);
94
95 // Lets query again for to get first 10 only
96 let query = "sea whale";
97 let computed = surfer.read_structs::<OldMan>(&name, query, None, None).unwrap().unwrap();
98 // Check 10 documents
99 println!("Total documents found: {} due to default limit = 10", computed.len());
100 assert_eq!(computed.len(), 10);
101
102 // Lets query again for to get first 10 only
103 let query = "sea whale";
104 let computed = surfer.read_structs::<OldMan>(&name, query, Some(100), None).unwrap().unwrap();
105 // Check 10 documents
106 println!("Total documents found: {} with limit = 100", computed.len());
107 assert_eq!(computed.len(), 52);
108
109 // Clean-up
110 let path = surfer.which_index(&name).unwrap();
111 let _ = remove_dir_all(&path);
112 let _ = remove_dir_all(&home);
113}
Trait Implementations§
Source§impl TryFrom<SurferBuilder> for Surf
impl TryFrom<SurferBuilder> for Surf
Source§type Error = IndexError
type Error = IndexError
The type returned in the event of a conversion error.
Auto Trait Implementations§
impl Freeze for Surf
impl !RefUnwindSafe for Surf
impl Send for Surf
impl Sync for Surf
impl Unpin for Surf
impl !UnwindSafe for Surf
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.