.
This commit is contained in:
+5
-24
@@ -1,14 +1,13 @@
|
||||
use std::fs;
|
||||
use crate::{error, IndexWriter};
|
||||
use crate::error;
|
||||
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};
|
||||
use tantivy::schema::{Field, IndexRecordOption};
|
||||
use tantivy::tokenizer::{LowerCaser, NgramTokenizer, TextAnalyzer, TokenizerManager};
|
||||
use tantivy::{Index, IndexReader, ReloadPolicy, TantivyDocument, Term};
|
||||
use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, TantivyDocument, Term};
|
||||
use tracing::debug_span;
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -52,16 +51,13 @@ impl SearchIndexBuilder {
|
||||
Ok(SearchIndex {
|
||||
index,
|
||||
reader,
|
||||
writer: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SearchIndex {
|
||||
index: Index,
|
||||
reader: IndexReader,
|
||||
writer: Option<Arc<IndexWriter>>,
|
||||
}
|
||||
|
||||
impl SearchIndex {
|
||||
@@ -69,23 +65,8 @@ impl SearchIndex {
|
||||
SearchIndexBuilder::default()
|
||||
}
|
||||
|
||||
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 writer(&mut self) -> crate::Result<IndexWriter> {
|
||||
Ok(self.index.writer(128 * 1024 * 1024)?)
|
||||
}
|
||||
|
||||
pub fn query(
|
||||
@@ -95,7 +76,7 @@ impl SearchIndex {
|
||||
default_fields: Vec<Field>,
|
||||
limit: usize,
|
||||
) -> error::Result<Vec<TantivyDocument>> {
|
||||
let _span = debug_span!("Search Query").entered();
|
||||
let _enter = debug_span!("Search Query").entered();
|
||||
|
||||
let parser = QueryParser::for_index(&self.index, default_fields);
|
||||
let (user_query, _) = parser.parse_query_lenient(user_query);
|
||||
|
||||
+1
-3
@@ -1,7 +1,6 @@
|
||||
mod error;
|
||||
mod index;
|
||||
mod schema;
|
||||
mod update;
|
||||
|
||||
pub use tantivy::TantivyDocument as SearchDocument;
|
||||
pub use tantivy::doc;
|
||||
@@ -9,5 +8,4 @@ pub use tantivy::schema::document::Value;
|
||||
|
||||
pub use error::{Result, SearchError};
|
||||
pub use index::{SearchIndex, SearchIndexBuilder};
|
||||
pub use schema::Schema;
|
||||
pub use update::IndexWriter;
|
||||
pub use schema::Schema;
|
||||
@@ -1,28 +0,0 @@
|
||||
use tantivy::Term;
|
||||
use crate::Schema;
|
||||
|
||||
pub struct IndexWriter {
|
||||
pub(crate) inner: tantivy::IndexWriter,
|
||||
}
|
||||
|
||||
impl IndexWriter {
|
||||
pub fn add(&self, document: crate::SearchDocument) -> crate::Result<()> {
|
||||
let _ = self.inner.add_document(document)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_all_of_type(&self, type_: u64) -> crate::Result<()> {
|
||||
self.inner.delete_term(Term::from_field_u64(Schema::type_field(), type_));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_all(&self) -> crate::Result<()> {
|
||||
self.inner.delete_all_documents()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn commit(&mut self) -> crate::Result<()> {
|
||||
self.inner.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user