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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#[test]
fn simple() {
use crate::simple::{AutocompleteType, Indexable, SearchIndex, SearchType};
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct MyStruct {
title: String,
year: u16,
body: String,
}
impl Indexable for MyStruct {
fn strings(&self) -> Vec<String> {
vec![
self.title.clone(),
self.year.to_string(),
self.body.clone(),
]
}
}
let my_vec = vec![
MyStruct {
title: "Harold Godwinson".to_string(),
year: 1066,
body: "Last crowned Anglo-Saxon king of England.".to_string(),
},
MyStruct {
title: "Edgar Ætheling".to_string(),
year: 1066,
body: "Last male member of the royal house of Cerdic of Wessex.".to_string(),
},
MyStruct {
title: "William the Conqueror".to_string(),
year: 1066,
body: "First Norman monarch of England.".to_string(),
},
MyStruct {
title: "William Rufus".to_string(),
year: 1087,
body: "Third son of William the Conqueror.".to_string(),
},
MyStruct {
title: "Henry Beauclerc".to_string(),
year: 1100,
body: "Fourth son of William the Conqueror.".to_string(),
},
];
let mut search_index: SearchIndex<usize> = SearchIndex::default();
my_vec
.iter()
.enumerate()
.for_each(|(index, element)|
search_index.insert(&index, element)
);
let search_results = search_index.search("third fourth");
assert_eq!(search_results, vec![&3, &4]);
let search_results = search_index.search("last Wessex");
assert_eq!(search_results, vec![&1, &0]);
let search_results = search_index.search_type(&SearchType::Keyword, "Wessex");
assert_eq!(search_results, vec![&1]);
let search_results = search_index.search_type(&SearchType::Or, "last England");
assert_eq!(search_results, vec![&0, &1, &2]);
let search_results = search_index.search_type(&SearchType::And, "Conqueror third");
assert_eq!(search_results, vec![&3]);
let search_results = search_index.search_type(&SearchType::Live, "Last m");
assert_eq!(search_results, vec![&1]);
let autocomplete_options = search_index.autocomplete("Edgar last c");
assert_eq!(autocomplete_options, vec!["edgar last cerdic".to_string()]);
let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Keyword, "E");
assert_eq!(autocomplete_options, vec!["edgar".to_string(), "edgar ætheling".to_string(), "england".to_string()]);
let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Global, "1100 e");
assert_eq!(autocomplete_options, vec!["1100 edgar".to_string(), "1100 edgar ætheling".to_string(), "1100 england".to_string()]);
let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Context, "1087 w");
assert_eq!(autocomplete_options, vec!["1087 william".to_string(), "1087 william rufus".to_string()]);
let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Context, "108");
assert_eq!(autocomplete_options, vec!["1087".to_string()]);
}