This commit is contained in:
Alex Wied
2026-07-20 23:53:33 -04:00
parent 57af8d592f
commit 21016af858
12 changed files with 456 additions and 372 deletions
+22 -5
View File
@@ -1,7 +1,8 @@
use std::fs;
use crate::{error, update};
use crate::{error, IndexWriter};
use crate::schema::Schema;
use std::path::PathBuf;
use std::sync::Arc;
use tantivy::collector::TopDocs;
use tantivy::directory::{ManagedDirectory, MmapDirectory};
use tantivy::query::{BooleanQuery, Occur, QueryParser, TermQuery};
@@ -51,6 +52,7 @@ impl SearchIndexBuilder {
Ok(SearchIndex {
index,
reader,
writer: None,
})
}
}
@@ -59,6 +61,7 @@ impl SearchIndexBuilder {
pub struct SearchIndex {
index: Index,
reader: IndexReader,
writer: Option<Arc<IndexWriter>>,
}
impl SearchIndex {
@@ -66,9 +69,23 @@ impl SearchIndex {
SearchIndexBuilder::default()
}
pub fn writer(&self) -> crate::Result<update::IndexWriter> {
let inner = self.index.writer(128 * 1024usize.pow(2))?;
Ok(crate::IndexWriter { inner })
pub fn writer(&mut self) -> crate::Result<&IndexWriter> {
if self.writer.is_none() {
let inner = self.index.writer(128 * 1024 * 1024)?;
self.writer = Some(Arc::new(IndexWriter { inner }));
}
Ok(self.writer.as_ref().unwrap())
}
pub fn commit(&mut self) -> crate::Result<()> {
if let Some(writer) = self.writer.take() {
if let Some(mut writer) = Arc::into_inner(writer) {
writer.commit()?;
}
}
Ok(())
}
pub fn query(
@@ -78,7 +95,7 @@ impl SearchIndex {
default_fields: Vec<Field>,
limit: usize,
) -> error::Result<Vec<TantivyDocument>> {
let _ = debug_span!("Search Query").entered();
let _span = debug_span!("Search Query").entered();
let parser = QueryParser::for_index(&self.index, default_fields);
let (user_query, _) = parser.parse_query_lenient(user_query);