aboutsummaryrefslogtreecommitdiff
path: root/src/modules/search.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-01-12 05:56:09 -0800
committerFuwn <[email protected]>2026-01-12 05:56:09 -0800
commitcb6df8c2749d8d3b88342398f3e5c13457206a2f (patch)
treea56230a03f370f6b263db469d5e74e5786e3a7cb /src/modules/search.rs
parentfeat(directory): Sort routes by hits (diff)
downloadlocus-cb6df8c2749d8d3b88342398f3e5c13457206a2f.tar.xz
locus-cb6df8c2749d8d3b88342398f3e5c13457206a2f.zip
fix: Various optimisations
Diffstat (limited to 'src/modules/search.rs')
-rw-r--r--src/modules/search.rs63
1 files changed, 34 insertions, 29 deletions
diff --git a/src/modules/search.rs b/src/modules/search.rs
index bafafed..80c5c6f 100644
--- a/src/modules/search.rs
+++ b/src/modules/search.rs
@@ -56,31 +56,32 @@ pub(super) fn module(router: &mut windmark::router::Router) {
}
{
- 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 schema = (*SCHEMA.lock().unwrap()).clone();
+ let path = schema.get_field("path").unwrap();
+ let description = schema.get_field("description").unwrap();
+ let content = schema.get_field("content").unwrap();
let mut results = String::new();
-
- let searcher = (*INDEX.lock().unwrap())
+ let index = INDEX.lock().unwrap();
+ let searcher = index
.reader_builder()
.reload_policy(tantivy::ReloadPolicy::OnCommit)
.try_into()
.unwrap()
.searcher();
- let top_docs = searcher
+ let top_documents = searcher
.search(
- &tantivy::query::QueryParser::for_index(
- &INDEX.lock().unwrap(),
- vec![path, description, content],
- )
+ &tantivy::query::QueryParser::for_index(&index, vec![
+ path,
+ description,
+ content,
+ ])
.parse_query(&query.0)
.unwrap(),
&tantivy::collector::TopDocs::with_limit(SEARCH_SIZE),
)
.unwrap();
- for (_score, document_address) in top_docs {
+ for (_score, document_address) in top_documents {
let retrieved_document = searcher.doc(document_address).unwrap();
macro_rules! text {
@@ -149,29 +150,33 @@ 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 schema = (*SCHEMA.lock().unwrap()).clone();
+ let path = schema.get_field("path").unwrap();
+ let description = schema.get_field("description").unwrap();
+ let content = schema.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();
+ {
+ let routes = crate::route::ROUTES.lock().unwrap();
+ let mut index_writer = INDEX_WRITER.lock().unwrap();
- (*INDEX_WRITER.lock().unwrap())
- .add_document(tantivy::doc!(
- path => route_path.clone(),
- description => information.description.clone(),
- content => information.text_cache.clone()
- ))
- .unwrap();
+ index_writer.delete_all_documents().unwrap();
- new += 1;
- }
+ for (route_path, information) in routes.iter() {
+ index_writer
+ .add_document(tantivy::doc!(
+ path => route_path.clone(),
+ description => information.description.clone(),
+ content => information.text_cache.clone()
+ ))
+ .unwrap();
- (*INDEX_WRITER.lock().unwrap()).commit().unwrap();
+ new += 1;
+ }
+
+ index_writer.commit().unwrap();
+ }
info!(
"commit {} new items into search index in {}ms",