tantivy/
02_example_from_tantivy.rs

1use std::convert::TryFrom;
2use std::fs::remove_dir_all;
3
4use serde::{Serialize, Deserialize};
5
6use json_surf::prelude::*;
7
8/// Document to be indexed and searched
9#[derive(Serialize, Debug, Deserialize, PartialEq, Clone)]
10struct OldMan {
11    title: String,
12    body: String,
13}
14
15impl OldMan {
16    pub fn new(title: String, body: String) -> Self {
17        Self {
18            title,
19            body,
20        }
21    }
22}
23
24/// Convenience implementation for bootstraping the builder
25impl Default for OldMan {
26    fn default() -> Self {
27        let title = "".to_string();
28        let body = "".to_string();
29        OldMan::new(title, body)
30    }
31}
32
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}