This commit is contained in:
Alex Wied
2026-06-30 19:25:15 -04:00
parent 5cea8e8307
commit 2a6e9432d9
21 changed files with 1039 additions and 235 deletions
+14 -19
View File
@@ -1,14 +1,15 @@
use crate::{error, SearchDocument};
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use crate::{error, update};
use crate::schema::Schema;
use std::path::PathBuf;
use tantivy::collector::TopDocs;
use tantivy::directory::{ManagedDirectory, MmapDirectory};
use tantivy::query::{BooleanQuery, Occur, QueryParser, TermQuery};
use tantivy::schema::{Field, IndexRecordOption, NamedFieldDocument, Value};
use tantivy::schema::{Field, IndexRecordOption};
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};
use tantivy::{Index, IndexReader, ReloadPolicy, TantivyDocument, Term};
use tracing::{span, Level};
#[derive(Default)]
pub struct SearchIndexBuilder {
@@ -65,15 +66,9 @@ impl SearchIndex {
SearchIndexBuilder::default()
}
pub fn writer(&self) -> crate::Result<IndexWriter> {
Ok(self.index.writer(128_000_000)?)
}
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(())
pub fn writer(&self) -> crate::Result<update::IndexWriter> {
let inner = self.index.writer(128 * 1024usize.pow(2))?;
Ok(crate::IndexWriter { inner })
}
pub fn query(
@@ -81,8 +76,9 @@ impl SearchIndex {
type_: Option<u64>,
user_query: &str,
default_fields: Vec<Field>,
) -> error::Result<Vec<NamedFieldDocument>> {
let span = span!(Level::INFO, "Search Query");
limit: usize,
) -> error::Result<Vec<TantivyDocument>> {
let span = span!(Level::DEBUG, "Search Query");
let _enter = span.enter();
let parser = QueryParser::for_index(&self.index, default_fields);
@@ -100,12 +96,11 @@ impl SearchIndex {
let query = BooleanQuery::new(subqueries);
let searcher = self.reader.searcher();
let results = searcher.search(&query, &TopDocs::with_limit(10000).order_by_score())?
let results: Vec<TantivyDocument> = searcher.search(&query, &TopDocs::with_limit(limit).order_by_score())?
.iter()
.map(|(_, address)| searcher.doc(*address))
.filter_map(Result::ok)
.map(|doc: SearchDocument| doc.to_named_doc(Schema::schema()))
.collect();
Ok(results)
}
}
}