This commit is contained in:
Alex Wied
2026-06-29 23:10:22 -04:00
parent 59c8dffb01
commit 5cea8e8307
7 changed files with 129 additions and 115 deletions
+12 -15
View File
@@ -1,5 +1,4 @@
use crate::{error, SearchDocument};
use crate::error::SearchError;
use crate::schema::Schema;
use std::path::PathBuf;
use tantivy::collector::TopDocs;
@@ -8,6 +7,8 @@ use tantivy::query::{BooleanQuery, Occur, QueryParser, TermQuery};
use tantivy::schema::{Field, IndexRecordOption, NamedFieldDocument, Value};
use tantivy::tokenizer::{LowerCaser, NgramTokenizer, TextAnalyzer, TokenizerManager};
use tantivy::{Document, Index, IndexReader, IndexWriter, ReloadPolicy, Score, TantivyDocument, Term};
use tantivy::indexer::UserOperation;
use tracing::{info, instrument, span, Level};
#[derive(Default)]
pub struct SearchIndexBuilder {
@@ -46,20 +47,17 @@ impl SearchIndexBuilder {
.reload_policy(ReloadPolicy::OnCommitWithDelay)
.try_into()?;
let writer = index.writer(50_000_000)?;
Ok(SearchIndex {
index,
reader,
writer,
})
}
}
#[derive(Clone)]
pub struct SearchIndex {
index: Index,
reader: IndexReader,
writer: IndexWriter,
}
impl SearchIndex {
@@ -67,18 +65,14 @@ impl SearchIndex {
SearchIndexBuilder::default()
}
pub fn add<'a>(&self, document: TantivyDocument) -> crate::Result<()> {
self.writer.add_document(document)?;
Ok(())
pub fn writer(&self) -> crate::Result<IndexWriter> {
Ok(self.index.writer(128_000_000)?)
}
pub fn remove_all_of_type(&mut self, type_: u64) {
self.writer
.delete_term(Term::from_field_u64(Schema::type_field(), type_));
}
pub fn commit(&mut self) -> crate::Result<()> {
self.writer.commit()?;
pub fn remove_all_of_type(&mut self, type_: u64) -> crate::Result<()> {
let mut writer: IndexWriter<TantivyDocument> = self.index.writer(64_000_000)?;
writer.delete_term(Term::from_field_u64(Schema::type_field(), type_));
writer.commit()?;
Ok(())
}
@@ -88,6 +82,9 @@ impl SearchIndex {
user_query: &str,
default_fields: Vec<Field>,
) -> error::Result<Vec<NamedFieldDocument>> {
let span = span!(Level::INFO, "Search Query");
let _enter = span.enter();
let parser = QueryParser::for_index(&self.index, default_fields);
let (user_query, _) = parser.parse_query_lenient(user_query);
+1
View File
@@ -5,6 +5,7 @@ mod schema;
pub use tantivy::TantivyDocument as SearchDocument;
pub use tantivy::doc;
pub use tantivy::schema::{NamedFieldDocument, OwnedValue};
pub use tantivy::IndexWriter;
pub use error::{Result, SearchError};
pub use index::{SearchIndex, SearchIndexBuilder};