This commit is contained in:
Alex Wied
2026-06-17 20:50:26 -04:00
parent 6df00eb5a3
commit f5ca8dd9ae
7 changed files with 352 additions and 160 deletions
+17 -17
View File
@@ -1,13 +1,13 @@
use crate::error;
use crate::{error, SearchDocument};
use crate::error::SearchError;
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, Value};
use tantivy::tokenizer::{NgramTokenizer, TokenizerManager};
use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, TantivyDocument, Term};
use tantivy::schema::{Field, IndexRecordOption, NamedFieldDocument, Value};
use tantivy::tokenizer::{LowerCaser, NgramTokenizer, TextAnalyzer, TokenizerManager};
use tantivy::{Document, Index, IndexReader, IndexWriter, ReloadPolicy, Score, TantivyDocument, Term};
#[derive(Default)]
pub struct SearchIndexBuilder {
@@ -23,8 +23,12 @@ impl SearchIndexBuilder {
pub fn build(self) -> error::Result<SearchIndex> {
if let Some(path) = self.path {
let ngram_32 = NgramTokenizer::new(1, 32, false)?;
let ngram_32_lowercase = TextAnalyzer::builder(ngram_32)
.filter(LowerCaser)
.build();
let tokenizer_manager = TokenizerManager::default();
tokenizer_manager.register("ngram_32", ngram_32);
tokenizer_manager.register("ngram_32", ngram_32_lowercase);
let mmap_directory = MmapDirectory::open(path)?;
let managed_directory = ManagedDirectory::wrap(Box::new(mmap_directory))?;
@@ -82,7 +86,7 @@ impl SearchIndex {
type_: Option<u64>,
user_query: &str,
default_fields: Vec<Field>,
) -> error::Result<Vec<String>> {
) -> error::Result<Vec<NamedFieldDocument>> {
let parser = QueryParser::for_index(&self.index, default_fields);
let (user_query, _) = parser.parse_query_lenient(user_query);
@@ -98,16 +102,12 @@ 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 mut iris = vec![];
for (_score, address) in results.iter() {
let doc: TantivyDocument = searcher.doc(*address)?;
if let Some(doc_iri) = doc.get_first(Schema::iri_field()) {
let doc_iri_string = doc_iri.as_str().unwrap_or("???").to_string();
iris.push(doc_iri_string);
}
}
Ok(iris)
let results = searcher.search(&query, &TopDocs::with_limit(10000).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)
}
}