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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
use {
std::{
fmt::Write,
sync::{LazyLock, Mutex},
},
tantivy::schema,
tempfile::TempDir,
};
const SEARCH_INDEX_SIZE: usize = 10_000_000;
const SEARCH_SIZE: usize = 10;
static INDEX_PATH: LazyLock<Mutex<TempDir>> =
LazyLock::new(|| Mutex::new(TempDir::new().unwrap()));
static SCHEMA: LazyLock<Mutex<schema::Schema>> = LazyLock::new(|| {
Mutex::new({
let mut schema_builder = schema::Schema::builder();
schema_builder.add_text_field("path", schema::TEXT | schema::STORED);
schema_builder.add_text_field("description", schema::TEXT | schema::STORED);
schema_builder.add_text_field("content", schema::TEXT | schema::STORED);
schema_builder.build()
})
});
static INDEX: LazyLock<Mutex<tantivy::Index>> = LazyLock::new(|| {
Mutex::new({
tantivy::Index::create_in_dir(
&(*INDEX_PATH.lock().unwrap()),
(*SCHEMA.lock().unwrap()).clone(),
)
.unwrap()
})
});
static INDEX_WRITER: LazyLock<Mutex<tantivy::IndexWriter>> =
LazyLock::new(|| {
Mutex::new((*INDEX.lock().unwrap()).writer(SEARCH_INDEX_SIZE).unwrap())
});
pub(super) fn module(router: &mut windmark::router::Router) {
crate::route::track_mount(
router,
"/search",
"A search engine for this Gemini capsule",
|context| {
let mut response = String::from(
"# Search\n\n=> /search?action=go Search!\n=> /random I'm Feeling \
Lucky",
);
if let Some(query) = context.url.query_pairs().next() {
if query.0 == "action" && query.1 == "go" {
return windmark::response::Response::input(
"What would you like to search for?",
);
}
{
let path = (*SCHEMA.lock().unwrap()).get_field("path").unwrap();
let description =
(*SCHEMA.lock().unwrap()).get_field("description").unwrap();
let content = (*SCHEMA.lock().unwrap()).get_field("content").unwrap();
let mut results = String::new();
let searcher = (*INDEX.lock().unwrap())
.reader_builder()
.reload_policy(tantivy::ReloadPolicy::OnCommit)
.try_into()
.unwrap()
.searcher();
let top_docs = searcher
.search(
&tantivy::query::QueryParser::for_index(
&INDEX.lock().unwrap(),
vec![path, description, content],
)
.parse_query(&query.0)
.unwrap(),
&tantivy::collector::TopDocs::with_limit(SEARCH_SIZE),
)
.unwrap();
for (_score, document_address) in top_docs {
let retrieved_document = searcher.doc(document_address).unwrap();
macro_rules! text {
($field:ident) => {{
retrieved_document.get_first($field).unwrap().as_text().unwrap()
}}; /* ($document:ident, $field:ident) => {{
* $document.get_first($field).unwrap().as_text().
* unwrap() }}; */
}
let _ = write!(
results,
"{}",
&format!("=> {} {}{}\n", text!(path), text!(description), {
let mut lines = retrieved_document
.get_first(content)
.unwrap()
.as_text()
.unwrap()
.lines()
.skip(2);
lines.next().map_or_else(String::new, |first_line| {
let mut context_lines = lines.skip_while(|l| {
!l.to_lowercase().contains(&query.0.to_string())
});
format!(
"\n> ... {}\n> {}\n> {} ...",
first_line,
context_lines.next().unwrap_or(""),
context_lines.next().unwrap_or("")
)
})
})
);
}
let _ = write!(
response,
"{}",
&format!(
"\n\nYou searched for \"{}\"!\n\n## RESULTS\n\n{}\n\nIn need of \
more results? This search engine populates its index with \
route paths and route descriptions on startup. However, route \
content isn't populated until the route is first visited. \
After a route's first visit, it is updated after every five \
minutes, at time of visit.",
query.0,
if results.is_empty() {
"There are no results for your query...".to_string()
} else {
results.trim_end().to_string()
},
)
);
}
}
crate::response::success(&response, &context)
},
);
}
pub fn index() {
info!("spawned search indexer");
loop {
let path = (*SCHEMA.lock().unwrap()).get_field("path").unwrap();
let description =
(*SCHEMA.lock().unwrap()).get_field("description").unwrap();
let content = (*SCHEMA.lock().unwrap()).get_field("content").unwrap();
let time = tokio::time::Instant::now();
let mut new = 0;
for (route_path, information) in &(*crate::route::ROUTES.lock().unwrap()) {
// Pretty inefficient, but I'll figure this out later.
(*INDEX_WRITER.lock().unwrap()).delete_all_documents().unwrap();
(*INDEX_WRITER.lock().unwrap())
.add_document(tantivy::doc!(
path => route_path.clone(),
description => information.description.clone(),
content => information.text_cache.clone()
))
.unwrap();
new += 1;
}
(*INDEX_WRITER.lock().unwrap()).commit().unwrap();
info!(
"commit {} new items into search index in {}ms",
new,
time.elapsed().as_nanos() as f64 / 1_000_000.0
);
std::thread::sleep(std::time::Duration::from_secs(
crate::route::CACHE_RATE,
));
}
}
|