From 25af8a1197de6b3aecb1fd7273e573b05f8e0000 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 18 Apr 2021 11:00:26 +0200 Subject: Add more fields Signed-off-by: Matthias Beyer --- src/schema.rs | 21 ++++++++++++++------- src/server.rs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/schema.rs b/src/schema.rs index 9a137eb..c051000 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -3,14 +3,21 @@ use tantivy::schema::*; pub fn schema() -> Schema { let mut schema_builder = Schema::builder(); - let body_options = TextOptions::default() - .set_stored() - .set_indexing_options(TextFieldIndexing::default() - .set_tokenizer("default") - .set_index_option(IndexRecordOption::WithFreqsAndPositions)); - schema_builder.add_text_field("path", STRING | STORED); schema_builder.add_text_field("ft", STRING | STORED); - schema_builder.add_text_field("body", body_options ); + schema_builder.add_u64_field("size", INDEXED | STORED); + schema_builder.add_i64_field("created", INDEXED | STORED); + schema_builder.add_i64_field("modified", INDEXED | STORED); + schema_builder.add_u64_field("indexed", INDEXED | STORED); + + { + let body_options = TextOptions::default() + .set_stored() + .set_indexing_options(TextFieldIndexing::default() + .set_tokenizer("default") + .set_index_option(IndexRecordOption::WithFreqsAndPositions)); + schema_builder.add_text_field("body", body_options); + } + schema_builder.build() } diff --git a/src/server.rs b/src/server.rs index 61251b6..f969e00 100644 --- a/src/server.rs +++ b/src/server.rs @@ -38,6 +38,18 @@ pub struct Server { #[getset(get_copy = "pub")] field_ft: tantivy::schema::Field, + #[getset(get_copy = "pub")] + field_size: tantivy::schema::Field, + + #[getset(get_copy = "pub")] + field_created: tantivy::schema::Field, + + #[getset(get_copy = "pub")] + field_modified: tantivy::schema::Field, + + #[getset(get_copy = "pub")] + field_indexed: tantivy::schema::Field, + #[getset(get_copy = "pub")] field_body: tantivy::schema::Field, } @@ -53,6 +65,15 @@ impl Server { let mut doc = tantivy::Document::default(); doc.add_text(self.field_path(), filepath.display().to_string()); doc.add_text(self.field_ft(), &ext); + { + use std::os::linux::fs::MetadataExt; + let meta = filepath.metadata()?; + doc.add_i64(self.field_created(), meta.st_atime()); + doc.add_i64(self.field_modified(), meta.st_mtime()); + if let Ok(now) = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).map(|d| d.as_secs()) { + doc.add_u64(self.field_indexed(), now) + } + } let doc = self.parse_for_file(doc, filepath, &ext)?; let mut index_writer = self.index.writer(50_000_000)?; @@ -79,7 +100,15 @@ impl Server { .try_into()?; let searcher = reader.searcher(); - let query_parser = tantivy::query::QueryParser::for_index(&self.index, vec![self.field_path, self.field_ft, self.field_body]); + let query_parser = tantivy::query::QueryParser::for_index(&self.index, vec![ + self.field_path, + self.field_ft, + self.field_size, + self.field_created, + self.field_modified, + self.field_indexed, + self.field_body, + ]); let query = query_parser.parse_query(query_str)?; let top_docs = searcher.search(&query, &tantivy::collector::TopDocs::with_limit(10))?; @@ -175,6 +204,10 @@ async fn main() -> Result<()> { let index = tantivy::Index::open_or_create(index_path, schema.clone())?; let field_path = schema.get_field("path").ok_or_else(|| anyhow!("BUG"))?; let field_ft = schema.get_field("ft").ok_or_else(|| anyhow!("BUG"))?; + let field_size = schema.get_field("size").ok_or_else(|| anyhow!("BUG"))?; + let field_created = schema.get_field("created").ok_or_else(|| anyhow!("BUG"))?; + let field_modified = schema.get_field("modified").ok_or_else(|| anyhow!("BUG"))?; + let field_indexed = schema.get_field("indexed").ok_or_else(|| anyhow!("BUG"))?; let field_body = schema.get_field("body").ok_or_else(|| anyhow!("BUG"))?; Server { @@ -182,6 +215,10 @@ async fn main() -> Result<()> { field_path, field_ft, + field_size, + field_created, + field_modified, + field_indexed, field_body, } }; -- cgit v1.2.3