diff options
Diffstat (limited to 'database/src/search.rs')
-rw-r--r-- | database/src/search.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/database/src/search.rs b/database/src/search.rs new file mode 100644 index 0000000..a7c074f --- /dev/null +++ b/database/src/search.rs @@ -0,0 +1,64 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2025 metamuffin <metamuffin.org> +*/ +use anyhow::Context; +use std::{fs::create_dir_all, path::Path, sync::RwLock}; +use tantivy::{ + DateOptions, Index, IndexReader, IndexWriter, ReloadPolicy, + directory::MmapDirectory, + schema::{FAST, Field, INDEXED, STORED, STRING, Schema, TEXT}, +}; + +pub struct NodeTextSearchIndex { + pub schema: Schema, + pub reader: IndexReader, + pub writer: RwLock<IndexWriter>, + pub index: Index, + pub id: Field, + pub title: Field, + pub releasedate: Field, + pub description: Field, + pub parent: Field, + pub f_index: Field, +} +impl NodeTextSearchIndex { + pub(crate) fn new(path: &Path) -> anyhow::Result<Self> { + let mut schema = Schema::builder(); + let id = schema.add_text_field("id", TEXT | STORED | FAST); + let title = schema.add_text_field("title", TEXT); + let description = schema.add_text_field("description", TEXT); + let parent = schema.add_text_field("parent", STRING | FAST); + let f_index = schema.add_u64_field("index", FAST); + let releasedate = schema.add_date_field( + "releasedate", + DateOptions::from(INDEXED) + .set_fast() + .set_precision(tantivy::DateTimePrecision::Seconds), + ); + let schema = schema.build(); + create_dir_all(path.join("node_index"))?; + let directory = + MmapDirectory::open(path.join("node_index")).context("opening index directory")?; + let index = Index::open_or_create(directory, schema.clone()).context("creating index")?; + let reader = index + .reader_builder() + .reload_policy(ReloadPolicy::OnCommitWithDelay) + .try_into() + .context("creating reader")?; + let writer = index.writer(30_000_000).context("creating writer")?; + Ok(Self { + index, + writer: writer.into(), + reader, + schema, + parent, + f_index, + releasedate, + id, + description, + title, + }) + } +} |