diff options
| author | Fuwn <[email protected]> | 2022-04-18 01:56:26 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-18 01:56:26 -0700 |
| commit | 4a2f91cb02a6978401e0fe99528f43be3f8fc6a7 (patch) | |
| tree | ea3f4005a9e172109d383c7d5731b8135d26150f /src/main.rs | |
| parent | build(rustc): bump toolchain (diff) | |
| download | locus-4a2f91cb02a6978401e0fe99528f43be3f8fc6a7.tar.xz locus-4a2f91cb02a6978401e0fe99528f43be3f8fc6a7.zip | |
feat: prepare for real search engine
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index edcb36d..b9ed0cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,7 @@ mod macros; mod modules; mod route; +mod search; #[macro_use] extern crate log; @@ -40,6 +41,7 @@ use std::{collections::HashMap, lazy::SyncLazy, sync::Mutex}; use pickledb::PickleDb; use route::track_mount; +use search::{INDEX, SCHEMA}; use tokio::time::Instant; use windmark::{Response, Router}; use yarte::Template; @@ -287,5 +289,43 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { time_mount.elapsed().as_nanos() as f64 / 1_000_000.0 ); + std::thread::spawn(search::index); + + std::thread::spawn(|| { + loop { + std::thread::sleep(std::time::Duration::from_secs(1)); + + 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 reader = (*INDEX.lock().unwrap()) + .reader_builder() + .reload_policy(tantivy::ReloadPolicy::OnCommit) + .try_into() + .unwrap(); + let searcher = reader.searcher(); + let query_parser = tantivy::query::QueryParser::for_index( + &(*INDEX.lock().unwrap()), + vec![path, description, content], + ); + let query = query_parser.parse_query("Node.js").unwrap(); + let top_docs = searcher + .search(&query, &tantivy::collector::TopDocs::with_limit(10)) + .unwrap(); + + for (score, doc_address) in top_docs { + let retrieved_doc = searcher.doc(doc_address).unwrap(); + + println!( + "{}: {}", + score, + (*SCHEMA.lock().unwrap()).to_json(&retrieved_doc) + ); + } + } + }); + router.run().await } |